test-shutdown-eof.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 #include <stdio.h>
25 #include <stdlib.h>
26 
28 static uv_tcp_t tcp;
32 static uv_buf_t qbuf;
33 static int got_q;
34 static int got_eof;
35 static int called_connect_cb;
36 static int called_shutdown_cb;
39 static int called_timer_cb;
40 
41 
42 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
43  buf->base = malloc(size);
44  buf->len = size;
45 }
46 
47 
48 static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) {
49  ASSERT((uv_tcp_t*)t == &tcp);
50 
51  if (nread == 0) {
52  free(buf->base);
53  return;
54  }
55 
56  if (!got_q) {
57  ASSERT(nread == 1);
58  ASSERT(!got_eof);
59  ASSERT(buf->base[0] == 'Q');
60  free(buf->base);
61  got_q = 1;
62  puts("got Q");
63  } else {
64  ASSERT(nread == UV_EOF);
65  if (buf->base) {
66  free(buf->base);
67  }
68  got_eof = 1;
69  puts("got EOF");
70  }
71 }
72 
73 
74 static void shutdown_cb(uv_shutdown_t *req, int status) {
75  ASSERT(req == &shutdown_req);
76 
78  ASSERT(!got_eof);
81  ASSERT(called_timer_cb == 0);
82 
84 }
85 
86 
87 static void connect_cb(uv_connect_t *req, int status) {
88  ASSERT(status == 0);
89  ASSERT(req == &connect_req);
90 
91  /* Start reading from our connection so we can receive the EOF. */
93 
94  /*
95  * Write the letter 'Q' to gracefully kill the echo-server. This will not
96  * effect our connection.
97  */
98  uv_write(&write_req, (uv_stream_t*) &tcp, &qbuf, 1, NULL);
99 
100  /* Shutdown our end of the connection. */
102 
105 }
106 
107 
109  ASSERT(handle == (uv_handle_t*) &tcp);
110 
112  ASSERT(got_q);
113  ASSERT(got_eof);
114  ASSERT(called_timer_cb == 1);
115 
117 }
118 
119 
121  ASSERT(handle == (uv_handle_t*) &timer);
123 }
124 
125 
126 static void timer_cb(uv_timer_t* handle) {
127  ASSERT(handle == &timer);
129 
130  /*
131  * The most important assert of the test: we have not received
132  * tcp_close_cb yet.
133  */
136 
137  called_timer_cb++;
138 }
139 
140 
141 /*
142  * This test has a client which connects to the echo_server and immediately
143  * issues a shutdown. The echo-server, in response, will also shutdown their
144  * connection. We check, with a timer, that libuv is not automatically
145  * calling uv_close when the client receives the EOF from echo-server.
146  */
147 TEST_IMPL(shutdown_eof) {
148  struct sockaddr_in server_addr;
149  int r;
150 
151  qbuf.base = "Q";
152  qbuf.len = 1;
153 
155  ASSERT(r == 0);
156 
157  uv_timer_start(&timer, timer_cb, 100, 0);
158 
159  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
161  ASSERT(!r);
162 
164  &tcp,
165  (const struct sockaddr*) &server_addr,
166  connect_cb);
167  ASSERT(!r);
168 
170 
173  ASSERT(got_eof);
174  ASSERT(got_q);
177  ASSERT(called_timer_cb == 1);
178 
180  return 0;
181 }
182 
task.h
uv_connect_s
Definition: uv.h:580
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-shutdown-eof.c:42
uv_shutdown_s
Definition: uv.h:417
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
tcp_close_cb
static void tcp_close_cb(uv_handle_t *handle)
Definition: test-shutdown-eof.c:108
called_tcp_close_cb
static int called_tcp_close_cb
Definition: test-shutdown-eof.c:37
write_req
Definition: benchmark-tcp-write-batch.c:31
write_req
static uv_write_t write_req
Definition: test-shutdown-eof.c:30
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
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
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
got_eof
static int got_eof
Definition: test-shutdown-eof.c:34
ssize_t
intptr_t ssize_t
Definition: win.h:27
uv_write
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: unix/stream.c:1492
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-shutdown-eof.c:87
req
static uv_connect_t req
Definition: test-connection-fail.c:30
qbuf
static uv_buf_t qbuf
Definition: test-shutdown-eof.c:32
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_shutdown
UV_PRIVATE_REQ_TYPES UV_EXTERN int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb)
Definition: unix/stream.c:1259
tcp
static uv_tcp_t tcp
Definition: test-shutdown-eof.c:28
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: test-shutdown-eof.c:74
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
called_timer_close_cb
static int called_timer_close_cb
Definition: test-shutdown-eof.c:38
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_timer_s
Definition: uv.h:850
called_shutdown_cb
static int called_shutdown_cb
Definition: test-shutdown-eof.c:36
called_connect_cb
static int called_connect_cb
Definition: test-shutdown-eof.c:35
uv_tcp_s
Definition: uv.h:544
uv_buf_t::base
char * base
Definition: unix.h:122
connect_req
static uv_connect_t connect_req
Definition: test-shutdown-eof.c:29
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
TEST_IMPL
TEST_IMPL(shutdown_eof)
Definition: test-shutdown-eof.c:147
shutdown_req
static uv_shutdown_t shutdown_req
Definition: test-shutdown-eof.c:31
fix_build_deps.r
r
Definition: fix_build_deps.py:491
got_q
static int got_q
Definition: test-shutdown-eof.c:33
timer
static uv_timer_t timer
Definition: test-shutdown-eof.c:27
read_cb
static void read_cb(uv_stream_t *t, ssize_t nread, const uv_buf_t *buf)
Definition: test-shutdown-eof.c:48
uv_buf_t::len
size_t len
Definition: unix.h:123
uv_write_s
Definition: uv.h:522
called_timer_cb
static int called_timer_cb
Definition: test-shutdown-eof.c:39
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
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
timer_cb
static void timer_cb(uv_timer_t *handle)
Definition: test-shutdown-eof.c:126
timer_close_cb
static void timer_close_cb(uv_handle_t *handle)
Definition: test-shutdown-eof.c:120


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:27