test-timer-again.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 close_cb_called = 0;
  24. static int repeat_1_cb_called = 0;
  25. static int repeat_2_cb_called = 0;
  26. static int repeat_2_cb_allowed = 0;
  27. static uv_timer_t dummy, repeat_1, repeat_2;
  28. static uint64_t start_time;
  29. static void close_cb(uv_handle_t* handle) {
  30. ASSERT(handle != NULL);
  31. close_cb_called++;
  32. }
  33. static void repeat_1_cb(uv_timer_t* handle, int status) {
  34. int r;
  35. ASSERT(handle == &repeat_1);
  36. ASSERT(status == 0);
  37. ASSERT(uv_timer_get_repeat((uv_timer_t*)handle) == 50);
  38. LOGF("repeat_1_cb called after %ld ms\n",
  39. (long int)(uv_now(uv_default_loop()) - start_time));
  40. repeat_1_cb_called++;
  41. r = uv_timer_again(&repeat_2);
  42. ASSERT(r == 0);
  43. if (repeat_1_cb_called == 10) {
  44. uv_close((uv_handle_t*)handle, close_cb);
  45. /* We're not calling uv_timer_again on repeat_2 any more, so after this */
  46. /* timer_2_cb is expected. */
  47. repeat_2_cb_allowed = 1;
  48. return;
  49. }
  50. }
  51. static void repeat_2_cb(uv_timer_t* handle, int status) {
  52. ASSERT(handle == &repeat_2);
  53. ASSERT(status == 0);
  54. ASSERT(repeat_2_cb_allowed);
  55. LOGF("repeat_2_cb called after %ld ms\n",
  56. (long int)(uv_now(uv_default_loop()) - start_time));
  57. repeat_2_cb_called++;
  58. if (uv_timer_get_repeat(&repeat_2) == 0) {
  59. ASSERT(0 == uv_is_active((uv_handle_t*) handle));
  60. uv_close((uv_handle_t*)handle, close_cb);
  61. return;
  62. }
  63. LOGF("uv_timer_get_repeat %ld ms\n",
  64. (long int)uv_timer_get_repeat(&repeat_2));
  65. ASSERT(uv_timer_get_repeat(&repeat_2) == 100);
  66. /* This shouldn't take effect immediately. */
  67. uv_timer_set_repeat(&repeat_2, 0);
  68. }
  69. TEST_IMPL(timer_again) {
  70. int r;
  71. start_time = uv_now(uv_default_loop());
  72. ASSERT(0 < start_time);
  73. /* Verify that it is not possible to uv_timer_again a never-started timer. */
  74. r = uv_timer_init(uv_default_loop(), &dummy);
  75. ASSERT(r == 0);
  76. r = uv_timer_again(&dummy);
  77. ASSERT(r == UV_EINVAL);
  78. uv_unref((uv_handle_t*)&dummy);
  79. /* Start timer repeat_1. */
  80. r = uv_timer_init(uv_default_loop(), &repeat_1);
  81. ASSERT(r == 0);
  82. r = uv_timer_start(&repeat_1, repeat_1_cb, 50, 0);
  83. ASSERT(r == 0);
  84. ASSERT(uv_timer_get_repeat(&repeat_1) == 0);
  85. /* Actually make repeat_1 repeating. */
  86. uv_timer_set_repeat(&repeat_1, 50);
  87. ASSERT(uv_timer_get_repeat(&repeat_1) == 50);
  88. /*
  89. * Start another repeating timer. It'll be again()ed by the repeat_1 so
  90. * it should not time out until repeat_1 stops.
  91. */
  92. r = uv_timer_init(uv_default_loop(), &repeat_2);
  93. ASSERT(r == 0);
  94. r = uv_timer_start(&repeat_2, repeat_2_cb, 100, 100);
  95. ASSERT(r == 0);
  96. ASSERT(uv_timer_get_repeat(&repeat_2) == 100);
  97. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  98. ASSERT(repeat_1_cb_called == 10);
  99. ASSERT(repeat_2_cb_called == 2);
  100. ASSERT(close_cb_called == 2);
  101. LOGF("Test took %ld ms (expected ~700 ms)\n",
  102. (long int)(uv_now(uv_default_loop()) - start_time));
  103. MAKE_VALGRIND_HAPPY();
  104. return 0;
  105. }