xref: /netbsd-src/external/mit/libuv/dist/test/test-udp-send-hang-loop.c (revision 5f2f42719cd62ff11fd913b40b7ce19f07c4fd25)
10e552da7Schristos /* Copyright The libuv project and contributors. All rights reserved.
20e552da7Schristos  *
30e552da7Schristos  * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos  * of this software and associated documentation files (the "Software"), to
50e552da7Schristos  * deal in the Software without restriction, including without limitation the
60e552da7Schristos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos  * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos  * furnished to do so, subject to the following conditions:
90e552da7Schristos  *
100e552da7Schristos  * The above copyright notice and this permission notice shall be included in
110e552da7Schristos  * all copies or substantial portions of the Software.
120e552da7Schristos  *
130e552da7Schristos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos  * IN THE SOFTWARE.
200e552da7Schristos  */
210e552da7Schristos 
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos 
250e552da7Schristos #include <stdio.h>
260e552da7Schristos #include <stdlib.h>
270e552da7Schristos #include <string.h>
280e552da7Schristos 
290e552da7Schristos #define CHECK_OBJECT(handle, type, parent) \
300e552da7Schristos   ASSERT((type*)(handle) == &(parent))
310e552da7Schristos 
320e552da7Schristos static uv_udp_t client;
330e552da7Schristos static uv_idle_t idle_handle;
340e552da7Schristos static uv_udp_send_t send_req;
350e552da7Schristos static uv_buf_t buf;
360e552da7Schristos static struct sockaddr_in addr;
370e552da7Schristos static char send_data[1024];
380e552da7Schristos 
390e552da7Schristos static int loop_hang_called;
400e552da7Schristos 
410e552da7Schristos static void send_cb(uv_udp_send_t* req, int status);
420e552da7Schristos 
430e552da7Schristos 
idle_cb(uv_idle_t * handle)440e552da7Schristos static void idle_cb(uv_idle_t* handle) {
450e552da7Schristos   int r;
460e552da7Schristos 
47*5f2f4271Schristos   ASSERT_NULL(send_req.handle);
480e552da7Schristos   CHECK_OBJECT(handle, uv_idle_t, idle_handle);
490e552da7Schristos   ASSERT(0 == uv_idle_stop(handle));
500e552da7Schristos 
510e552da7Schristos   /* It probably would have stalled by now if it's going to stall at all. */
520e552da7Schristos   if (++loop_hang_called > 1000) {
530e552da7Schristos     uv_close((uv_handle_t*) &client, NULL);
540e552da7Schristos     uv_close((uv_handle_t*) &idle_handle, NULL);
550e552da7Schristos     return;
560e552da7Schristos   }
570e552da7Schristos 
580e552da7Schristos   r = uv_udp_send(&send_req,
590e552da7Schristos                   &client,
600e552da7Schristos                   &buf,
610e552da7Schristos                   1,
620e552da7Schristos                   (const struct sockaddr*) &addr,
630e552da7Schristos                   send_cb);
640e552da7Schristos   ASSERT(r == 0);
650e552da7Schristos }
660e552da7Schristos 
670e552da7Schristos 
send_cb(uv_udp_send_t * req,int status)680e552da7Schristos static void send_cb(uv_udp_send_t* req, int status) {
69*5f2f4271Schristos   ASSERT_NOT_NULL(req);
700e552da7Schristos   ASSERT(status == 0 || status == UV_ENETUNREACH);
710e552da7Schristos   CHECK_OBJECT(req->handle, uv_udp_t, client);
720e552da7Schristos   CHECK_OBJECT(req, uv_udp_send_t, send_req);
730e552da7Schristos   req->handle = NULL;
740e552da7Schristos 
750e552da7Schristos   ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
760e552da7Schristos }
770e552da7Schristos 
780e552da7Schristos 
TEST_IMPL(udp_send_hang_loop)790e552da7Schristos TEST_IMPL(udp_send_hang_loop) {
800e552da7Schristos   ASSERT(0 == uv_idle_init(uv_default_loop(), &idle_handle));
810e552da7Schristos 
820e552da7Schristos   /* 192.0.2.0/8 is "TEST-NET" and reserved for documentation.
830e552da7Schristos    * Good for us, though. Since we want to have something unreachable.
840e552da7Schristos    */
850e552da7Schristos   ASSERT(0 == uv_ip4_addr("192.0.2.3", TEST_PORT, &addr));
860e552da7Schristos 
870e552da7Schristos   ASSERT(0 == uv_udp_init(uv_default_loop(), &client));
880e552da7Schristos 
890e552da7Schristos   buf = uv_buf_init(send_data, sizeof(send_data));
900e552da7Schristos 
910e552da7Schristos   ASSERT(0 == uv_idle_start(&idle_handle, idle_cb));
920e552da7Schristos 
930e552da7Schristos   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
940e552da7Schristos 
950e552da7Schristos   ASSERT(loop_hang_called > 1000);
960e552da7Schristos 
970e552da7Schristos   MAKE_VALGRIND_HAPPY();
980e552da7Schristos   return 0;
990e552da7Schristos }
100