test-loop-handles.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. /* Tests commented out with XXX are ones that are failing on Linux */
  22. /*
  23. * Purpose of this test is to check semantics of starting and stopping
  24. * prepare, check and idle watchers.
  25. *
  26. * - A watcher must be able to safely stop or close itself;
  27. * - Once a watcher is stopped or closed its callback should never be called.
  28. * - If a watcher is closed, it is implicitly stopped and its close_cb should
  29. * be called exactly once.
  30. * - A watcher can safely start and stop other watchers of the same type.
  31. * - Prepare and check watchers are called once per event loop iterations.
  32. * - All active idle watchers are queued when the event loop has no more work
  33. * to do. This is done repeatedly until all idle watchers are inactive.
  34. * - If a watcher starts another watcher of the same type its callback is not
  35. * immediately queued. For check and prepare watchers, that means that if
  36. * a watcher makes another of the same type active, it'll not be called until
  37. * the next event loop iteration. For idle. watchers this means that the
  38. * newly activated idle watcher might not be queued immediately.
  39. * - Prepare, check, idle watchers keep the event loop alive even when they're
  40. * not active.
  41. *
  42. * This is what the test globally does:
  43. *
  44. * - prepare_1 is always active and counts event loop iterations. It also
  45. * creates and starts prepare_2 every other iteration. Finally it verifies
  46. * that no idle watchers are active before polling.
  47. * - prepare_2 is started by prepare_1 every other iteration. It immediately
  48. * stops itself. It verifies that a watcher is not queued immediately
  49. * if created by another watcher of the same type.
  50. * - There's a check watcher that stops the event loop after a certain number
  51. * of iterations. It starts a varying number of idle_1 watchers.
  52. * - Idle_1 watchers stop themselves after being called a few times. All idle_1
  53. * watchers try to start the idle_2 watcher if it is not already started or
  54. * awaiting its close callback.
  55. * - The idle_2 watcher always exists but immediately closes itself after
  56. * being started by a check_1 watcher. It verifies that a watcher is
  57. * implicitly stopped when closed, and that a watcher can close itself
  58. * safely.
  59. * - There is a repeating timer. It does not keep the event loop alive
  60. * (ev_unref) but makes sure that the loop keeps polling the system for
  61. * events.
  62. */
  63. #include "uv.h"
  64. #include "task.h"
  65. #include <math.h>
  66. #define IDLE_COUNT 7
  67. #define ITERATIONS 21
  68. #define TIMEOUT 100
  69. static uv_prepare_t prepare_1_handle;
  70. static uv_prepare_t prepare_2_handle;
  71. static uv_check_t check_handle;
  72. static uv_idle_t idle_1_handles[IDLE_COUNT];
  73. static uv_idle_t idle_2_handle;
  74. static uv_timer_t timer_handle;
  75. static int loop_iteration = 0;
  76. static int prepare_1_cb_called = 0;
  77. static int prepare_1_close_cb_called = 0;
  78. static int prepare_2_cb_called = 0;
  79. static int prepare_2_close_cb_called = 0;
  80. static int check_cb_called = 0;
  81. static int check_close_cb_called = 0;
  82. static int idle_1_cb_called = 0;
  83. static int idle_1_close_cb_called = 0;
  84. static int idles_1_active = 0;
  85. static int idle_2_cb_called = 0;
  86. static int idle_2_close_cb_called = 0;
  87. static int idle_2_cb_started = 0;
  88. static int idle_2_is_active = 0;
  89. static void timer_cb(uv_timer_t* handle, int status) {
  90. ASSERT(handle == &timer_handle);
  91. ASSERT(status == 0);
  92. }
  93. static void idle_2_close_cb(uv_handle_t* handle) {
  94. LOG("IDLE_2_CLOSE_CB\n");
  95. ASSERT(handle == (uv_handle_t*)&idle_2_handle);
  96. ASSERT(idle_2_is_active);
  97. idle_2_close_cb_called++;
  98. idle_2_is_active = 0;
  99. }
  100. static void idle_2_cb(uv_idle_t* handle, int status) {
  101. LOG("IDLE_2_CB\n");
  102. ASSERT(handle == &idle_2_handle);
  103. ASSERT(status == 0);
  104. idle_2_cb_called++;
  105. uv_close((uv_handle_t*)handle, idle_2_close_cb);
  106. }
  107. static void idle_1_cb(uv_idle_t* handle, int status) {
  108. int r;
  109. LOG("IDLE_1_CB\n");
  110. ASSERT(handle != NULL);
  111. ASSERT(status == 0);
  112. ASSERT(idles_1_active > 0);
  113. /* Init idle_2 and make it active */
  114. if (!idle_2_is_active && !uv_is_closing((uv_handle_t*)&idle_2_handle)) {
  115. r = uv_idle_init(uv_default_loop(), &idle_2_handle);
  116. ASSERT(r == 0);
  117. r = uv_idle_start(&idle_2_handle, idle_2_cb);
  118. ASSERT(r == 0);
  119. idle_2_is_active = 1;
  120. idle_2_cb_started++;
  121. }
  122. idle_1_cb_called++;
  123. if (idle_1_cb_called % 5 == 0) {
  124. r = uv_idle_stop((uv_idle_t*)handle);
  125. ASSERT(r == 0);
  126. idles_1_active--;
  127. }
  128. }
  129. static void idle_1_close_cb(uv_handle_t* handle) {
  130. LOG("IDLE_1_CLOSE_CB\n");
  131. ASSERT(handle != NULL);
  132. idle_1_close_cb_called++;
  133. }
  134. static void prepare_1_close_cb(uv_handle_t* handle) {
  135. LOG("PREPARE_1_CLOSE_CB");
  136. ASSERT(handle == (uv_handle_t*)&prepare_1_handle);
  137. prepare_1_close_cb_called++;
  138. }
  139. static void check_close_cb(uv_handle_t* handle) {
  140. LOG("CHECK_CLOSE_CB\n");
  141. ASSERT(handle == (uv_handle_t*)&check_handle);
  142. check_close_cb_called++;
  143. }
  144. static void prepare_2_close_cb(uv_handle_t* handle) {
  145. LOG("PREPARE_2_CLOSE_CB\n");
  146. ASSERT(handle == (uv_handle_t*)&prepare_2_handle);
  147. prepare_2_close_cb_called++;
  148. }
  149. static void check_cb(uv_check_t* handle, int status) {
  150. int i, r;
  151. LOG("CHECK_CB\n");
  152. ASSERT(handle == &check_handle);
  153. ASSERT(status == 0);
  154. if (loop_iteration < ITERATIONS) {
  155. /* Make some idle watchers active */
  156. for (i = 0; i < 1 + (loop_iteration % IDLE_COUNT); i++) {
  157. r = uv_idle_start(&idle_1_handles[i], idle_1_cb);
  158. ASSERT(r == 0);
  159. idles_1_active++;
  160. }
  161. } else {
  162. /* End of the test - close all handles */
  163. uv_close((uv_handle_t*)&prepare_1_handle, prepare_1_close_cb);
  164. uv_close((uv_handle_t*)&check_handle, check_close_cb);
  165. uv_close((uv_handle_t*)&prepare_2_handle, prepare_2_close_cb);
  166. for (i = 0; i < IDLE_COUNT; i++) {
  167. uv_close((uv_handle_t*)&idle_1_handles[i], idle_1_close_cb);
  168. }
  169. /* This handle is closed/recreated every time, close it only if it is */
  170. /* active.*/
  171. if (idle_2_is_active) {
  172. uv_close((uv_handle_t*)&idle_2_handle, idle_2_close_cb);
  173. }
  174. }
  175. check_cb_called++;
  176. }
  177. static void prepare_2_cb(uv_prepare_t* handle, int status) {
  178. int r;
  179. LOG("PREPARE_2_CB\n");
  180. ASSERT(handle == &prepare_2_handle);
  181. ASSERT(status == 0);
  182. /* prepare_2 gets started by prepare_1 when (loop_iteration % 2 == 0), */
  183. /* and it stops itself immediately. A started watcher is not queued */
  184. /* until the next round, so when this callback is made */
  185. /* (loop_iteration % 2 == 0) cannot be true. */
  186. ASSERT(loop_iteration % 2 != 0);
  187. r = uv_prepare_stop((uv_prepare_t*)handle);
  188. ASSERT(r == 0);
  189. prepare_2_cb_called++;
  190. }
  191. static void prepare_1_cb(uv_prepare_t* handle, int status) {
  192. int r;
  193. LOG("PREPARE_1_CB\n");
  194. ASSERT(handle == &prepare_1_handle);
  195. ASSERT(status == 0);
  196. if (loop_iteration % 2 == 0) {
  197. r = uv_prepare_start(&prepare_2_handle, prepare_2_cb);
  198. ASSERT(r == 0);
  199. }
  200. prepare_1_cb_called++;
  201. loop_iteration++;
  202. printf("Loop iteration %d of %d.\n", loop_iteration, ITERATIONS);
  203. }
  204. TEST_IMPL(loop_handles) {
  205. int i;
  206. int r;
  207. r = uv_prepare_init(uv_default_loop(), &prepare_1_handle);
  208. ASSERT(r == 0);
  209. r = uv_prepare_start(&prepare_1_handle, prepare_1_cb);
  210. ASSERT(r == 0);
  211. r = uv_check_init(uv_default_loop(), &check_handle);
  212. ASSERT(r == 0);
  213. r = uv_check_start(&check_handle, check_cb);
  214. ASSERT(r == 0);
  215. /* initialize only, prepare_2 is started by prepare_1_cb */
  216. r = uv_prepare_init(uv_default_loop(), &prepare_2_handle);
  217. ASSERT(r == 0);
  218. for (i = 0; i < IDLE_COUNT; i++) {
  219. /* initialize only, idle_1 handles are started by check_cb */
  220. r = uv_idle_init(uv_default_loop(), &idle_1_handles[i]);
  221. ASSERT(r == 0);
  222. }
  223. /* don't init or start idle_2, both is done by idle_1_cb */
  224. /* the timer callback is there to keep the event loop polling */
  225. /* unref it as it is not supposed to keep the loop alive */
  226. r = uv_timer_init(uv_default_loop(), &timer_handle);
  227. ASSERT(r == 0);
  228. r = uv_timer_start(&timer_handle, timer_cb, TIMEOUT, TIMEOUT);
  229. ASSERT(r == 0);
  230. uv_unref((uv_handle_t*)&timer_handle);
  231. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  232. ASSERT(r == 0);
  233. ASSERT(loop_iteration == ITERATIONS);
  234. ASSERT(prepare_1_cb_called == ITERATIONS);
  235. ASSERT(prepare_1_close_cb_called == 1);
  236. ASSERT(prepare_2_cb_called == floor(ITERATIONS / 2.0));
  237. ASSERT(prepare_2_close_cb_called == 1);
  238. ASSERT(check_cb_called == ITERATIONS);
  239. ASSERT(check_close_cb_called == 1);
  240. /* idle_1_cb should be called a lot */
  241. ASSERT(idle_1_close_cb_called == IDLE_COUNT);
  242. ASSERT(idle_2_close_cb_called == idle_2_cb_started);
  243. ASSERT(idle_2_is_active == 0);
  244. MAKE_VALGRIND_HAPPY();
  245. return 0;
  246. }