|
0 |
/* Copyright (c) 2015 Saúl Ibarra Corretgé <saghul@gmail.com>.
|
|
1 |
* 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 |
#include <stdio.h>
|
|
25 |
#include <stdlib.h>
|
|
26 |
|
|
27 |
|
|
28 |
static int connection_cb_called = 0;
|
|
29 |
static int connect_cb_called = 0;
|
|
30 |
|
|
31 |
#define NUM_CLIENTS 4
|
|
32 |
|
|
33 |
typedef struct {
|
|
34 |
uv_pipe_t pipe_handle;
|
|
35 |
uv_connect_t conn_req;
|
|
36 |
} client_t;
|
|
37 |
|
|
38 |
static uv_pipe_t server_handle;
|
|
39 |
static client_t clients[NUM_CLIENTS];
|
|
40 |
static uv_pipe_t connections[NUM_CLIENTS];
|
|
41 |
|
|
42 |
|
|
43 |
static void connection_cb(uv_stream_t* server, int status) {
|
|
44 |
int r;
|
|
45 |
uv_pipe_t* conn;
|
|
46 |
ASSERT(status == 0);
|
|
47 |
|
|
48 |
conn = &connections[connection_cb_called];
|
|
49 |
r = uv_pipe_init(server->loop, conn, 0);
|
|
50 |
ASSERT(r == 0);
|
|
51 |
|
|
52 |
r = uv_accept(server, (uv_stream_t*)conn);
|
|
53 |
ASSERT(r == 0);
|
|
54 |
|
|
55 |
if (++connection_cb_called == NUM_CLIENTS &&
|
|
56 |
connect_cb_called == NUM_CLIENTS) {
|
|
57 |
uv_stop(server->loop);
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
|
|
62 |
static void connect_cb(uv_connect_t* connect_req, int status) {
|
|
63 |
ASSERT(status == 0);
|
|
64 |
if (++connect_cb_called == NUM_CLIENTS &&
|
|
65 |
connection_cb_called == NUM_CLIENTS) {
|
|
66 |
uv_stop(connect_req->handle->loop);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
|
|
71 |
TEST_IMPL(pipe_connect_multiple) {
|
|
72 |
int i;
|
|
73 |
int r;
|
|
74 |
uv_loop_t* loop;
|
|
75 |
|
|
76 |
loop = uv_default_loop();
|
|
77 |
|
|
78 |
r = uv_pipe_init(loop, &server_handle, 0);
|
|
79 |
ASSERT(r == 0);
|
|
80 |
|
|
81 |
r = uv_pipe_bind(&server_handle, TEST_PIPENAME);
|
|
82 |
ASSERT(r == 0);
|
|
83 |
|
|
84 |
r = uv_listen((uv_stream_t*)&server_handle, 128, connection_cb);
|
|
85 |
ASSERT(r == 0);
|
|
86 |
|
|
87 |
for (i = 0; i < NUM_CLIENTS; i++) {
|
|
88 |
r = uv_pipe_init(loop, &clients[i].pipe_handle, 0);
|
|
89 |
ASSERT(r == 0);
|
|
90 |
uv_pipe_connect(&clients[i].conn_req,
|
|
91 |
&clients[i].pipe_handle,
|
|
92 |
TEST_PIPENAME,
|
|
93 |
connect_cb);
|
|
94 |
}
|
|
95 |
|
|
96 |
uv_run(loop, UV_RUN_DEFAULT);
|
|
97 |
|
|
98 |
ASSERT(connection_cb_called == NUM_CLIENTS);
|
|
99 |
ASSERT(connect_cb_called == NUM_CLIENTS);
|
|
100 |
|
|
101 |
MAKE_VALGRIND_HAPPY();
|
|
102 |
return 0;
|
|
103 |
}
|