test-timer-from-check.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 uv_prepare_t prepare_handle;
  24. static uv_check_t check_handle;
  25. static uv_timer_t timer_handle;
  26. static int prepare_cb_called;
  27. static int check_cb_called;
  28. static int timer_cb_called;
  29. static void prepare_cb(uv_prepare_t* handle, int status) {
  30. ASSERT(0 == uv_prepare_stop(&prepare_handle));
  31. ASSERT(0 == prepare_cb_called);
  32. ASSERT(1 == check_cb_called);
  33. ASSERT(0 == timer_cb_called);
  34. prepare_cb_called++;
  35. }
  36. static void timer_cb(uv_timer_t* handle, int status) {
  37. ASSERT(0 == uv_timer_stop(&timer_handle));
  38. ASSERT(1 == prepare_cb_called);
  39. ASSERT(1 == check_cb_called);
  40. ASSERT(0 == timer_cb_called);
  41. timer_cb_called++;
  42. }
  43. static void check_cb(uv_check_t* handle, int status) {
  44. ASSERT(0 == uv_check_stop(&check_handle));
  45. ASSERT(0 == uv_timer_stop(&timer_handle)); /* Runs before timer_cb. */
  46. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0));
  47. ASSERT(0 == uv_prepare_start(&prepare_handle, prepare_cb));
  48. ASSERT(0 == prepare_cb_called);
  49. ASSERT(0 == check_cb_called);
  50. ASSERT(0 == timer_cb_called);
  51. check_cb_called++;
  52. }
  53. TEST_IMPL(timer_from_check) {
  54. ASSERT(0 == uv_prepare_init(uv_default_loop(), &prepare_handle));
  55. ASSERT(0 == uv_check_init(uv_default_loop(), &check_handle));
  56. ASSERT(0 == uv_check_start(&check_handle, check_cb));
  57. ASSERT(0 == uv_timer_init(uv_default_loop(), &timer_handle));
  58. ASSERT(0 == uv_timer_start(&timer_handle, timer_cb, 50, 0));
  59. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  60. ASSERT(1 == prepare_cb_called);
  61. ASSERT(1 == check_cb_called);
  62. ASSERT(1 == timer_cb_called);
  63. uv_close((uv_handle_t*) &prepare_handle, NULL);
  64. uv_close((uv_handle_t*) &check_handle, NULL);
  65. uv_close((uv_handle_t*) &timer_handle, NULL);
  66. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_ONCE));
  67. MAKE_VALGRIND_HAPPY();
  68. return 0;
  69. }