0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/deps/uv/test/test-tcp-writealot.c

177 lines
4.5 KiB
C
Raw Normal View History

2011-05-13 04:16:40 +02:00
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
2011-05-13 04:16:40 +02:00
#include "task.h"
#include <stdio.h>
#include <stdlib.h>
#define WRITES 3
2013-04-12 17:43:05 +02:00
#define CHUNKS_PER_WRITE 4096
#define CHUNK_SIZE 10024 /* 10 kb */
2011-05-13 04:16:40 +02:00
#define TOTAL_BYTES (WRITES * CHUNKS_PER_WRITE * CHUNK_SIZE)
static char* send_buffer;
static int shutdown_cb_called = 0;
static int connect_cb_called = 0;
static int write_cb_called = 0;
static int close_cb_called = 0;
2012-06-05 15:45:46 +02:00
static size_t bytes_sent = 0;
static size_t bytes_sent_done = 0;
static size_t bytes_received_done = 0;
2011-05-13 04:16:40 +02:00
2012-05-23 04:01:19 +02:00
static uv_connect_t connect_req;
static uv_shutdown_t shutdown_req;
static uv_write_t write_reqs[WRITES];
2011-05-13 04:16:40 +02:00
static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
buf->base = malloc(size);
buf->len = size;
}
2011-06-09 15:14:58 +02:00
static void close_cb(uv_handle_t* handle) {
2011-05-13 04:16:40 +02:00
ASSERT(handle != NULL);
close_cb_called++;
}
static void shutdown_cb(uv_shutdown_t* req, int status) {
uv_tcp_t* tcp;
2012-05-23 04:01:19 +02:00
ASSERT(req == &shutdown_req);
2011-05-13 04:16:40 +02:00
ASSERT(status == 0);
tcp = (uv_tcp_t*)(req->handle);
2011-05-13 04:16:40 +02:00
/* The write buffer should be empty by now. */
ASSERT(tcp->write_queue_size == 0);
2011-05-13 04:16:40 +02:00
/* Now we wait for the EOF */
shutdown_cb_called++;
/* We should have had all the writes called already. */
ASSERT(write_cb_called == WRITES);
}
static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
ASSERT(tcp != NULL);
2011-05-13 04:16:40 +02:00
2012-05-23 04:01:19 +02:00
if (nread >= 0) {
bytes_received_done += nread;
}
else {
2013-07-16 21:04:31 +02:00
ASSERT(nread == UV_EOF);
2011-05-13 04:16:40 +02:00
printf("GOT EOF\n");
2011-06-09 15:14:58 +02:00
uv_close((uv_handle_t*)tcp, close_cb);
2011-05-13 04:16:40 +02:00
}
free(buf->base);
2011-05-13 04:16:40 +02:00
}
static void write_cb(uv_write_t* req, int status) {
2011-05-13 04:16:40 +02:00
ASSERT(req != NULL);
if (status) {
2013-07-16 21:04:31 +02:00
fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
2011-05-13 04:16:40 +02:00
ASSERT(0);
}
bytes_sent_done += CHUNKS_PER_WRITE * CHUNK_SIZE;
write_cb_called++;
}
static void connect_cb(uv_connect_t* req, int status) {
uv_buf_t send_bufs[CHUNKS_PER_WRITE];
2012-05-23 04:01:19 +02:00
uv_stream_t* stream;
2011-05-13 04:16:40 +02:00
int i, j, r;
2012-05-23 04:01:19 +02:00
ASSERT(req == &connect_req);
2011-05-13 04:16:40 +02:00
ASSERT(status == 0);
2012-05-23 04:01:19 +02:00
stream = req->handle;
2011-05-13 04:16:40 +02:00
connect_cb_called++;
/* Write a lot of data */
for (i = 0; i < WRITES; i++) {
2012-05-23 04:01:19 +02:00
uv_write_t* write_req = write_reqs + i;
2011-05-13 04:16:40 +02:00
for (j = 0; j < CHUNKS_PER_WRITE; j++) {
2012-05-23 04:01:19 +02:00
send_bufs[j] = uv_buf_init(send_buffer + bytes_sent, CHUNK_SIZE);
2011-05-13 04:16:40 +02:00
bytes_sent += CHUNK_SIZE;
}
2012-05-23 04:01:19 +02:00
r = uv_write(write_req, stream, send_bufs, CHUNKS_PER_WRITE, write_cb);
2011-05-13 04:16:40 +02:00
ASSERT(r == 0);
}
2012-05-23 04:01:19 +02:00
/* Shutdown on drain. */
r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
2011-05-13 04:16:40 +02:00
ASSERT(r == 0);
/* Start reading */
2012-05-23 04:01:19 +02:00
r = uv_read_start(stream, alloc_cb, read_cb);
2011-05-13 04:16:40 +02:00
ASSERT(r == 0);
}
TEST_IMPL(tcp_writealot) {
struct sockaddr_in addr;
2012-05-23 04:01:19 +02:00
uv_tcp_t client;
2011-05-13 04:16:40 +02:00
int r;
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
2012-10-06 23:04:30 +02:00
send_buffer = calloc(1, TOTAL_BYTES);
2011-05-13 04:16:40 +02:00
ASSERT(send_buffer != NULL);
2012-05-23 04:01:19 +02:00
r = uv_tcp_init(uv_default_loop(), &client);
2011-05-13 04:16:40 +02:00
ASSERT(r == 0);
r = uv_tcp_connect(&connect_req,
&client,
(const struct sockaddr*) &addr,
connect_cb);
2011-05-13 04:16:40 +02:00
ASSERT(r == 0);
2013-01-16 23:38:02 +01:00
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
2011-05-13 04:16:40 +02:00
ASSERT(shutdown_cb_called == 1);
ASSERT(connect_cb_called == 1);
ASSERT(write_cb_called == WRITES);
ASSERT(close_cb_called == 1);
ASSERT(bytes_sent == TOTAL_BYTES);
ASSERT(bytes_sent_done == TOTAL_BYTES);
ASSERT(bytes_received_done == TOTAL_BYTES);
2012-05-23 04:01:19 +02:00
free(send_buffer);
2012-10-17 18:33:40 +02:00
MAKE_VALGRIND_HAPPY();
2011-05-13 04:16:40 +02:00
return 0;
}