Skip to content
This repository
Browse code

test: fix up test-callback-order.c

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...
commit 8143d7665c3ba2188267082f3d62747619c8159d 1 parent 3604b8d
Ben Noordhuis authored May 23, 2012
83  test/test-callback-order.c
@@ -19,58 +19,99 @@
19 19
  * IN THE SOFTWARE.
20 20
  */
21 21
 
  22
+/* This test is a port of test/simple/test-next-tick-ordering2.js */
  23
+
22 24
 #include "uv.h"
23 25
 #include "task.h"
24 26
 
25  
-static int idle_cb_called;
26  
-static int timer_cb_called;
  27
+typedef void (*next_tick_cb)(uv_loop_t*);
  28
+static next_tick_cb tick_cb;
27 29
 
28  
-static uv_idle_t idle_handle;
  30
+static uv_prepare_t prepare_tick_handle;
  31
+static uv_check_t check_tick_handle;
  32
+static uv_idle_t idle_tick_handle;
29 33
 static uv_timer_t timer_handle;
30 34
 
  35
+static int timer_cb_called;
  36
+static int tick2_cb_called;
31 37
 
32  
-/* idle_cb should run before timer_cb */
33  
-static void idle_cb(uv_idle_t* handle, int status) {
34  
-  ASSERT(idle_cb_called == 0);
35  
-  ASSERT(timer_cb_called == 0);
  38
+
  39
+static void tick(uv_loop_t* loop) {
  40
+  next_tick_cb cb = tick_cb;
  41
+  tick_cb = NULL;
  42
+  if (cb) cb(loop);
  43
+}
  44
+
  45
+
  46
+static void prepare_tick(uv_prepare_t* handle, int status) {
  47
+  ASSERT(handle == &prepare_tick_handle);
  48
+  tick(handle->loop);
  49
+}
  50
+
  51
+
  52
+static void check_tick(uv_check_t* handle, int status) {
  53
+  ASSERT(handle == &check_tick_handle);
  54
+  tick(handle->loop);
  55
+}
  56
+
  57
+
  58
+static void idle_tick(uv_idle_t* handle, int status) {
  59
+  ASSERT(handle == &idle_tick_handle);
36 60
   uv_idle_stop(handle);
37  
-  idle_cb_called++;
  61
+  tick(handle->loop);
  62
+}
  63
+
  64
+
  65
+static void next_tick(uv_loop_t* loop, next_tick_cb cb) {
  66
+  tick_cb = cb;
  67
+  uv_idle_start(&idle_tick_handle, idle_tick);
38 68
 }
39 69
 
40 70
 
41 71
 static void timer_cb(uv_timer_t* handle, int status) {
42  
-  ASSERT(idle_cb_called == 1);
43 72
   ASSERT(timer_cb_called == 0);
  73
+  ASSERT(tick2_cb_called == 1);
44 74
   uv_timer_stop(handle);
45 75
   timer_cb_called++;
46 76
 }
47 77
 
48 78
 
49  
-static void next_tick(uv_idle_t* handle, int status) {
50  
-  uv_loop_t* loop = handle->loop;
51  
-  uv_idle_stop(handle);
52  
-  uv_idle_init(loop, &idle_handle);
53  
-  uv_idle_start(&idle_handle, idle_cb);
  79
+static void tick_2(uv_loop_t* loop) {
  80
+  ASSERT(timer_cb_called == 0);
  81
+  ASSERT(tick2_cb_called == 0);
  82
+  tick2_cb_called++;
  83
+}
  84
+
  85
+
  86
+static void tick_1(uv_loop_t* loop) {
54 87
   uv_timer_init(loop, &timer_handle);
55 88
   uv_timer_start(&timer_handle, timer_cb, 0, 0);
  89
+  next_tick(loop, tick_2);
56 90
 }
57 91
 
58 92
 
59 93
 TEST_IMPL(callback_order) {
60  
-  uv_loop_t* loop;
61  
-  uv_idle_t idle;
  94
+  uv_loop_t* loop = uv_default_loop();
  95
+
  96
+  uv_prepare_init(loop, &prepare_tick_handle);
  97
+  uv_check_init(loop, &check_tick_handle);
  98
+  uv_idle_init(loop, &idle_tick_handle);
  99
+
  100
+  uv_prepare_start(&prepare_tick_handle, prepare_tick);
  101
+  uv_check_start(&check_tick_handle, check_tick);
  102
+
  103
+  uv_unref((uv_handle_t*)&prepare_tick_handle);
  104
+  uv_unref((uv_handle_t*)&check_tick_handle);
62 105
 
63  
-  loop = uv_default_loop();
64  
-  uv_idle_init(loop, &idle);
65  
-  uv_idle_start(&idle, next_tick);
  106
+  next_tick(loop, tick_1);
66 107
 
67  
-  ASSERT(idle_cb_called == 0);
68 108
   ASSERT(timer_cb_called == 0);
  109
+  ASSERT(tick2_cb_called == 0);
69 110
 
70 111
   uv_run(loop);
71 112
 
72  
-  ASSERT(idle_cb_called == 1);
73 113
   ASSERT(timer_cb_called == 1);
  114
+  ASSERT(tick2_cb_called == 1);
74 115
 
75 116
   return 0;
76 117
 }
3  test/test-list.h
@@ -182,10 +182,7 @@ HELPER_DECLARE (pipe_echo_server)
182 182
 TASK_LIST_START
183 183
   TEST_OUTPUT_ENTRY  (platform_output)
184 184
 
185  
-#if 0
186 185
   TEST_ENTRY  (callback_order)
187  
-#endif
188  
-
189 186
   TEST_ENTRY  (pipe_connect_bad_name)
190 187
   TEST_ENTRY  (pipe_connect_to_file)
191 188
 

0 notes on commit 8143d76

Please sign in to comment.
Something went wrong with that request. Please try again.