1
0

test-signal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /* This test does not pretend to be cross-platform. */
  22. #ifndef _WIN32
  23. #include "uv.h"
  24. #include "task.h"
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #define NSIGNALS 10
  33. struct timer_ctx {
  34. unsigned int ncalls;
  35. uv_timer_t handle;
  36. int signum;
  37. };
  38. struct signal_ctx {
  39. enum { CLOSE, STOP } stop_or_close;
  40. unsigned int ncalls;
  41. uv_signal_t handle;
  42. int signum;
  43. };
  44. static void signal_cb(uv_signal_t* handle, int signum) {
  45. struct signal_ctx* ctx = container_of(handle, struct signal_ctx, handle);
  46. ASSERT(signum == ctx->signum);
  47. if (++ctx->ncalls == NSIGNALS) {
  48. if (ctx->stop_or_close == STOP)
  49. uv_signal_stop(handle);
  50. else if (ctx->stop_or_close == CLOSE)
  51. uv_close((uv_handle_t*)handle, NULL);
  52. else
  53. ASSERT(0);
  54. }
  55. }
  56. static void timer_cb(uv_timer_t* handle, int status) {
  57. struct timer_ctx* ctx = container_of(handle, struct timer_ctx, handle);
  58. raise(ctx->signum);
  59. if (++ctx->ncalls == NSIGNALS)
  60. uv_close((uv_handle_t*)handle, NULL);
  61. }
  62. static void start_watcher(uv_loop_t* loop, int signum, struct signal_ctx* ctx) {
  63. ctx->ncalls = 0;
  64. ctx->signum = signum;
  65. ctx->stop_or_close = CLOSE;
  66. ASSERT(0 == uv_signal_init(loop, &ctx->handle));
  67. ASSERT(0 == uv_signal_start(&ctx->handle, signal_cb, signum));
  68. }
  69. static void start_timer(uv_loop_t* loop, int signum, struct timer_ctx* ctx) {
  70. ctx->ncalls = 0;
  71. ctx->signum = signum;
  72. ASSERT(0 == uv_timer_init(loop, &ctx->handle));
  73. ASSERT(0 == uv_timer_start(&ctx->handle, timer_cb, 5, 5));
  74. }
  75. TEST_IMPL(we_get_signal) {
  76. struct signal_ctx sc;
  77. struct timer_ctx tc;
  78. uv_loop_t* loop;
  79. loop = uv_default_loop();
  80. start_timer(loop, SIGCHLD, &tc);
  81. start_watcher(loop, SIGCHLD, &sc);
  82. sc.stop_or_close = STOP; /* stop, don't close the signal handle */
  83. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  84. ASSERT(tc.ncalls == NSIGNALS);
  85. ASSERT(sc.ncalls == NSIGNALS);
  86. start_timer(loop, SIGCHLD, &tc);
  87. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  88. ASSERT(tc.ncalls == NSIGNALS);
  89. ASSERT(sc.ncalls == NSIGNALS);
  90. sc.ncalls = 0;
  91. sc.stop_or_close = CLOSE; /* now close it when it's done */
  92. uv_signal_start(&sc.handle, signal_cb, SIGCHLD);
  93. start_timer(loop, SIGCHLD, &tc);
  94. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  95. ASSERT(tc.ncalls == NSIGNALS);
  96. ASSERT(sc.ncalls == NSIGNALS);
  97. MAKE_VALGRIND_HAPPY();
  98. return 0;
  99. }
  100. TEST_IMPL(we_get_signals) {
  101. struct signal_ctx sc[4];
  102. struct timer_ctx tc[2];
  103. uv_loop_t* loop;
  104. unsigned int i;
  105. loop = uv_default_loop();
  106. start_watcher(loop, SIGUSR1, sc + 0);
  107. start_watcher(loop, SIGUSR1, sc + 1);
  108. start_watcher(loop, SIGUSR2, sc + 2);
  109. start_watcher(loop, SIGUSR2, sc + 3);
  110. start_timer(loop, SIGUSR1, tc + 0);
  111. start_timer(loop, SIGUSR2, tc + 1);
  112. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  113. for (i = 0; i < ARRAY_SIZE(sc); i++)
  114. ASSERT(sc[i].ncalls == NSIGNALS);
  115. for (i = 0; i < ARRAY_SIZE(tc); i++)
  116. ASSERT(tc[i].ncalls == NSIGNALS);
  117. MAKE_VALGRIND_HAPPY();
  118. return 0;
  119. }
  120. #endif /* _WIN32 */