Blame test/test-loop-handles.c

Packit b5b901
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Packit b5b901
 *
Packit b5b901
 * Permission is hereby granted, free of charge, to any person obtaining a copy
Packit b5b901
 * of this software and associated documentation files (the "Software"), to
Packit b5b901
 * deal in the Software without restriction, including without limitation the
Packit b5b901
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
Packit b5b901
 * sell copies of the Software, and to permit persons to whom the Software is
Packit b5b901
 * furnished to do so, subject to the following conditions:
Packit b5b901
 *
Packit b5b901
 * The above copyright notice and this permission notice shall be included in
Packit b5b901
 * all copies or substantial portions of the Software.
Packit b5b901
 *
Packit b5b901
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit b5b901
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit b5b901
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit b5b901
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Packit b5b901
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Packit b5b901
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
Packit b5b901
 * IN THE SOFTWARE.
Packit b5b901
 */
Packit b5b901
Packit b5b901
/* Tests commented out with XXX are ones that are failing on Linux */
Packit b5b901
Packit b5b901
/*
Packit b5b901
 * Purpose of this test is to check semantics of starting and stopping
Packit b5b901
 * prepare, check and idle watchers.
Packit b5b901
 *
Packit b5b901
 * - A watcher must be able to safely stop or close itself;
Packit b5b901
 * - Once a watcher is stopped or closed its callback should never be called.
Packit b5b901
 * - If a watcher is closed, it is implicitly stopped and its close_cb should
Packit b5b901
 *   be called exactly once.
Packit b5b901
 * - A watcher can safely start and stop other watchers of the same type.
Packit b5b901
 * - Prepare and check watchers are called once per event loop iterations.
Packit b5b901
 * - All active idle watchers are queued when the event loop has no more work
Packit b5b901
 *   to do. This is done repeatedly until all idle watchers are inactive.
Packit b5b901
 * - If a watcher starts another watcher of the same type its callback is not
Packit b5b901
 *   immediately queued. For check and prepare watchers, that means that if
Packit b5b901
 *   a watcher makes another of the same type active, it'll not be called until
Packit b5b901
 *   the next event loop iteration. For idle. watchers this means that the
Packit b5b901
 *   newly activated idle watcher might not be queued immediately.
Packit b5b901
 * - Prepare, check, idle watchers keep the event loop alive even when they're
Packit b5b901
 *   not active.
Packit b5b901
 *
Packit b5b901
 * This is what the test globally does:
Packit b5b901
 *
Packit b5b901
 * - prepare_1 is always active and counts event loop iterations. It also
Packit b5b901
 *   creates and starts prepare_2 every other iteration. Finally it verifies
Packit b5b901
 *   that no idle watchers are active before polling.
Packit b5b901
 * - prepare_2 is started by prepare_1 every other iteration. It immediately
Packit b5b901
 *   stops itself. It verifies that a watcher is not queued immediately
Packit b5b901
 *   if created by another watcher of the same type.
Packit b5b901
 * - There's a check watcher that stops the event loop after a certain number
Packit b5b901
 *   of iterations. It starts a varying number of idle_1 watchers.
Packit b5b901
 * - Idle_1 watchers stop themselves after being called a few times. All idle_1
Packit b5b901
 *   watchers try to start the idle_2 watcher if it is not already started or
Packit b5b901
 *   awaiting its close callback.
Packit b5b901
 * - The idle_2 watcher always exists but immediately closes itself after
Packit b5b901
 *   being started by a check_1 watcher. It verifies that a watcher is
Packit b5b901
 *   implicitly stopped when closed, and that a watcher can close itself
Packit b5b901
 *   safely.
Packit b5b901
 * - There is a repeating timer. It does not keep the event loop alive
Packit b5b901
 *   (ev_unref) but makes sure that the loop keeps polling the system for
Packit b5b901
 *   events.
Packit b5b901
 */
