test-timer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. #include "uv.h"
  22. #include "task.h"
  23. static int once_cb_called = 0;
  24. static int once_close_cb_called = 0;
  25. static int repeat_cb_called = 0;
  26. static int repeat_close_cb_called = 0;
  27. static int order_cb_called = 0;
  28. static uint64_t start_time;
  29. static uv_timer_t tiny_timer;
  30. static uv_timer_t huge_timer1;
  31. static uv_timer_t huge_timer2;
  32. static void once_close_cb(uv_handle_t* handle) {
  33. printf("ONCE_CLOSE_CB\n");
  34. ASSERT(handle != NULL);
  35. ASSERT(0 == uv_is_active(handle));
  36. once_close_cb_called++;
  37. }
  38. static void once_cb(uv_timer_t* handle) {
  39. printf("ONCE_CB %d\n", once_cb_called);
  40. ASSERT(handle != NULL);
  41. ASSERT(0 == uv_is_active((uv_handle_t*) handle));
  42. once_cb_called++;
  43. uv_close((uv_handle_t*)handle, once_close_cb);
  44. /* Just call this randomly for the code coverage. */
  45. uv_update_time(uv_default_loop());
  46. }
  47. static void repeat_close_cb(uv_handle_t* handle) {
  48. printf("REPEAT_CLOSE_CB\n");
  49. ASSERT(handle != NULL);
  50. repeat_close_cb_called++;
  51. }
  52. static void repeat_cb(uv_timer_t* handle) {
  53. printf("REPEAT_CB\n");
  54. ASSERT(handle != NULL);
  55. ASSERT(1 == uv_is_active((uv_handle_t*) handle));
  56. repeat_cb_called++;
  57. if (repeat_cb_called == 5) {
  58. uv_close((uv_handle_t*)handle, repeat_close_cb);
  59. }
  60. }
  61. static void never_cb(uv_timer_t* handle) {
  62. FATAL("never_cb should never be called");
  63. }
  64. TEST_IMPL(timer) {
  65. uv_timer_t once_timers[10];
  66. uv_timer_t *once;
  67. uv_timer_t repeat, never;
  68. unsigned int i;
  69. int r;
  70. start_time = uv_now(uv_default_loop());
  71. ASSERT(0 < start_time);
  72. /* Let 10 timers time out in 500 ms total. */
  73. for (i = 0; i < ARRAY_SIZE(once_timers); i++) {
  74. once = once_timers + i;
  75. r = uv_timer_init(uv_default_loop(), once);
  76. ASSERT(r == 0);
  77. r = uv_timer_start(once, once_cb, i * 50, 0);
  78. ASSERT(r == 0);
  79. }
  80. /* The 11th timer is a repeating timer that runs 4 times */
  81. r = uv_timer_init(uv_default_loop(), &repeat);
  82. ASSERT(r == 0);
  83. r = uv_timer_start(&repeat, repeat_cb, 100, 100);
  84. ASSERT(r == 0);
  85. /* The 12th timer should not do anything. */
  86. r = uv_timer_init(uv_default_loop(), &never);
  87. ASSERT(r == 0);
  88. r = uv_timer_start(&never, never_cb, 100, 100);
  89. ASSERT(r == 0);
  90. r = uv_timer_stop(&never);
  91. ASSERT(r == 0);
  92. uv_unref((uv_handle_t*)&never);
  93. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  94. ASSERT(once_cb_called == 10);
  95. ASSERT(once_close_cb_called == 10);
  96. printf("repeat_cb_called %d\n", repeat_cb_called);
  97. ASSERT(repeat_cb_called == 5);
  98. ASSERT(repeat_close_cb_called == 1);
  99. ASSERT(500 <= uv_now(uv_default_loop()) - start_time);
  100. MAKE_VALGRIND_HAPPY();
  101. return 0;
  102. }
  103. TEST_IMPL(timer_start_twice) {
  104. uv_timer_t once;
  105. int r;
  106. r = uv_timer_init(uv_default_loop(), &once);
  107. ASSERT(r == 0);
  108. r = uv_timer_start(&once, never_cb, 86400 * 1000, 0);
  109. ASSERT(r == 0);
  110. r = uv_timer_start(&once, once_cb, 10, 0);
  111. ASSERT(r == 0);
  112. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  113. ASSERT(r == 0);
  114. ASSERT(once_cb_called == 1);
  115. MAKE_VALGRIND_HAPPY();
  116. return 0;
  117. }
  118. TEST_IMPL(timer_init) {
  119. uv_timer_t handle;
  120. ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
  121. ASSERT(0 == uv_timer_get_repeat(&handle));
  122. ASSERT(0 == uv_is_active((uv_handle_t*) &handle));
  123. MAKE_VALGRIND_HAPPY();
  124. return 0;
  125. }
  126. static void order_cb_a(uv_timer_t *handle) {
  127. ASSERT(order_cb_called++ == *(int*)handle->data);
  128. }
  129. static void order_cb_b(uv_timer_t *handle) {
  130. ASSERT(order_cb_called++ == *(int*)handle->data);
  131. }
  132. TEST_IMPL(timer_order) {
  133. int first;
  134. int second;
  135. uv_timer_t handle_a;
  136. uv_timer_t handle_b;
  137. first = 0;
  138. second = 1;
  139. ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_a));
  140. ASSERT(0 == uv_timer_init(uv_default_loop(), &handle_b));
  141. /* Test for starting handle_a then handle_b */
  142. handle_a.data = &first;
  143. ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
  144. handle_b.data = &second;
  145. ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
  146. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  147. ASSERT(order_cb_called == 2);
  148. ASSERT(0 == uv_timer_stop(&handle_a));
  149. ASSERT(0 == uv_timer_stop(&handle_b));
  150. /* Test for starting handle_b then handle_a */
  151. order_cb_called = 0;
  152. handle_b.data = &first;
  153. ASSERT(0 == uv_timer_start(&handle_b, order_cb_b, 0, 0));
  154. handle_a.data = &second;
  155. ASSERT(0 == uv_timer_start(&handle_a, order_cb_a, 0, 0));
  156. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  157. ASSERT(order_cb_called == 2);
  158. MAKE_VALGRIND_HAPPY();
  159. return 0;
  160. }
  161. static void tiny_timer_cb(uv_timer_t* handle) {
  162. ASSERT(handle == &tiny_timer);
  163. uv_close((uv_handle_t*) &tiny_timer, NULL);
  164. uv_close((uv_handle_t*) &huge_timer1, NULL);
  165. uv_close((uv_handle_t*) &huge_timer2, NULL);
  166. }
  167. TEST_IMPL(timer_huge_timeout) {
  168. ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
  169. ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
  170. ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer2));
  171. ASSERT(0 == uv_timer_start(&tiny_timer, tiny_timer_cb, 1, 0));
  172. ASSERT(0 == uv_timer_start(&huge_timer1, tiny_timer_cb, 0xffffffffffffLL, 0));
  173. ASSERT(0 == uv_timer_start(&huge_timer2, tiny_timer_cb, (uint64_t) -1, 0));
  174. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  175. MAKE_VALGRIND_HAPPY();
  176. return 0;
  177. }
  178. static void huge_repeat_cb(uv_timer_t* handle) {
  179. static int ncalls;
  180. if (ncalls == 0)
  181. ASSERT(handle == &huge_timer1);
  182. else
  183. ASSERT(handle == &tiny_timer);
  184. if (++ncalls == 10) {
  185. uv_close((uv_handle_t*) &tiny_timer, NULL);
  186. uv_close((uv_handle_t*) &huge_timer1, NULL);
  187. }
  188. }
  189. TEST_IMPL(timer_huge_repeat) {
  190. ASSERT(0 == uv_timer_init(uv_default_loop(), &tiny_timer));
  191. ASSERT(0 == uv_timer_init(uv_default_loop(), &huge_timer1));
  192. ASSERT(0 == uv_timer_start(&tiny_timer, huge_repeat_cb, 2, 2));
  193. ASSERT(0 == uv_timer_start(&huge_timer1, huge_repeat_cb, 1, (uint64_t) -1));
  194. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  195. MAKE_VALGRIND_HAPPY();
  196. return 0;
  197. }
  198. static unsigned int timer_run_once_timer_cb_called;
  199. static void timer_run_once_timer_cb(uv_timer_t* handle) {
  200. timer_run_once_timer_cb_called++;
  201. }
  202. TEST_IMPL(timer_run_once) {
  203. uv_timer_t timer_handle;
  204. ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
  205. ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 0, 0));
  206. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
  207. ASSERT(1 == timer_run_once_timer_cb_called);
  208. ASSERT(0 == uv_timer_start(&timer_handle, timer_run_once_timer_cb, 1, 0));
  209. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
  210. ASSERT(2 == timer_run_once_timer_cb_called);
  211. uv_close((uv_handle_t*) &timer_handle, NULL);
  212. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
  213. MAKE_VALGRIND_HAPPY();
  214. return 0;
  215. }
  216. TEST_IMPL(timer_is_closing) {
  217. uv_timer_t handle;
  218. ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
  219. uv_close((uv_handle_t *)&handle, NULL);
  220. ASSERT(UV_EINVAL == uv_timer_start(&handle, never_cb, 100, 100));
  221. MAKE_VALGRIND_HAPPY();
  222. return 0;
  223. }
  224. TEST_IMPL(timer_null_callback) {
  225. uv_timer_t handle;
  226. ASSERT(0 == uv_timer_init(uv_default_loop(), &handle));
  227. ASSERT(UV_EINVAL == uv_timer_start(&handle, NULL, 100, 100));
  228. MAKE_VALGRIND_HAPPY();
  229. return 0;
  230. }
  231. static uint64_t timer_early_check_expected_time;
  232. static void timer_early_check_cb(uv_timer_t* handle) {
  233. uint64_t hrtime = uv_hrtime() / 1000000;
  234. ASSERT(hrtime >= timer_early_check_expected_time);
  235. }
  236. TEST_IMPL(timer_early_check) {
  237. uv_timer_t timer_handle;
  238. const uint64_t timeout_ms = 10;
  239. timer_early_check_expected_time = uv_now(uv_default_loop()) + timeout_ms;
  240. ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
  241. ASSERT(0 == uv_timer_start(&timer_handle, timer_early_check_cb, timeout_ms, 0));
  242. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  243. uv_close((uv_handle_t*) &timer_handle, NULL);
  244. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  245. MAKE_VALGRIND_HAPPY();
  246. return 0;
  247. }