This source file includes following definitions.
- timer_cb
- idle_2_close_cb
- idle_2_cb
- idle_1_cb
- idle_1_close_cb
- prepare_1_close_cb
- check_close_cb
- prepare_2_close_cb
- check_cb
- prepare_2_cb
- prepare_1_cb
- TEST_IMPL
#include "uv.h"
#include "task.h"
#include <math.h>
#define IDLE_COUNT 7
#define ITERATIONS 21
#define TIMEOUT 100
static uv_prepare_t prepare_1_handle;
static uv_prepare_t prepare_2_handle;
static uv_check_t check_handle;
static uv_idle_t idle_1_handles[IDLE_COUNT];
static uv_idle_t idle_2_handle;
static uv_timer_t timer_handle;
static int loop_iteration = 0;
static int prepare_1_cb_called = 0;
static int prepare_1_close_cb_called = 0;
static int prepare_2_cb_called = 0;
static int prepare_2_close_cb_called = 0;
static int check_cb_called = 0;
static int check_close_cb_called = 0;
static int idle_1_cb_called = 0;
static int idle_1_close_cb_called = 0;
static int idles_1_active = 0;
static int idle_2_cb_called = 0;
static int idle_2_close_cb_called = 0;
static int idle_2_cb_started = 0;
static int idle_2_is_active = 0;
static void timer_cb(uv_timer_t* handle, int status) {
ASSERT(handle == &timer_handle);
ASSERT(status == 0);
}
static void idle_2_close_cb(uv_handle_t* handle) {
LOG("IDLE_2_CLOSE_CB\n");
ASSERT(handle == (uv_handle_t*)&idle_2_handle);
ASSERT(idle_2_is_active);
idle_2_close_cb_called++;
idle_2_is_active = 0;
}
static void idle_2_cb(uv_idle_t* handle, int status) {
LOG("IDLE_2_CB\n");
ASSERT(handle == &idle_2_handle);
ASSERT(status == 0);
idle_2_cb_called++;
uv_close((uv_handle_t*)handle, idle_2_close_cb);
}
static void idle_1_cb(uv_idle_t* handle, int status) {
int r;
LOG("IDLE_1_CB\n");
ASSERT(handle != NULL);
ASSERT(status == 0);
ASSERT(idles_1_active > 0);
if (!idle_2_is_active && !uv_is_closing((uv_handle_t*)&idle_2_handle)) {
r = uv_idle_init(uv_default_loop(), &idle_2_handle);
ASSERT(r == 0);
r = uv_idle_start(&idle_2_handle, idle_2_cb);
ASSERT(r == 0);
idle_2_is_active = 1;
idle_2_cb_started++;
}
idle_1_cb_called++;
if (idle_1_cb_called % 5 == 0) {
r = uv_idle_stop((uv_idle_t*)handle);
ASSERT(r == 0);
idles_1_active--;
}
}
static void idle_1_close_cb(uv_handle_t* handle) {
LOG("IDLE_1_CLOSE_CB\n");
ASSERT(handle != NULL);
idle_1_close_cb_called++;
}
static void prepare_1_close_cb(uv_handle_t* handle) {
LOG("PREPARE_1_CLOSE_CB");
ASSERT(handle == (uv_handle_t*)&prepare_1_handle);
prepare_1_close_cb_called++;
}
static void check_close_cb(uv_handle_t* handle) {
LOG("CHECK_CLOSE_CB\n");
ASSERT(handle == (uv_handle_t*)&check_handle);
check_close_cb_called++;
}
static void prepare_2_close_cb(uv_handle_t* handle) {
LOG("PREPARE_2_CLOSE_CB\n");
ASSERT(handle == (uv_handle_t*)&prepare_2_handle);
prepare_2_close_cb_called++;
}
static void check_cb(uv_check_t* handle, int status) {
int i, r;
LOG("CHECK_CB\n");
ASSERT(handle == &check_handle);
ASSERT(status == 0);
if (loop_iteration < ITERATIONS) {
for (i = 0; i < 1 + (loop_iteration % IDLE_COUNT); i++) {
r = uv_idle_start(&idle_1_handles[i], idle_1_cb);
ASSERT(r == 0);
idles_1_active++;
}
} else {
uv_close((uv_handle_t*)&prepare_1_handle, prepare_1_close_cb);
uv_close((uv_handle_t*)&check_handle, check_close_cb);
uv_close((uv_handle_t*)&prepare_2_handle, prepare_2_close_cb);
for (i = 0; i < IDLE_COUNT; i++) {
uv_close((uv_handle_t*)&idle_1_handles[i], idle_1_close_cb);
}
if (idle_2_is_active) {
uv_close((uv_handle_t*)&idle_2_handle, idle_2_close_cb);
}
}
check_cb_called++;
}
static void prepare_2_cb(uv_prepare_t* handle, int status) {
int r;
LOG("PREPARE_2_CB\n");
ASSERT(handle == &prepare_2_handle);
ASSERT(status == 0);
ASSERT(loop_iteration % 2 != 0);
r = uv_prepare_stop((uv_prepare_t*)handle);
ASSERT(r == 0);
prepare_2_cb_called++;
}
static void prepare_1_cb(uv_prepare_t* handle, int status) {
int r;
LOG("PREPARE_1_CB\n");
ASSERT(handle == &prepare_1_handle);
ASSERT(status == 0);
if (loop_iteration % 2 == 0) {
r = uv_prepare_start(&prepare_2_handle, prepare_2_cb);
ASSERT(r == 0);
}
prepare_1_cb_called++;
loop_iteration++;
printf("Loop iteration %d of %d.\n", loop_iteration, ITERATIONS);
}
TEST_IMPL(loop_handles) {
int i;
int r;
r = uv_prepare_init(uv_default_loop(), &prepare_1_handle);
ASSERT(r == 0);
r = uv_prepare_start(&prepare_1_handle, prepare_1_cb);
ASSERT(r == 0);
r = uv_check_init(uv_default_loop(), &check_handle);
ASSERT(r == 0);
r = uv_check_start(&check_handle, check_cb);
ASSERT(r == 0);
r = uv_prepare_init(uv_default_loop(), &prepare_2_handle);
ASSERT(r == 0);
for (i = 0; i < IDLE_COUNT; i++) {
r = uv_idle_init(uv_default_loop(), &idle_1_handles[i]);
ASSERT(r == 0);
}
r = uv_timer_init(uv_default_loop(), &timer_handle);
ASSERT(r == 0);
r = uv_timer_start(&timer_handle, timer_cb, TIMEOUT, TIMEOUT);
ASSERT(r == 0);
uv_unref((uv_handle_t*)&timer_handle);
r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(r == 0);
ASSERT(loop_iteration == ITERATIONS);
ASSERT(prepare_1_cb_called == ITERATIONS);
ASSERT(prepare_1_close_cb_called == 1);
ASSERT(prepare_2_cb_called == floor(ITERATIONS / 2.0));
ASSERT(prepare_2_close_cb_called == 1);
ASSERT(check_cb_called == ITERATIONS);
ASSERT(check_close_cb_called == 1);
ASSERT(idle_1_close_cb_called == IDLE_COUNT);
ASSERT(idle_2_close_cb_called == idle_2_cb_started);
ASSERT(idle_2_is_active == 0);
MAKE_VALGRIND_HAPPY();
return 0;
}