PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test-tcp-writealot.c

http://github.com/joyent/libuv
C | 171 lines | 101 code | 44 blank | 26 comment | 28 complexity | 7e460919bcaacb799ad275ae5ea9e4cf MD5 | raw 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
 27
 28#define WRITES            3
 29#define CHUNKS_PER_WRITE  3
 30#define CHUNK_SIZE        10485760 /* 10 MB */
 31
 32#define TOTAL_BYTES       (WRITES * CHUNKS_PER_WRITE * CHUNK_SIZE)
 33
 34static char* send_buffer;
 35
 36static int shutdown_cb_called = 0;
 37static int connect_cb_called = 0;
 38static int write_cb_called = 0;
 39static int close_cb_called = 0;
 40static size_t bytes_sent = 0;
 41static size_t bytes_sent_done = 0;
 42static size_t bytes_received_done = 0;
 43
 44static uv_connect_t connect_req;
 45static uv_shutdown_t shutdown_req;
 46static uv_write_t write_reqs[WRITES];
 47
 48
 49static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
 50  return uv_buf_init(malloc(size), size);
 51}
 52
 53
 54static void close_cb(uv_handle_t* handle) {
 55  ASSERT(handle != NULL);
 56  close_cb_called++;
 57}
 58
 59
 60static void shutdown_cb(uv_shutdown_t* req, int status) {
 61  uv_tcp_t* tcp;
 62
 63  ASSERT(req == &shutdown_req);
 64  ASSERT(status == 0);
 65
 66  tcp = (uv_tcp_t*)(req->handle);
 67
 68  /* The write buffer should be empty by now. */
 69  ASSERT(tcp->write_queue_size == 0);
 70
 71  /* Now we wait for the EOF */
 72  shutdown_cb_called++;
 73
 74  /* We should have had all the writes called already. */
 75  ASSERT(write_cb_called == WRITES);
 76}
 77
 78
 79static void read_cb(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
 80  ASSERT(tcp != NULL);
 81
 82  if (nread >= 0) {
 83    bytes_received_done += nread;
 84  }
 85  else {
 86    ASSERT(uv_last_error(uv_default_loop()).code == UV_EOF);
 87    printf("GOT EOF\n");
 88    uv_close((uv_handle_t*)tcp, close_cb);
 89  }
 90
 91  free(buf.base);
 92}
 93
 94
 95static void write_cb(uv_write_t* req, int status) {
 96  ASSERT(req != NULL);
 97
 98  if (status) {
 99    uv_err_t err = uv_last_error(uv_default_loop());
100    fprintf(stderr, "uv_write error: %s\n", uv_strerror(err));
101    ASSERT(0);
102  }
103
104  bytes_sent_done += CHUNKS_PER_WRITE * CHUNK_SIZE;
105  write_cb_called++;
106}
107
108
109static void connect_cb(uv_connect_t* req, int status) {
110  uv_buf_t send_bufs[CHUNKS_PER_WRITE];
111  uv_stream_t* stream;
112  int i, j, r;
113
114  ASSERT(req == &connect_req);
115  ASSERT(status == 0);
116
117  stream = req->handle;
118  connect_cb_called++;
119
120  /* Write a lot of data */
121  for (i = 0; i < WRITES; i++) {
122    uv_write_t* write_req = write_reqs + i;
123
124    for (j = 0; j < CHUNKS_PER_WRITE; j++) {
125      send_bufs[j] = uv_buf_init(send_buffer + bytes_sent, CHUNK_SIZE);
126      bytes_sent += CHUNK_SIZE;
127    }
128
129    r = uv_write(write_req, stream, send_bufs, CHUNKS_PER_WRITE, write_cb);
130    ASSERT(r == 0);
131  }
132
133  /* Shutdown on drain. */
134  r = uv_shutdown(&shutdown_req, stream, shutdown_cb);
135  ASSERT(r == 0);
136
137  /* Start reading */
138  r = uv_read_start(stream, alloc_cb, read_cb);
139  ASSERT(r == 0);
140}
141
142
143TEST_IMPL(tcp_writealot) {
144  struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
145  uv_tcp_t client;
146  int r;
147
148  send_buffer = calloc(1, TOTAL_BYTES);
149  ASSERT(send_buffer != NULL);
150
151  r = uv_tcp_init(uv_default_loop(), &client);
152  ASSERT(r == 0);
153
154  r = uv_tcp_connect(&connect_req, &client, addr, connect_cb);
155  ASSERT(r == 0);
156
157  uv_run(uv_default_loop());
158
159  ASSERT(shutdown_cb_called == 1);
160  ASSERT(connect_cb_called == 1);
161  ASSERT(write_cb_called == WRITES);
162  ASSERT(close_cb_called == 1);
163  ASSERT(bytes_sent == TOTAL_BYTES);
164  ASSERT(bytes_sent_done == TOTAL_BYTES);
165  ASSERT(bytes_received_done == TOTAL_BYTES);
166
167  free(send_buffer);
168
169  MAKE_VALGRIND_HAPPY();
170  return 0;
171}