1
0

test-watcher-cross-stop.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <string.h>
  24. #include <errno.h>
  25. /* NOTE: Number should be big enough to trigger this problem */
  26. static uv_udp_t sockets[2500];
  27. static uv_udp_send_t reqs[ARRAY_SIZE(sockets)];
  28. static char slab[1];
  29. static unsigned int recv_cb_called;
  30. static unsigned int send_cb_called;
  31. static unsigned int close_cb_called;
  32. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  33. buf->base = slab;
  34. buf->len = sizeof(slab);
  35. }
  36. static void recv_cb(uv_udp_t* handle,
  37. ssize_t nread,
  38. const uv_buf_t* buf,
  39. const struct sockaddr* addr,
  40. unsigned flags) {
  41. recv_cb_called++;
  42. }
  43. static void send_cb(uv_udp_send_t* req, int status) {
  44. send_cb_called++;
  45. }
  46. static void close_cb(uv_handle_t* handle) {
  47. close_cb_called++;
  48. }
  49. TEST_IMPL(watcher_cross_stop) {
  50. uv_loop_t* loop = uv_default_loop();
  51. unsigned int i;
  52. struct sockaddr_in addr;
  53. uv_buf_t buf;
  54. char big_string[1024];
  55. TEST_FILE_LIMIT(ARRAY_SIZE(sockets) + 32);
  56. ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
  57. memset(big_string, 'A', sizeof(big_string));
  58. buf = uv_buf_init(big_string, sizeof(big_string));
  59. for (i = 0; i < ARRAY_SIZE(sockets); i++) {
  60. ASSERT(0 == uv_udp_init(loop, &sockets[i]));
  61. ASSERT(0 == uv_udp_bind(&sockets[i], (const struct sockaddr*) &addr, 0));
  62. ASSERT(0 == uv_udp_recv_start(&sockets[i], alloc_cb, recv_cb));
  63. ASSERT(0 == uv_udp_send(&reqs[i],
  64. &sockets[i],
  65. &buf,
  66. 1,
  67. (const struct sockaddr*) &addr,
  68. send_cb));
  69. }
  70. while (recv_cb_called == 0)
  71. uv_run(loop, UV_RUN_ONCE);
  72. for (i = 0; i < ARRAY_SIZE(sockets); i++)
  73. uv_close((uv_handle_t*) &sockets[i], close_cb);
  74. ASSERT(0 < recv_cb_called && recv_cb_called <= ARRAY_SIZE(sockets));
  75. ASSERT(ARRAY_SIZE(sockets) == send_cb_called);
  76. uv_run(loop, UV_RUN_DEFAULT);
  77. ASSERT(ARRAY_SIZE(sockets) == close_cb_called);
  78. MAKE_VALGRIND_HAPPY();
  79. return 0;
  80. }