test-timer.c 7.8 KB

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