Packit b5b901
Packit b5b901
Packit b5b901
#include "uv.h"
Packit b5b901
#include "task.h"
Packit b5b901
Packit b5b901
#include <math.h>
Packit b5b901
Packit b5b901
Packit b5b901
#define IDLE_COUNT      7
Packit b5b901
#define ITERATIONS      21
Packit b5b901
#define TIMEOUT         100
Packit b5b901
Packit b5b901
Packit b5b901
static uv_prepare_t prepare_1_handle;
Packit b5b901
static uv_prepare_t prepare_2_handle;
Packit b5b901
Packit b5b901
static uv_check_t check_handle;
Packit b5b901
Packit b5b901
static uv_idle_t idle_1_handles[IDLE_COUNT];
Packit b5b901
static uv_idle_t idle_2_handle;
Packit b5b901
Packit b5b901
static uv_timer_t timer_handle;
Packit b5b901
Packit b5b901
Packit b5b901
static int loop_iteration = 0;
Packit b5b901
Packit b5b901
static int prepare_1_cb_called = 0;
Packit b5b901
static int prepare_1_close_cb_called = 0;
Packit b5b901
Packit b5b901
static int prepare_2_cb_called = 0;
Packit b5b901
static int prepare_2_close_cb_called = 0;
Packit b5b901
Packit b5b901
static int check_cb_called = 0;
Packit b5b901
static int check_close_cb_called = 0;
Packit b5b901
Packit b5b901
static int idle_1_cb_called = 0;
Packit b5b901
static int idle_1_close_cb_called = 0;
Packit b5b901
static int idles_1_active = 0;
Packit b5b901
Packit b5b901
static int idle_2_cb_called = 0;
Packit b5b901
static int idle_2_close_cb_called = 0;
Packit b5b901
static int idle_2_cb_started = 0;
Packit b5b901
static int idle_2_is_active = 0;
Packit b5b901
Packit b5b901
Packit b5b901
static void timer_cb(uv_timer_t* handle) {
Packit b5b901
  ASSERT(handle == &timer_handle);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void idle_2_close_cb(uv_handle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "IDLE_2_CLOSE_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
Packit b5b901
  ASSERT(handle == (uv_handle_t*)&idle_2_handle);
Packit b5b901
Packit b5b901
  ASSERT(idle_2_is_active);
Packit b5b901
Packit b5b901
  idle_2_close_cb_called++;
Packit b5b901
  idle_2_is_active = 0;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void idle_2_cb(uv_idle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "IDLE_2_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
Packit b5b901
  ASSERT(handle == &idle_2_handle);
Packit b5b901
Packit b5b901
  idle_2_cb_called++;
Packit b5b901
Packit b5b901
  uv_close((uv_handle_t*)handle, idle_2_close_cb);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void idle_1_cb(uv_idle_t* handle) {
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  fprintf(stderr, "%s", "IDLE_1_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
Packit b5b901
  ASSERT(handle != NULL);
Packit b5b901
  ASSERT(idles_1_active > 0);
Packit b5b901
Packit b5b901
  /* Init idle_2 and make it active */
Packit b5b901
  if (!idle_2_is_active && !uv_is_closing((uv_handle_t*)&idle_2_handle)) {
Packit b5b901
    r = uv_idle_init(uv_default_loop(), &idle_2_handle);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
    r = uv_idle_start(&idle_2_handle, idle_2_cb);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
    idle_2_is_active = 1;
Packit b5b901
    idle_2_cb_started++;
Packit b5b901
  }
Packit b5b901
Packit b5b901
  idle_1_cb_called++;
Packit b5b901
Packit b5b901
  if (idle_1_cb_called % 5 == 0) {
Packit b5b901
    r = uv_idle_stop((uv_idle_t*)handle);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
    idles_1_active--;
Packit b5b901
  }
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void idle_1_close_cb(uv_handle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "IDLE_1_CLOSE_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
Packit b5b901
  ASSERT(handle != NULL);
Packit b5b901
Packit b5b901
  idle_1_close_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void prepare_1_close_cb(uv_handle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "PREPARE_1_CLOSE_CB");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == (uv_handle_t*)&prepare_1_handle);
Packit b5b901
Packit b5b901
  prepare_1_close_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void check_close_cb(uv_handle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "CHECK_CLOSE_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == (uv_handle_t*)&check_handle);
Packit b5b901
Packit b5b901
  check_close_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void prepare_2_close_cb(uv_handle_t* handle) {
Packit b5b901
  fprintf(stderr, "%s", "PREPARE_2_CLOSE_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == (uv_handle_t*)&prepare_2_handle);
Packit b5b901
Packit b5b901
  prepare_2_close_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void check_cb(uv_check_t* handle) {
Packit b5b901
  int i, r;
Packit b5b901
Packit b5b901
  fprintf(stderr, "%s", "CHECK_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == &check_handle);
Packit b5b901
Packit b5b901
  if (loop_iteration < ITERATIONS) {
Packit b5b901
    /* Make some idle watchers active */
Packit b5b901
    for (i = 0; i < 1 + (loop_iteration % IDLE_COUNT); i++) {
Packit b5b901
      r = uv_idle_start(&idle_1_handles[i], idle_1_cb);
Packit b5b901
      ASSERT(r == 0);
Packit b5b901
      idles_1_active++;
Packit b5b901
    }
Packit b5b901
Packit b5b901
  } else {
Packit b5b901
    /* End of the test - close all handles */
Packit b5b901
    uv_close((uv_handle_t*)&prepare_1_handle, prepare_1_close_cb);
Packit b5b901
    uv_close((uv_handle_t*)&check_handle, check_close_cb);
Packit b5b901
    uv_close((uv_handle_t*)&prepare_2_handle, prepare_2_close_cb);
Packit b5b901
Packit b5b901
    for (i = 0; i < IDLE_COUNT; i++) {
Packit b5b901
      uv_close((uv_handle_t*)&idle_1_handles[i], idle_1_close_cb);
Packit b5b901
    }
Packit b5b901
Packit b5b901
    /* This handle is closed/recreated every time, close it only if it is
Packit b5b901
     * active. */
Packit b5b901
    if (idle_2_is_active) {
Packit b5b901
      uv_close((uv_handle_t*)&idle_2_handle, idle_2_close_cb);
Packit b5b901
    }
Packit b5b901
  }
Packit b5b901
Packit b5b901
  check_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void prepare_2_cb(uv_prepare_t* handle) {
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  fprintf(stderr, "%s", "PREPARE_2_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == &prepare_2_handle);
Packit b5b901
Packit b5b901
  /* Prepare_2 gets started by prepare_1 when (loop_iteration % 2 == 0), and it
Packit b5b901
   * stops itself immediately. A started watcher is not queued until the next
Packit b5b901
   * round, so when this callback is made (loop_iteration % 2 == 0) cannot be
Packit b5b901
   * true. */
Packit b5b901
  ASSERT(loop_iteration % 2 != 0);
Packit b5b901
Packit b5b901
  r = uv_prepare_stop((uv_prepare_t*)handle);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  prepare_2_cb_called++;
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
static void prepare_1_cb(uv_prepare_t* handle) {
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  fprintf(stderr, "%s", "PREPARE_1_CB\n");
Packit b5b901
  fflush(stderr);
Packit b5b901
  ASSERT(handle == &prepare_1_handle);
Packit b5b901
Packit b5b901
  if (loop_iteration % 2 == 0) {
Packit b5b901
    r = uv_prepare_start(&prepare_2_handle, prepare_2_cb);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  prepare_1_cb_called++;
Packit b5b901
  loop_iteration++;
Packit b5b901
Packit b5b901
  printf("Loop iteration %d of %d.\n", loop_iteration, ITERATIONS);
Packit b5b901
}
Packit b5b901
Packit b5b901
Packit b5b901
TEST_IMPL(loop_handles) {
Packit b5b901
  int i;
Packit b5b901
  int r;
Packit b5b901
Packit b5b901
  r = uv_prepare_init(uv_default_loop(), &prepare_1_handle);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  r = uv_prepare_start(&prepare_1_handle, prepare_1_cb);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  r = uv_check_init(uv_default_loop(), &check_handle);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  r = uv_check_start(&check_handle, check_cb);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  /* initialize only, prepare_2 is started by prepare_1_cb */
Packit b5b901
  r = uv_prepare_init(uv_default_loop(), &prepare_2_handle);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  for (i = 0; i < IDLE_COUNT; i++) {
Packit b5b901
    /* initialize only, idle_1 handles are started by check_cb */
Packit b5b901
    r = uv_idle_init(uv_default_loop(), &idle_1_handles[i]);
Packit b5b901
    ASSERT(r == 0);
Packit b5b901
  }
Packit b5b901
Packit b5b901
  /* don't init or start idle_2, both is done by idle_1_cb */
Packit b5b901
Packit b5b901
  /* The timer callback is there to keep the event loop polling unref it as it
Packit b5b901
   * is not supposed to keep the loop alive */
Packit b5b901
  r = uv_timer_init(uv_default_loop(), &timer_handle);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  r = uv_timer_start(&timer_handle, timer_cb, TIMEOUT, TIMEOUT);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
  uv_unref((uv_handle_t*)&timer_handle);
Packit b5b901
Packit b5b901
  r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
Packit b5b901
  ASSERT(r == 0);
Packit b5b901
Packit b5b901
  ASSERT(loop_iteration == ITERATIONS);
Packit b5b901
Packit b5b901
  ASSERT(prepare_1_cb_called == ITERATIONS);
Packit b5b901
  ASSERT(prepare_1_close_cb_called == 1);
Packit b5b901
Packit Service e08953
  ASSERT(prepare_2_cb_called == ITERATIONS / 2);
Packit b5b901
  ASSERT(prepare_2_close_cb_called == 1);
Packit b5b901
Packit b5b901
  ASSERT(check_cb_called == ITERATIONS);
Packit b5b901
  ASSERT(check_close_cb_called == 1);
Packit b5b901
Packit b5b901
  /* idle_1_cb should be called a lot */
Packit b5b901
  ASSERT(idle_1_close_cb_called == IDLE_COUNT);
Packit b5b901
Packit b5b901
  ASSERT(idle_2_close_cb_called == idle_2_cb_started);
Packit b5b901
  ASSERT(idle_2_is_active == 0);
Packit b5b901
Packit b5b901
  MAKE_VALGRIND_HAPPY();
Packit b5b901
  return 0;
Packit b5b901
}