test-udp-send-hang-loop.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright The libuv project and 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 <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #define CHECK_OBJECT(handle, type, parent) \
  27. ASSERT((type*)(handle) == &(parent))
  28. static uv_udp_t client;
  29. static uv_idle_t idle_handle;
  30. static uv_udp_send_t send_req;
  31. static uv_buf_t buf;
  32. static struct sockaddr_in addr;
  33. static char send_data[1024];
  34. static int loop_hang_called;
  35. static void send_cb(uv_udp_send_t* req, int status);
  36. static void idle_cb(uv_idle_t* handle) {
  37. int r;
  38. ASSERT(send_req.handle == NULL);
  39. CHECK_OBJECT(handle, uv_idle_t, idle_handle);
  40. ASSERT(0 == uv_idle_stop(handle));
  41. /* It probably would have stalled by now if it's going to stall at all. */
  42. if (++loop_hang_called > 1000) {
  43. uv_close((uv_handle_t*) &client, NULL);
  44. uv_close((uv_handle_t*) &idle_handle, NULL);
  45. return;
  46. }
  47. r = uv_udp_send(&send_req,
  48. &client,
  49. &buf,
  50. 1,
  51. (const struct sockaddr*) &addr,
  52. send_cb);
  53. ASSERT(r == 0);
  54. }
  55. static void send_cb(uv_udp_send_t* req, int status) {
  56. ASSERT(req != NULL);
  57. ASSERT(status == 0 || status == UV_ENETUNREACH);
  58. CHECK_OBJECT(req->handle, uv_udp_t, client);
  59. CHECK_OBJECT(req, uv_udp_send_t, send_req);
  60. req->handle = NULL;
  61. ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
  62. }
  63. TEST_IMPL(udp_send_hang_loop) {
  64. ASSERT(0 == uv_idle_init(uv_default_loop(), &idle_handle));
  65. /* 192.0.2.0/8 is "TEST-NET" and reserved for documentation.
  66. * Good for us, though. Since we want to have something unreachable.
  67. */
  68. ASSERT(0 == uv_ip4_addr("192.0.2.3", TEST_PORT, &addr));
  69. ASSERT(0 == uv_udp_init(uv_default_loop(), &client));
  70. buf = uv_buf_init(send_data, sizeof(send_data));
  71. ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
  72. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  73. ASSERT(loop_hang_called > 1000);
  74. MAKE_VALGRIND_HAPPY();
  75. return 0;
  76. }