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 4
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(status == 0);
48
49 conn = &connections[connection_cb_called];
50 r = uv_pipe_init(server->loop, conn, 0);
51 ASSERT(r == 0);
52
53 r = uv_accept(server, (uv_stream_t*)conn);
54 ASSERT(r == 0);
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(status == 0);
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(r == 0);
84
85 r = uv_pipe_bind(&server_handle, TEST_PIPENAME);
86 ASSERT(r == 0);
87
88 r = uv_listen((uv_stream_t*)&server_handle, 128, connection_cb);
89 ASSERT(r == 0);
90
91 for (i = 0; i < NUM_CLIENTS; i++) {
92 r = uv_pipe_init(loop, &clients[i].pipe_handle, 0);
93 ASSERT(r == 0);
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(connection_cb_called == NUM_CLIENTS);
103 ASSERT(connect_cb_called == NUM_CLIENTS);
104
105 MAKE_VALGRIND_HAPPY();
106 return 0;
107 }
108