xref: /libuv/test/test-tcp-writealot.c (revision f01219df)
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 #define WRITES            3
29 #if defined(__arm__) /* Decrease the chunks so the test passes on arm CI bots */
30 #define CHUNKS_PER_WRITE  2048
31 #else
32 #define CHUNKS_PER_WRITE  4096
33 #endif
34 #define CHUNK_SIZE        10024 /* 10 kb */
35 
36 #define TOTAL_BYTES       (WRITES * CHUNKS_PER_WRITE * CHUNK_SIZE)
37 
38 static char* send_buffer;
39 
40 static int shutdown_cb_called = 0;
41 static int connect_cb_called = 0;
42 static int write_cb_called = 0;
43 static int close_cb_called = 0;
44 static size_t bytes_sent = 0;
45 static size_t bytes_sent_done = 0;
46 static size_t bytes_received_done = 0;
47 
48 static uv_connect_t connect_req;
49 static uv_shutdown_t shutdown_req;
50 static uv_write_t write_reqs[WRITES];
51 
52 
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)53 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
54   buf->base = malloc(size);
55   buf->len = size;
56 }
57 
58 
close_cb(uv_handle_t * handle)59 static void close_cb(uv_handle_t* handle) {
60   ASSERT_NOT_NULL(handle);
61   close_cb_called++;
62 }
63 
64 
shutdown_cb(uv_shutdown_t * req,int status)65 static void shutdown_cb(uv_shutdown_t* req, int status) {
66   uv_tcp_t* tcp;
67 
68   ASSERT_PTR_EQ(req, &shutdown_req);
69   ASSERT_OK(status);
70 
71   tcp = (uv_tcp_t*)(req->handle);
72 
73   /* The write buffer should be empty by now. */
74   ASSERT_OK(tcp->write_queue_size);
75 
76   /* Now we wait for the EOF */
77   shutdown_cb_called++;
78 
79   /* We should have had all the writes called already. */
80   ASSERT_EQ(write_cb_called, WRITES);
81 }
82 
83 
read_cb(uv_stream_t * tcp,ssize_t nread,const uv_buf_t * buf)84 static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
85   ASSERT_NOT_NULL(tcp);
86 
87   if (nread >= 0) {
88     bytes_received_done += nread;
89   }
90   else {
91     ASSERT_EQ(nread, UV_EOF);
92     printf("GOT EOF\n");
93     uv_close((uv_handle_t*)tcp, close_cb);
94   }
95 
96   free(buf->base);
97 }
98 
99 
write_cb(uv_write_t * req,int status)100 static void write_cb(uv_write_t* req, int status) {
101   ASSERT_NOT_NULL(req);
102 
103   if (status) {
104     fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
105     ASSERT(0);
106   }
107 
108   bytes_sent_done += CHUNKS_PER_WRITE * CHUNK_SIZE;
109   write_cb_called++;
110 }
111 
112 
connect_cb(uv_connect_t * req,int status)113 static void connect_cb(uv_connect_t* req, int status) {
114   uv_buf_t send_bufs[CHUNKS_PER_WRITE];
115   uv_stream_t* stream;
116   int i, j, r;
117 
118   ASSERT_PTR_EQ(req, &connect_req);
119   ASSERT_OK(status);
120 
121   stream = req->handle;
122   connect_cb_called++;
123 
124   /* Write a lot of data */
125   for (i = 0; i < WRITES; i++) {
126     uv_write_t* write_req = write_reqs + i;
127 
128     for (j = 0; j < CHUNKS_PER_WRITE; j++) {
129       send_bufs[j] = uv_buf_init(send_buffer + bytes_sent, CHUNK_SIZE);
130       bytes_sent += CHUNK_SIZE;
131     }
132 
133     r = uv_write(write_req, stream, send_bufs, CHUNKS_PER_WRITE, write_cb);
134     ASSERT_OK(r);
135   }
136 
137   /* Shutdown on drain. */
138   r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
139   ASSERT_OK(r);
140 
141   /* Start reading */
142   r = uv_read_start(stream, alloc_cb, read_cb);
143   ASSERT_OK(r);
144 }
145 
146 
TEST_IMPL(tcp_writealot)147 TEST_IMPL(tcp_writealot) {
148   struct sockaddr_in addr;
149   uv_tcp_t client;
150   int r;
151 
152 #if defined(__MSAN__) || defined(__TSAN__)
153   RETURN_SKIP("Test is too slow to run under "
154               "MemorySanitizer or ThreadSanitizer");
155 #endif
156 
157   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
158 
159   send_buffer = calloc(1, TOTAL_BYTES);
160   ASSERT_NOT_NULL(send_buffer);
161 
162   r = uv_tcp_init(uv_default_loop(), &client);
163   ASSERT_OK(r);
164 
165   r = uv_tcp_connect(&connect_req,
166                      &client,
167                      (const struct sockaddr*) &addr,
168                      connect_cb);
169   ASSERT_OK(r);
170 
171   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
172 
173   ASSERT_EQ(1, shutdown_cb_called);
174   ASSERT_EQ(1, connect_cb_called);
175   ASSERT_EQ(write_cb_called, WRITES);
176   ASSERT_EQ(1, close_cb_called);
177   ASSERT_EQ(bytes_sent, TOTAL_BYTES);
178   ASSERT_EQ(bytes_sent_done, TOTAL_BYTES);
179   ASSERT_EQ(bytes_received_done, TOTAL_BYTES);
180 
181   free(send_buffer);
182 
183   MAKE_VALGRIND_HAPPY(uv_default_loop());
184   return 0;
185 }
186