1 /* Copyright (c) 2015 Saúl Ibarra Corretgé <saghul@gmail.com>.
2 * All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 * IN THE SOFTWARE.
21 */
22
23 #include "uv.h"
24 #include "task.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27
28
29 static int connection_cb_called = 0;
30 static int connect_cb_called = 0;
31
32 #define NUM_CLIENTS 10
33
34 typedef struct {
35 uv_pipe_t pipe_handle;
36 uv_connect_t conn_req;
37 } client_t;
38
39 static uv_pipe_t server_handle;
40 static client_t clients[NUM_CLIENTS];
41 static uv_pipe_t connections[NUM_CLIENTS];
42
43
connection_cb(uv_stream_t * server,int status)44 static void connection_cb(uv_stream_t* server, int status) {
45 int r;
46 uv_pipe_t* conn;
47 ASSERT_OK(status);
48
49 conn = &connections[connection_cb_called];
50 r = uv_pipe_init(server->loop, conn, 0);
51 ASSERT_OK(r);
52
53 r = uv_accept(server, (uv_stream_t*)conn);
54 ASSERT_OK(r);
55
56 if (++connection_cb_called == NUM_CLIENTS &&
57 connect_cb_called == NUM_CLIENTS) {
58 uv_stop(server->loop);
59 }
60 }
61
62
connect_cb(uv_connect_t * connect_req,int status)63 static void connect_cb(uv_connect_t* connect_req, int status) {
64 ASSERT_OK(status);
65 if (++connect_cb_called == NUM_CLIENTS &&
66 connection_cb_called == NUM_CLIENTS) {
67 uv_stop(connect_req->handle->loop);
68 }
69 }
70
71
TEST_IMPL(pipe_connect_multiple)72 TEST_IMPL(pipe_connect_multiple) {
73 #if defined(NO_SELF_CONNECT)
74 RETURN_SKIP(NO_SELF_CONNECT);
75 #endif
76 int i;
77 int r;
78 uv_loop_t* loop;
79
80 loop = uv_default_loop();
81
82 r = uv_pipe_init(loop, &server_handle, 0);
83 ASSERT_OK(r);
84
85 r = uv_pipe_bind(&server_handle, TEST_PIPENAME);
86 ASSERT_OK(r);
87
88 r = uv_listen((uv_stream_t*)&server_handle, 128, connection_cb);
89 ASSERT_OK(r);
90
91 for (i = 0; i < NUM_CLIENTS; i++) {
92 r = uv_pipe_init(loop, &clients[i].pipe_handle, 0);
93 ASSERT_OK(r);
94 uv_pipe_connect(&clients[i].conn_req,
95 &clients[i].pipe_handle,
96 TEST_PIPENAME,
97 connect_cb);
98 }
99
100 uv_run(loop, UV_RUN_DEFAULT);
101
102 ASSERT_EQ(connection_cb_called, NUM_CLIENTS);
103 ASSERT_EQ(connect_cb_called, NUM_CLIENTS);
104
105 MAKE_VALGRIND_HAPPY(loop);
106 return 0;
107 }
108
109
connection_cb2(uv_stream_t * server,int status)110 static void connection_cb2(uv_stream_t* server, int status) {
111 int r;
112 uv_pipe_t* conn;
113 ASSERT_OK(status);
114
115 conn = &connections[connection_cb_called];
116 r = uv_pipe_init(server->loop, conn, 0);
117 ASSERT_OK(r);
118
119 r = uv_accept(server, (uv_stream_t*)conn);
120 ASSERT_OK(r);
121
122 uv_close((uv_handle_t*)conn, NULL);
123 if (++connection_cb_called == NUM_CLIENTS &&
124 connect_cb_called == NUM_CLIENTS) {
125 uv_close((uv_handle_t*)&server_handle, NULL);
126 }
127 }
128
connect_cb2(uv_connect_t * connect_req,int status)129 static void connect_cb2(uv_connect_t* connect_req, int status) {
130 ASSERT_EQ(status, UV_ECANCELED);
131 if (++connect_cb_called == NUM_CLIENTS &&
132 connection_cb_called == NUM_CLIENTS) {
133 uv_close((uv_handle_t*)&server_handle, NULL);
134 }
135 }
136
137
TEST_IMPL(pipe_connect_close_multiple)138 TEST_IMPL(pipe_connect_close_multiple) {
139 #if defined(NO_SELF_CONNECT)
140 RETURN_SKIP(NO_SELF_CONNECT);
141 #endif
142 int i;
143 int r;
144 uv_loop_t* loop;
145
146 loop = uv_default_loop();
147
148 r = uv_pipe_init(loop, &server_handle, 0);
149 ASSERT_OK(r);
150
151 r = uv_pipe_bind(&server_handle, TEST_PIPENAME);
152 ASSERT_OK(r);
153
154 r = uv_listen((uv_stream_t*)&server_handle, 128, connection_cb2);
155 ASSERT_OK(r);
156
157 for (i = 0; i < NUM_CLIENTS; i++) {
158 r = uv_pipe_init(loop, &clients[i].pipe_handle, 0);
159 ASSERT_OK(r);
160 uv_pipe_connect(&clients[i].conn_req,
161 &clients[i].pipe_handle,
162 TEST_PIPENAME,
163 connect_cb2);
164 }
165
166 for (i = 0; i < NUM_CLIENTS; i++) {
167 uv_close((uv_handle_t*)&clients[i].pipe_handle, NULL);
168 }
169
170
171 uv_run(loop, UV_RUN_DEFAULT);
172
173 ASSERT_EQ(connection_cb_called, NUM_CLIENTS);
174 ASSERT_EQ(connect_cb_called, NUM_CLIENTS);
175
176 MAKE_VALGRIND_HAPPY(loop);
177 return 0;
178 }
179