test-connection-fail.c
Go to the documentation of this file.
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 
25 #include <stdlib.h>
26 #include <stdio.h>
27 
28 
29 static uv_tcp_t tcp;
31 static int connect_cb_calls;
32 static int close_cb_calls;
33 
36 static int timer_cb_calls;
37 
38 
39 static void on_close(uv_handle_t* handle) {
41 }
42 
43 
46 }
47 
48 
49 static void timer_cb(uv_timer_t* handle) {
51 
52  /*
53  * These are the important asserts. The connection callback has been made,
54  * but libuv hasn't automatically closed the socket. The user must
55  * uv_close the handle manually.
56  */
57  ASSERT(close_cb_calls == 0);
59 
60  /* Close the tcp handle. */
62 
63  /* Close the timer. */
65 }
66 
67 
69  ASSERT((uv_stream_t*) &tcp == req->handle);
70  ASSERT(status == UV_ECONNREFUSED);
72 
73  ASSERT(close_cb_calls == 0);
75 }
76 
77 
79  ASSERT(status == UV_ECONNREFUSED);
81 
82  uv_timer_start(&timer, timer_cb, 100, 0);
83 
84  ASSERT(close_cb_calls == 0);
85 }
86 
87 
89  struct sockaddr_in client_addr, server_addr;
90  int r;
91 
92  ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &client_addr));
93 
94  /* There should be no servers listening on this port. */
95  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
96 
97  /* Try to connect to the server and do NUM_PINGS ping-pongs. */
99  ASSERT(!r);
100 
101  /* We are never doing multiple reads/connects at a time anyway. so these
102  * handles can be pre-initialized. */
103  ASSERT(0 == uv_tcp_bind(&tcp, (const struct sockaddr*) &client_addr, 0));
104 
105  r = uv_tcp_connect(&req,
106  &tcp,
107  (const struct sockaddr*) &server_addr,
108  connect_cb);
109  ASSERT(!r);
110 
112 
113  ASSERT(connect_cb_calls == 1);
114  ASSERT(close_cb_calls == 1);
115 }
116 
117 
118 /*
119  * This test attempts to connect to a port where no server is running. We
120  * expect an error.
121  */
124 
126  ASSERT(timer_cb_calls == 0);
127 
129  return 0;
130 }
131 
132 
133 /*
134  * This test is the same as the first except it check that the close
135  * callback of the tcp handle hasn't been made after the failed connection
136  * attempt.
137  */
138 TEST_IMPL(connection_fail_doesnt_auto_close) {
139  int r;
140 
142  ASSERT(r == 0);
143 
145 
147  ASSERT(timer_cb_calls == 1);
148 
150  return 0;
151 }
connect_cb
static void connect_cb(uv_connect_t *conn_req, int status)
Definition: benchmark-pound.c:103
connect_cb_calls
static int connect_cb_calls
Definition: test-connection-fail.c:31
task.h
uv_connect_s
Definition: uv.h:580
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
ASSERT
#define ASSERT(expr)
Definition: task.h:102
tcp
static uv_tcp_t tcp
Definition: test-connection-fail.c:29
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
on_connect_with_close
static void on_connect_with_close(uv_connect_t *req, int status)
Definition: test-connection-fail.c:68
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
connection_fail
static void connection_fail(uv_connect_cb connect_cb)
Definition: test-connection-fail.c:88
timer_cb_calls
static int timer_cb_calls
Definition: test-connection-fail.c:36
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
on_close
static void on_close(uv_handle_t *handle)
Definition: test-connection-fail.c:39
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
timer_cb
static void timer_cb(uv_timer_t *handle)
Definition: test-connection-fail.c:49
TEST_IMPL
TEST_IMPL(connection_fail)
Definition: test-connection-fail.c:122
uv_timer_s
Definition: uv.h:850
on_connect_without_close
static void on_connect_without_close(uv_connect_t *req, int status)
Definition: test-connection-fail.c:78
uv_tcp_s
Definition: uv.h:544
uv_connect_cb
void(* uv_connect_cb)(uv_connect_t *req, int status)
Definition: uv.h:313
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
timer
static uv_timer_t timer
Definition: test-connection-fail.c:34
fix_build_deps.r
r
Definition: fix_build_deps.py:491
close_cb_calls
static int close_cb_calls
Definition: test-connection-fail.c:32
timer_close_cb
static void timer_close_cb(uv_handle_t *handle)
Definition: test-connection-fail.c:44
timer_close_cb_calls
static int timer_close_cb_calls
Definition: test-connection-fail.c:35
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_timer_start
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:29