1
0

test-idle.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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_idle_t idle_handle;
  24. static uv_check_t check_handle;
  25. static uv_timer_t timer_handle;
  26. static int idle_cb_called = 0;
  27. static int check_cb_called = 0;
  28. static int timer_cb_called = 0;
  29. static int close_cb_called = 0;
  30. static void close_cb(uv_handle_t* handle) {
  31. close_cb_called++;
  32. }
  33. static void timer_cb(uv_timer_t* handle, int status) {
  34. ASSERT(handle == &timer_handle);
  35. ASSERT(status == 0);
  36. uv_close((uv_handle_t*) &idle_handle, close_cb);
  37. uv_close((uv_handle_t*) &check_handle, close_cb);
  38. uv_close((uv_handle_t*) &timer_handle, close_cb);
  39. timer_cb_called++;
  40. LOGF("timer_cb %d\n", timer_cb_called);
  41. }
  42. static void idle_cb(uv_idle_t* handle, int status) {
  43. ASSERT(handle == &idle_handle);
  44. ASSERT(status == 0);
  45. idle_cb_called++;
  46. LOGF("idle_cb %d\n", idle_cb_called);
  47. }
  48. static void check_cb(uv_check_t* handle, int status) {
  49. ASSERT(handle == &check_handle);
  50. ASSERT(status == 0);
  51. check_cb_called++;
  52. LOGF("check_cb %d\n", check_cb_called);
  53. }
  54. TEST_IMPL(idle_starvation) {
  55. int r;
  56. r = uv_idle_init(uv_default_loop(), &idle_handle);
  57. ASSERT(r == 0);
  58. r = uv_idle_start(&idle_handle, idle_cb);
  59. ASSERT(r == 0);
  60. r = uv_check_init(uv_default_loop(), &check_handle);
  61. ASSERT(r == 0);
  62. r = uv_check_start(&check_handle, check_cb);
  63. ASSERT(r == 0);
  64. r = uv_timer_init(uv_default_loop(), &timer_handle);
  65. ASSERT(r == 0);
  66. r = uv_timer_start(&timer_handle, timer_cb, 50, 0);
  67. ASSERT(r == 0);
  68. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  69. ASSERT(r == 0);
  70. ASSERT(idle_cb_called > 0);
  71. ASSERT(timer_cb_called == 1);
  72. ASSERT(close_cb_called == 3);
  73. MAKE_VALGRIND_HAPPY();
  74. return 0;
  75. }