test-callback-stack.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 /*
23  * TODO: Add explanation of why we want on_close to be called from fresh
24  * stack.
25  */
26 
27 #include "uv.h"
28 #include "task.h"
29 
30 
31 static const char MESSAGE[] = "Failure is for the weak. Everyone dies alone.";
32 
38 
39 static int nested = 0;
40 static int close_cb_called = 0;
41 static int connect_cb_called = 0;
42 static int write_cb_called = 0;
43 static int timer_cb_called = 0;
44 static int bytes_received = 0;
45 static int shutdown_cb_called = 0;
46 
47 
48 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
49  buf->len = size;
50  buf->base = malloc(size);
51  ASSERT(buf->base != NULL);
52 }
53 
54 
55 static void close_cb(uv_handle_t* handle) {
56  ASSERT(nested == 0 && "close_cb must be called from a fresh stack");
57 
59 }
60 
61 
62 static void shutdown_cb(uv_shutdown_t* req, int status) {
63  ASSERT(status == 0);
64  ASSERT(nested == 0 && "shutdown_cb must be called from a fresh stack");
65 
67 }
68 
69 
70 static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
71  ASSERT(nested == 0 && "read_cb must be called from a fresh stack");
72 
73  printf("Read. nread == %d\n", (int)nread);
74  free(buf->base);
75 
76  if (nread == 0) {
77  return;
78 
79  } else if (nread < 0) {
80  ASSERT(nread == UV_EOF);
81 
82  nested++;
84  nested--;
85 
86  return;
87  }
88 
89  bytes_received += nread;
90 
91  /* We call shutdown here because when bytes_received == sizeof MESSAGE there
92  * will be no more data sent nor received, so here it would be possible for a
93  * backend to call shutdown_cb immediately and *not* from a fresh stack. */
94  if (bytes_received == sizeof MESSAGE) {
95  nested++;
96 
97  puts("Shutdown");
98 
100  FATAL("uv_shutdown failed");
101  }
102  nested--;
103  }
104 }
105 
106 
107 static void timer_cb(uv_timer_t* handle) {
108  ASSERT(handle == &timer);
109  ASSERT(nested == 0 && "timer_cb must be called from a fresh stack");
110 
111  puts("Timeout complete. Now read data...");
112 
113  nested++;
115  FATAL("uv_read_start failed");
116  }
117  nested--;
118 
119  timer_cb_called++;
120 
122 }
123 
124 
125 static void write_cb(uv_write_t* req, int status) {
126  int r;
127 
128  ASSERT(status == 0);
129  ASSERT(nested == 0 && "write_cb must be called from a fresh stack");
130 
131  puts("Data written. 500ms timeout...");
132 
133  /* After the data has been sent, we're going to wait for a while, then start
134  * reading. This makes us certain that the message has been echoed back to
135  * our receive buffer when we start reading. This maximizes the temptation
136  * for the backend to use dirty stack for calling read_cb. */
137  nested++;
139  ASSERT(r == 0);
140  r = uv_timer_start(&timer, timer_cb, 500, 0);
141  ASSERT(r == 0);
142  nested--;
143 
144  write_cb_called++;
145 }
146 
147 
148 static void connect_cb(uv_connect_t* req, int status) {
149  uv_buf_t buf;
150 
151  puts("Connected. Write some data to echo server...");
152 
153  ASSERT(status == 0);
154  ASSERT(nested == 0 && "connect_cb must be called from a fresh stack");
155 
156  nested++;
157 
158  buf.base = (char*) &MESSAGE;
159  buf.len = sizeof MESSAGE;
160 
161  if (uv_write(&write_req, (uv_stream_t*)req->handle, &buf, 1, write_cb)) {
162  FATAL("uv_write failed");
163  }
164 
165  nested--;
166 
168 }
169 
170 
171 TEST_IMPL(callback_stack) {
172  struct sockaddr_in addr;
173 
174  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
175 
177  FATAL("uv_tcp_init failed");
178  }
179 
180  puts("Connecting...");
181 
182  nested++;
183 
185  &client,
186  (const struct sockaddr*) &addr,
187  connect_cb)) {
188  FATAL("uv_tcp_connect failed");
189  }
190  nested--;
191 
193 
194  ASSERT(nested == 0);
195  ASSERT(connect_cb_called == 1 && "connect_cb must be called exactly once");
196  ASSERT(write_cb_called == 1 && "write_cb must be called exactly once");
197  ASSERT(timer_cb_called == 1 && "timer_cb must be called exactly once");
198  ASSERT(bytes_received == sizeof MESSAGE);
199  ASSERT(shutdown_cb_called == 1 && "shutdown_cb must be called exactly once");
200  ASSERT(close_cb_called == 2 && "close_cb must be called exactly twice");
201 
203  return 0;
204 }
MESSAGE
static const char MESSAGE[]
Definition: test-callback-stack.c:31
task.h
uv_connect_s
Definition: uv.h:580
connect_req
static uv_connect_t connect_req
Definition: test-callback-stack.c:35
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-callback-stack.c:48
uv_shutdown_s
Definition: uv.h:417
client
Definition: examples/python/async_streaming/client.py:1
connect_cb_called
static int connect_cb_called
Definition: test-callback-stack.c:41
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
read_cb
static void read_cb(uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
Definition: test-callback-stack.c:70
bytes_received
static int bytes_received
Definition: test-callback-stack.c:44
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
write_req
Definition: benchmark-tcp-write-batch.c:31
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-callback-stack.c:148
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
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
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
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
req
static uv_connect_t req
Definition: test-connection-fail.c:30
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
close_cb_called
static int close_cb_called
Definition: test-callback-stack.c:40
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
write_req
static uv_write_t write_req
Definition: test-callback-stack.c:36
write_cb_called
static int write_cb_called
Definition: test-callback-stack.c:42
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
timer_cb_called
static int timer_cb_called
Definition: test-callback-stack.c:43
uv_tcp_s
Definition: uv.h:544
nested
static int nested
Definition: test-callback-stack.c:39
write_cb
static void write_cb(uv_write_t *req, int status)
Definition: test-callback-stack.c:125
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
FATAL
#define FATAL(msg)
Definition: task.h:88
uv_buf_t
Definition: unix.h:121
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: test-callback-stack.c:62
fix_build_deps.r
r
Definition: fix_build_deps.py:491
uv_write_s
Definition: uv.h:522
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
shutdown_cb_called
static int shutdown_cb_called
Definition: test-callback-stack.c:45
timer_cb
static void timer_cb(uv_timer_t *handle)
Definition: test-callback-stack.c:107
uv_timer_init
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
shutdown_req
static uv_shutdown_t shutdown_req
Definition: test-callback-stack.c:37
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-callback-stack.c:55
timer
static uv_timer_t timer
Definition: test-callback-stack.c:34
TEST_IMPL
TEST_IMPL(callback_stack)
Definition: test-callback-stack.c:171


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