xref: /netbsd-src/external/mit/libuv/dist/test/test-tcp-connect-timeout.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
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 
22 #include "uv.h"
23 #include "task.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 
27 
28 static int connect_cb_called;
29 static int close_cb_called;
30 
31 static uv_connect_t connect_req;
32 static uv_timer_t timer;
33 static uv_tcp_t conn;
34 
35 static void connect_cb(uv_connect_t* req, int status);
36 static void timer_cb(uv_timer_t* handle);
37 static void close_cb(uv_handle_t* handle);
38 
39 
40 static void connect_cb(uv_connect_t* req, int status) {
41   ASSERT(req == &connect_req);
42   ASSERT(status == UV_ECANCELED);
43   connect_cb_called++;
44 }
45 
46 
47 static void timer_cb(uv_timer_t* handle) {
48   ASSERT(handle == &timer);
49   uv_close((uv_handle_t*)&conn, close_cb);
50   uv_close((uv_handle_t*)&timer, close_cb);
51 }
52 
53 
54 static void close_cb(uv_handle_t* handle) {
55   ASSERT(handle == (uv_handle_t*)&conn || handle == (uv_handle_t*)&timer);
56   close_cb_called++;
57 }
58 
59 
60 /* Verify that connecting to an unreachable address or port doesn't hang
61  * the event loop.
62  */
63 TEST_IMPL(tcp_connect_timeout) {
64   struct sockaddr_in addr;
65   int r;
66 
67   ASSERT(0 == uv_ip4_addr("8.8.8.8", 9999, &addr));
68 
69   r = uv_timer_init(uv_default_loop(), &timer);
70   ASSERT(r == 0);
71 
72   r = uv_timer_start(&timer, timer_cb, 50, 0);
73   ASSERT(r == 0);
74 
75   r = uv_tcp_init(uv_default_loop(), &conn);
76   ASSERT(r == 0);
77 
78   r = uv_tcp_connect(&connect_req,
79                      &conn,
80                      (const struct sockaddr*) &addr,
81                      connect_cb);
82   if (r == UV_ENETUNREACH)
83     RETURN_SKIP("Network unreachable.");
84   ASSERT(r == 0);
85 
86   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
87   ASSERT(r == 0);
88 
89   MAKE_VALGRIND_HAPPY();
90   return 0;
91 }
92