test-loop-handles.c 9.7 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) {
  90. ASSERT(handle == &timer_handle);
  91. }
  92. static void idle_2_close_cb(uv_handle_t* handle) {
  93. fprintf(stderr, "%s", "IDLE_2_CLOSE_CB\n");
  94. fflush(stderr);
  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) {
  101. fprintf(stderr, "%s", "IDLE_2_CB\n");
  102. fflush(stderr);
  103. ASSERT(handle == &idle_2_handle);
  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) {
  108. int r;
  109. fprintf(stderr, "%s", "IDLE_1_CB\n");
  110. fflush(stderr);
  111. ASSERT(handle != NULL);
  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. fprintf(stderr, "%s", "IDLE_1_CLOSE_CB\n");
  131. fflush(stderr);
  132. ASSERT(handle != NULL);
  133. idle_1_close_cb_called++;
  134. }
  135. static void prepare_1_close_cb(uv_handle_t* handle) {
  136. fprintf(stderr, "%s", "PREPARE_1_CLOSE_CB");
  137. fflush(stderr);
  138. ASSERT(handle == (uv_handle_t*)&prepare_1_handle);
  139. prepare_1_close_cb_called++;
  140. }
  141. static void check_close_cb(uv_handle_t* handle) {
  142. fprintf(stderr, "%s", "CHECK_CLOSE_CB\n");
  143. fflush(stderr);
  144. ASSERT(handle == (uv_handle_t*)&check_handle);
  145. check_close_cb_called++;
  146. }
  147. static void prepare_2_close_cb(uv_handle_t* handle) {
  148. fprintf(stderr, "%s", "PREPARE_2_CLOSE_CB\n");
  149. fflush(stderr);
  150. ASSERT(handle == (uv_handle_t*)&prepare_2_handle);
  151. prepare_2_close_cb_called++;
  152. }
  153. static void check_cb(uv_check_t* handle) {
  154. int i, r;
  155. fprintf(stderr, "%s", "CHECK_CB\n");
  156. fflush(stderr);
  157. ASSERT(handle == &check_handle);
  158. if (loop_iteration < ITERATIONS) {
  159. /* Make some idle watchers active */
  160. for (i = 0; i < 1 + (loop_iteration % IDLE_COUNT); i++) {
  161. r = uv_idle_start(&idle_1_handles[i], idle_1_cb);
  162. ASSERT(r == 0);
  163. idles_1_active++;
  164. }
  165. } else {
  166. /* End of the test - close all handles */
  167. uv_close((uv_handle_t*)&prepare_1_handle, prepare_1_close_cb);
  168. uv_close((uv_handle_t*)&check_handle, check_close_cb);
  169. uv_close((uv_handle_t*)&prepare_2_handle, prepare_2_close_cb);
  170. for (i = 0; i < IDLE_COUNT; i++) {
  171. uv_close((uv_handle_t*)&idle_1_handles[i], idle_1_close_cb);
  172. }
  173. /* This handle is closed/recreated every time, close it only if it is
  174. * active. */
  175. if (idle_2_is_active) {
  176. uv_close((uv_handle_t*)&idle_2_handle, idle_2_close_cb);
  177. }
  178. }
  179. check_cb_called++;
  180. }
  181. static void prepare_2_cb(uv_prepare_t* handle) {
  182. int r;
  183. fprintf(stderr, "%s", "PREPARE_2_CB\n");
  184. fflush(stderr);
  185. ASSERT(handle == &prepare_2_handle);
  186. /* Prepare_2 gets started by prepare_1 when (loop_iteration % 2 == 0), and it
  187. * stops itself immediately. A started watcher is not queued until the next
  188. * round, so when this callback is made (loop_iteration % 2 == 0) cannot be
  189. * true. */
  190. ASSERT(loop_iteration % 2 != 0);
  191. r = uv_prepare_stop((uv_prepare_t*)handle);
  192. ASSERT(r == 0);
  193. prepare_2_cb_called++;
  194. }
  195. static void prepare_1_cb(uv_prepare_t* handle) {
  196. int r;
  197. fprintf(stderr, "%s", "PREPARE_1_CB\n");
  198. fflush(stderr);
  199. ASSERT(handle == &prepare_1_handle);
  200. if (loop_iteration % 2 == 0) {
  201. r = uv_prepare_start(&prepare_2_handle, prepare_2_cb);
  202. ASSERT(r == 0);
  203. }
  204. prepare_1_cb_called++;
  205. loop_iteration++;
  206. printf("Loop iteration %d of %d.\n", loop_iteration, ITERATIONS);
  207. }
  208. TEST_IMPL(loop_handles) {
  209. int i;
  210. int r;
  211. r = uv_prepare_init(uv_default_loop(), &prepare_1_handle);
  212. ASSERT(r == 0);
  213. r = uv_prepare_start(&prepare_1_handle, prepare_1_cb);
  214. ASSERT(r == 0);
  215. r = uv_check_init(uv_default_loop(), &check_handle);
  216. ASSERT(r == 0);
  217. r = uv_check_start(&check_handle, check_cb);
  218. ASSERT(r == 0);
  219. /* initialize only, prepare_2 is started by prepare_1_cb */
  220. r = uv_prepare_init(uv_default_loop(), &prepare_2_handle);
  221. ASSERT(r == 0);
  222. for (i = 0; i < IDLE_COUNT; i++) {
  223. /* initialize only, idle_1 handles are started by check_cb */
  224. r = uv_idle_init(uv_default_loop(), &idle_1_handles[i]);
  225. ASSERT(r == 0);
  226. }
  227. /* don't init or start idle_2, both is done by idle_1_cb */
  228. /* The timer callback is there to keep the event loop polling unref it as it
  229. * is not supposed to keep the loop alive */
  230. r = uv_timer_init(uv_default_loop(), &timer_handle);
  231. ASSERT(r == 0);
  232. r = uv_timer_start(&timer_handle, timer_cb, TIMEOUT, TIMEOUT);
  233. ASSERT(r == 0);
  234. uv_unref((uv_handle_t*)&timer_handle);
  235. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  236. ASSERT(r == 0);
  237. ASSERT(loop_iteration == ITERATIONS);
  238. ASSERT(prepare_1_cb_called == ITERATIONS);
  239. ASSERT(prepare_1_close_cb_called == 1);
  240. ASSERT(prepare_2_cb_called == ITERATIONS / 2);
  241. ASSERT(prepare_2_close_cb_called == 1);
  242. ASSERT(check_cb_called == ITERATIONS);
  243. ASSERT(check_close_cb_called == 1);
  244. /* idle_1_cb should be called a lot */
  245. ASSERT(idle_1_close_cb_called == IDLE_COUNT);
  246. ASSERT(idle_2_close_cb_called == idle_2_cb_started);
  247. ASSERT(idle_2_is_active == 0);
  248. MAKE_VALGRIND_HAPPY();
  249. return 0;
  250. }