xref: /libuv/test/test-tcp-try-write-error.c (revision 011a1ac1)
1 /* Copyright libuv 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 static uv_tcp_t server;
30 static uv_tcp_t client;
31 static uv_tcp_t incoming;
32 static int connect_cb_called;
33 static int close_cb_called;
34 static int connection_cb_called;
35 
36 
close_cb(uv_handle_t * handle)37 static void close_cb(uv_handle_t* handle) {
38   close_cb_called++;
39 }
40 
incoming_close_cb(uv_handle_t * handle)41 static void incoming_close_cb(uv_handle_t* handle) {
42   uv_buf_t buf;
43   int r = 1;
44 
45   close_cb_called++;
46 
47   buf = uv_buf_init("meow", 4);
48   while (r > 0)
49     r = uv_try_write((uv_stream_t*) &client, &buf, 1);
50   fprintf(stderr, "uv_try_write error: %d %s\n", r, uv_strerror(r));
51   ASSERT(r == UV_EPIPE || r == UV_ECONNABORTED || r == UV_ECONNRESET);
52   ASSERT_OK(client.write_queue_size);
53 }
54 
55 
connect_cb(uv_connect_t * req,int status)56 static void connect_cb(uv_connect_t* req, int status) {
57   ASSERT_OK(status);
58   connect_cb_called++;
59 }
60 
61 
connection_cb(uv_stream_t * tcp,int status)62 static void connection_cb(uv_stream_t* tcp, int status) {
63   ASSERT_OK(status);
64 
65   ASSERT_OK(uv_tcp_init(tcp->loop, &incoming));
66   ASSERT_OK(uv_accept(tcp, (uv_stream_t*) &incoming));
67 
68   connection_cb_called++;
69   uv_close((uv_handle_t*) &incoming, incoming_close_cb);
70   uv_close((uv_handle_t*) tcp, close_cb);
71 }
72 
73 
start_server(void)74 static void start_server(void) {
75   struct sockaddr_in addr;
76 
77   ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
78 
79   ASSERT_OK(uv_tcp_init(uv_default_loop(), &server));
80   ASSERT_OK(uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));
81   ASSERT_OK(uv_listen((uv_stream_t*) &server, 128, connection_cb));
82 }
83 
84 
TEST_IMPL(tcp_try_write_error)85 TEST_IMPL(tcp_try_write_error) {
86   uv_connect_t connect_req;
87   struct sockaddr_in addr;
88 
89   start_server();
90 
91   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
92 
93   ASSERT_OK(uv_tcp_init(uv_default_loop(), &client));
94   ASSERT_OK(uv_tcp_connect(&connect_req,
95                            &client,
96                            (struct sockaddr*) &addr,
97                            connect_cb));
98 
99   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
100   uv_close((uv_handle_t*) &client, close_cb);
101   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
102 
103   ASSERT_EQ(1, connect_cb_called);
104   ASSERT_EQ(3, close_cb_called);
105   ASSERT_EQ(1, connection_cb_called);
106 
107   MAKE_VALGRIND_HAPPY(uv_default_loop());
108   return 0;
109 }
110