Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
test: fix up test-callback-order.c
Browse files Browse the repository at this point in the history
Turn test/test-callback-order.c into a straight-to-C copy of the
simple/test-next-tick-ordering2 test from node.js.
  • Loading branch information
bnoordhuis committed May 23, 2012
1 parent 3604b8d commit 8143d76
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
83 changes: 62 additions & 21 deletions test/test-callback-order.c
Expand Up @@ -19,58 +19,99 @@
* IN THE SOFTWARE.
*/

/* This test is a port of test/simple/test-next-tick-ordering2.js */

#include "uv.h"
#include "task.h"

static int idle_cb_called;
static int timer_cb_called;
typedef void (*next_tick_cb)(uv_loop_t*);
static next_tick_cb tick_cb;

static uv_idle_t idle_handle;
static uv_prepare_t prepare_tick_handle;
static uv_check_t check_tick_handle;
static uv_idle_t idle_tick_handle;
static uv_timer_t timer_handle;

static int timer_cb_called;
static int tick2_cb_called;

/* idle_cb should run before timer_cb */
static void idle_cb(uv_idle_t* handle, int status) {
ASSERT(idle_cb_called == 0);
ASSERT(timer_cb_called == 0);

static void tick(uv_loop_t* loop) {
next_tick_cb cb = tick_cb;
tick_cb = NULL;
if (cb) cb(loop);
}


static void prepare_tick(uv_prepare_t* handle, int status) {
ASSERT(handle == &prepare_tick_handle);
tick(handle->loop);
}


static void check_tick(uv_check_t* handle, int status) {
ASSERT(handle == &check_tick_handle);
tick(handle->loop);
}


static void idle_tick(uv_idle_t* handle, int status) {
ASSERT(handle == &idle_tick_handle);
uv_idle_stop(handle);
idle_cb_called++;
tick(handle->loop);
}


static void next_tick(uv_loop_t* loop, next_tick_cb cb) {
tick_cb = cb;
uv_idle_start(&idle_tick_handle, idle_tick);
}


static void timer_cb(uv_timer_t* handle, int status) {
ASSERT(idle_cb_called == 1);
ASSERT(timer_cb_called == 0);
ASSERT(tick2_cb_called == 1);
uv_timer_stop(handle);
timer_cb_called++;
}


static void next_tick(uv_idle_t* handle, int status) {
uv_loop_t* loop = handle->loop;
uv_idle_stop(handle);
uv_idle_init(loop, &idle_handle);
uv_idle_start(&idle_handle, idle_cb);
static void tick_2(uv_loop_t* loop) {
ASSERT(timer_cb_called == 0);
ASSERT(tick2_cb_called == 0);
tick2_cb_called++;
}


static void tick_1(uv_loop_t* loop) {
uv_timer_init(loop, &timer_handle);
uv_timer_start(&timer_handle, timer_cb, 0, 0);
next_tick(loop, tick_2);
}


TEST_IMPL(callback_order) {
uv_loop_t* loop;
uv_idle_t idle;
uv_loop_t* loop = uv_default_loop();

uv_prepare_init(loop, &prepare_tick_handle);
uv_check_init(loop, &check_tick_handle);
uv_idle_init(loop, &idle_tick_handle);

uv_prepare_start(&prepare_tick_handle, prepare_tick);
uv_check_start(&check_tick_handle, check_tick);

uv_unref((uv_handle_t*)&prepare_tick_handle);
uv_unref((uv_handle_t*)&check_tick_handle);

loop = uv_default_loop();
uv_idle_init(loop, &idle);
uv_idle_start(&idle, next_tick);
next_tick(loop, tick_1);

ASSERT(idle_cb_called == 0);
ASSERT(timer_cb_called == 0);
ASSERT(tick2_cb_called == 0);

uv_run(loop);

ASSERT(idle_cb_called == 1);
ASSERT(timer_cb_called == 1);
ASSERT(tick2_cb_called == 1);

return 0;
}
3 changes: 0 additions & 3 deletions test/test-list.h
Expand Up @@ -182,10 +182,7 @@ HELPER_DECLARE (pipe_echo_server)
TASK_LIST_START
TEST_OUTPUT_ENTRY (platform_output)

#if 0
TEST_ENTRY (callback_order)
#endif

TEST_ENTRY (pipe_connect_bad_name)
TEST_ENTRY (pipe_connect_to_file)

Expand Down

0 comments on commit 8143d76

Please sign in to comment.