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 <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 26 #include "uv.h" 27 #include "task.h" 28 29 #define REQ_COUNT 10000 30 31 static uv_timer_t timer; 32 static uv_tcp_t server; 33 static uv_tcp_t client; 34 static uv_tcp_t incoming; 35 static int connect_cb_called; 36 static int close_cb_called; 37 static int connection_cb_called; 38 static int write_callbacks; 39 static int write_cancelled_callbacks; 40 static int write_error_callbacks; 41 42 static uv_write_t write_requests[REQ_COUNT]; 43 44 45 static void close_cb(uv_handle_t* handle) { 46 close_cb_called++; 47 } 48 49 static void timer_cb(uv_timer_t* handle) { 50 uv_close((uv_handle_t*) &client, close_cb); 51 uv_close((uv_handle_t*) &server, close_cb); 52 uv_close((uv_handle_t*) &incoming, close_cb); 53 } 54 55 static void write_cb(uv_write_t* req, int status) { 56 if (status == 0) 57 write_callbacks++; 58 else if (status == UV_ECANCELED) 59 write_cancelled_callbacks++; 60 else 61 write_error_callbacks++; 62 } 63 64 static void connect_cb(uv_connect_t* req, int status) { 65 static char base[1024]; 66 int r; 67 int i; 68 uv_buf_t buf; 69 70 ASSERT(status == 0); 71 connect_cb_called++; 72 73 buf = uv_buf_init(base, sizeof(base)); 74 75 for (i = 0; i < REQ_COUNT; i++) { 76 r = uv_write(&write_requests[i], 77 req->handle, 78 &buf, 79 1, 80 write_cb); 81 ASSERT(r == 0); 82 } 83 } 84 85 86 static void connection_cb(uv_stream_t* tcp, int status) { 87 ASSERT(status == 0); 88 89 ASSERT(0 == uv_tcp_init(tcp->loop, &incoming)); 90 ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming)); 91 92 ASSERT(0 == uv_timer_init(uv_default_loop(), &timer)); 93 ASSERT(0 == uv_timer_start(&timer, timer_cb, 1000, 0)); 94 95 connection_cb_called++; 96 } 97 98 99 static void start_server(void) { 100 struct sockaddr_in addr; 101 102 ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr)); 103 104 ASSERT(0 == uv_tcp_init(uv_default_loop(), &server)); 105 ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0)); 106 ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb)); 107 } 108 109 110 TEST_IMPL(tcp_write_queue_order) { 111 uv_connect_t connect_req; 112 struct sockaddr_in addr; 113 int buffer_size = 16 * 1024; 114 115 start_server(); 116 117 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); 118 119 ASSERT(0 == uv_tcp_init(uv_default_loop(), &client)); 120 ASSERT(0 == uv_tcp_connect(&connect_req, 121 &client, 122 (struct sockaddr*) &addr, 123 connect_cb)); 124 ASSERT(0 == uv_send_buffer_size((uv_handle_t*) &client, &buffer_size)); 125 126 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); 127 128 ASSERT(connect_cb_called == 1); 129 ASSERT(connection_cb_called == 1); 130 ASSERT(write_callbacks > 0); 131 ASSERT(write_cancelled_callbacks > 0); 132 ASSERT(write_callbacks + 133 write_error_callbacks + 134 write_cancelled_callbacks == REQ_COUNT); 135 ASSERT(close_cb_called == 3); 136 137 MAKE_VALGRIND_HAPPY(); 138 return 0; 139 } 140