uv: Upgrade to v0.11.17
[platform/upstream/nodejs.git] / deps / uv / test / test-tcp-try-write.c
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
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define MAX_BYTES 1024 * 1024
30
31 #ifdef _WIN32
32
33 TEST_IMPL(tcp_try_write) {
34
35   MAKE_VALGRIND_HAPPY();
36   return 0;
37 }
38
39 #else  /* !_WIN32 */
40
41 static uv_tcp_t server;
42 static uv_tcp_t client;
43 static uv_tcp_t incoming;
44 static int connect_cb_called;
45 static int close_cb_called;
46 static int connection_cb_called;
47 static int bytes_read;
48 static int bytes_written;
49
50
51 static void close_cb(uv_handle_t* handle) {
52   close_cb_called++;
53 }
54
55
56 static void connect_cb(uv_connect_t* req, int status) {
57   static char zeroes[1024];
58   int r;
59   uv_buf_t buf;
60   ASSERT(status == 0);
61   connect_cb_called++;
62
63   do {
64     buf = uv_buf_init(zeroes, sizeof(zeroes));
65     r = uv_try_write((uv_stream_t*) &client, &buf, 1);
66     ASSERT(r >= 0);
67     bytes_written += r;
68
69     /* Partial write */
70     if (r != (int) sizeof(zeroes))
71       break;
72   } while (1);
73   uv_close((uv_handle_t*) &client, close_cb);
74 }
75
76
77 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
78   static char base[1024];
79
80   buf->base = base;
81   buf->len = sizeof(base);
82 }
83
84
85 static void read_cb(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
86   if (nread < 0) {
87     uv_close((uv_handle_t*) tcp, close_cb);
88     uv_close((uv_handle_t*) &server, close_cb);
89     return;
90   }
91
92   bytes_read += nread;
93 }
94
95
96 static void connection_cb(uv_stream_t* tcp, int status) {
97   ASSERT(status == 0);
98
99   ASSERT(0 == uv_tcp_init(tcp->loop, &incoming));
100   ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming));
101
102   connection_cb_called++;
103   ASSERT(0 == uv_read_start((uv_stream_t*) &incoming, alloc_cb, read_cb));
104 }
105
106
107 static void start_server(void) {
108   struct sockaddr_in addr;
109
110   ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
111
112   ASSERT(0 == uv_tcp_init(uv_default_loop(), &server));
113   ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr));
114   ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb));
115 }
116
117
118 TEST_IMPL(tcp_try_write) {
119   uv_connect_t connect_req;
120   struct sockaddr_in addr;
121
122   start_server();
123
124   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
125
126   ASSERT(0 == uv_tcp_init(uv_default_loop(), &client));
127   ASSERT(0 == uv_tcp_connect(&connect_req,
128                              &client,
129                              (struct sockaddr*) &addr,
130                              connect_cb));
131
132   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
133
134   ASSERT(connect_cb_called == 1);
135   ASSERT(close_cb_called == 3);
136   ASSERT(connection_cb_called == 1);
137   ASSERT(bytes_read == bytes_written);
138   ASSERT(bytes_written > 0);
139
140   MAKE_VALGRIND_HAPPY();
141   return 0;
142 }
143
144 #endif  /* !_WIN32 */