10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos #include <stdio.h>
250e552da7Schristos #include <stdlib.h>
260e552da7Schristos
270e552da7Schristos
280e552da7Schristos #define WRITES 3
290e552da7Schristos #if defined(__arm__) /* Decrease the chunks so the test passes on arm CI bots */
300e552da7Schristos #define CHUNKS_PER_WRITE 2048
310e552da7Schristos #else
320e552da7Schristos #define CHUNKS_PER_WRITE 4096
330e552da7Schristos #endif
340e552da7Schristos #define CHUNK_SIZE 10024 /* 10 kb */
350e552da7Schristos
360e552da7Schristos #define TOTAL_BYTES (WRITES * CHUNKS_PER_WRITE * CHUNK_SIZE)
370e552da7Schristos
380e552da7Schristos static char* send_buffer;
390e552da7Schristos
400e552da7Schristos static int shutdown_cb_called = 0;
410e552da7Schristos static int connect_cb_called = 0;
420e552da7Schristos static int write_cb_called = 0;
430e552da7Schristos static int close_cb_called = 0;
440e552da7Schristos static size_t bytes_sent = 0;
450e552da7Schristos static size_t bytes_sent_done = 0;
460e552da7Schristos static size_t bytes_received_done = 0;
470e552da7Schristos
480e552da7Schristos static uv_connect_t connect_req;
490e552da7Schristos static uv_shutdown_t shutdown_req;
500e552da7Schristos static uv_write_t write_reqs[WRITES];
510e552da7Schristos
520e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)530e552da7Schristos static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
540e552da7Schristos buf->base = malloc(size);
550e552da7Schristos buf->len = size;
560e552da7Schristos }
570e552da7Schristos
580e552da7Schristos
close_cb(uv_handle_t * handle)590e552da7Schristos static void close_cb(uv_handle_t* handle) {
60*5f2f4271Schristos ASSERT_NOT_NULL(handle);
610e552da7Schristos close_cb_called++;
620e552da7Schristos }
630e552da7Schristos
640e552da7Schristos
shutdown_cb(uv_shutdown_t * req,int status)650e552da7Schristos static void shutdown_cb(uv_shutdown_t* req, int status) {
660e552da7Schristos uv_tcp_t* tcp;
670e552da7Schristos
680e552da7Schristos ASSERT(req == &shutdown_req);
690e552da7Schristos ASSERT(status == 0);
700e552da7Schristos
710e552da7Schristos tcp = (uv_tcp_t*)(req->handle);
720e552da7Schristos
730e552da7Schristos /* The write buffer should be empty by now. */
740e552da7Schristos ASSERT(tcp->write_queue_size == 0);
750e552da7Schristos
760e552da7Schristos /* Now we wait for the EOF */
770e552da7Schristos shutdown_cb_called++;
780e552da7Schristos
790e552da7Schristos /* We should have had all the writes called already. */
800e552da7Schristos ASSERT(write_cb_called == WRITES);
810e552da7Schristos }
820e552da7Schristos
830e552da7Schristos
read_cb(uv_stream_t * tcp,ssize_t nread,const uv_buf_t * buf)840e552da7Schristos static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
85*5f2f4271Schristos ASSERT_NOT_NULL(tcp);
860e552da7Schristos
870e552da7Schristos if (nread >= 0) {
880e552da7Schristos bytes_received_done += nread;
890e552da7Schristos }
900e552da7Schristos else {
910e552da7Schristos ASSERT(nread == UV_EOF);
920e552da7Schristos printf("GOT EOF\n");
930e552da7Schristos uv_close((uv_handle_t*)tcp, close_cb);
940e552da7Schristos }
950e552da7Schristos
960e552da7Schristos free(buf->base);
970e552da7Schristos }
980e552da7Schristos
990e552da7Schristos
write_cb(uv_write_t * req,int status)1000e552da7Schristos static void write_cb(uv_write_t* req, int status) {
101*5f2f4271Schristos ASSERT_NOT_NULL(req);
1020e552da7Schristos
1030e552da7Schristos if (status) {
1040e552da7Schristos fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
1050e552da7Schristos ASSERT(0);
1060e552da7Schristos }
1070e552da7Schristos
1080e552da7Schristos bytes_sent_done += CHUNKS_PER_WRITE * CHUNK_SIZE;
1090e552da7Schristos write_cb_called++;
1100e552da7Schristos }
1110e552da7Schristos
1120e552da7Schristos
connect_cb(uv_connect_t * req,int status)1130e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
1140e552da7Schristos uv_buf_t send_bufs[CHUNKS_PER_WRITE];
1150e552da7Schristos uv_stream_t* stream;
1160e552da7Schristos int i, j, r;
1170e552da7Schristos
1180e552da7Schristos ASSERT(req == &connect_req);
1190e552da7Schristos ASSERT(status == 0);
1200e552da7Schristos
1210e552da7Schristos stream = req->handle;
1220e552da7Schristos connect_cb_called++;
1230e552da7Schristos
1240e552da7Schristos /* Write a lot of data */
1250e552da7Schristos for (i = 0; i < WRITES; i++) {
1260e552da7Schristos uv_write_t* write_req = write_reqs + i;
1270e552da7Schristos
1280e552da7Schristos for (j = 0; j < CHUNKS_PER_WRITE; j++) {
1290e552da7Schristos send_bufs[j] = uv_buf_init(send_buffer + bytes_sent, CHUNK_SIZE);
1300e552da7Schristos bytes_sent += CHUNK_SIZE;
1310e552da7Schristos }
1320e552da7Schristos
1330e552da7Schristos r = uv_write(write_req, stream, send_bufs, CHUNKS_PER_WRITE, write_cb);
1340e552da7Schristos ASSERT(r == 0);
1350e552da7Schristos }
1360e552da7Schristos
1370e552da7Schristos /* Shutdown on drain. */
1380e552da7Schristos r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
1390e552da7Schristos ASSERT(r == 0);
1400e552da7Schristos
1410e552da7Schristos /* Start reading */
1420e552da7Schristos r = uv_read_start(stream, alloc_cb, read_cb);
1430e552da7Schristos ASSERT(r == 0);
1440e552da7Schristos }
1450e552da7Schristos
1460e552da7Schristos
TEST_IMPL(tcp_writealot)1470e552da7Schristos TEST_IMPL(tcp_writealot) {
1480e552da7Schristos struct sockaddr_in addr;
1490e552da7Schristos uv_tcp_t client;
1500e552da7Schristos int r;
1510e552da7Schristos
1520e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
1530e552da7Schristos
1540e552da7Schristos send_buffer = calloc(1, TOTAL_BYTES);
155*5f2f4271Schristos ASSERT_NOT_NULL(send_buffer);
1560e552da7Schristos
1570e552da7Schristos r = uv_tcp_init(uv_default_loop(), &client);
1580e552da7Schristos ASSERT(r == 0);
1590e552da7Schristos
1600e552da7Schristos r = uv_tcp_connect(&connect_req,
1610e552da7Schristos &client,
1620e552da7Schristos (const struct sockaddr*) &addr,
1630e552da7Schristos connect_cb);
1640e552da7Schristos ASSERT(r == 0);
1650e552da7Schristos
1660e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1670e552da7Schristos
1680e552da7Schristos ASSERT(shutdown_cb_called == 1);
1690e552da7Schristos ASSERT(connect_cb_called == 1);
1700e552da7Schristos ASSERT(write_cb_called == WRITES);
1710e552da7Schristos ASSERT(close_cb_called == 1);
1720e552da7Schristos ASSERT(bytes_sent == TOTAL_BYTES);
1730e552da7Schristos ASSERT(bytes_sent_done == TOTAL_BYTES);
1740e552da7Schristos ASSERT(bytes_received_done == TOTAL_BYTES);
1750e552da7Schristos
1760e552da7Schristos free(send_buffer);
1770e552da7Schristos
1780e552da7Schristos MAKE_VALGRIND_HAPPY();
1790e552da7Schristos return 0;
1800e552da7Schristos }
181