xref: /libuv/test/test-multiple-listen.c (revision 011a1ac1)
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 #include <stdio.h>
25 #include <stdlib.h>
26 
27 static int connection_cb_called = 0;
28 static int close_cb_called = 0;
29 static int connect_cb_called = 0;
30 static uv_tcp_t server;
31 static uv_tcp_t client;
32 
33 
close_cb(uv_handle_t * handle)34 static void close_cb(uv_handle_t* handle) {
35   ASSERT_NOT_NULL(handle);
36   close_cb_called++;
37 }
38 
39 
connection_cb(uv_stream_t * tcp,int status)40 static void connection_cb(uv_stream_t* tcp, int status) {
41   ASSERT_OK(status);
42   uv_close((uv_handle_t*)&server, close_cb);
43   connection_cb_called++;
44 }
45 
46 
start_server(void)47 static void start_server(void) {
48   struct sockaddr_in addr;
49   int r;
50 
51   ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
52 
53   r = uv_tcp_init(uv_default_loop(), &server);
54   ASSERT_OK(r);
55 
56   r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
57   ASSERT_OK(r);
58 
59   r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
60   ASSERT_OK(r);
61 
62   r = uv_listen((uv_stream_t*)&server, 128, connection_cb);
63   ASSERT_OK(r);
64 }
65 
66 
connect_cb(uv_connect_t * req,int status)67 static void connect_cb(uv_connect_t* req, int status) {
68   ASSERT_NOT_NULL(req);
69   ASSERT_OK(status);
70   free(req);
71   uv_close((uv_handle_t*)&client, close_cb);
72   connect_cb_called++;
73 }
74 
75 
client_connect(void)76 static void client_connect(void) {
77   struct sockaddr_in addr;
78   uv_connect_t* connect_req = malloc(sizeof *connect_req);
79   int r;
80 
81   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
82   ASSERT_NOT_NULL(connect_req);
83 
84   r = uv_tcp_init(uv_default_loop(), &client);
85   ASSERT_OK(r);
86 
87   r = uv_tcp_connect(connect_req,
88                      &client,
89                      (const struct sockaddr*) &addr,
90                      connect_cb);
91   ASSERT_OK(r);
92 }
93 
94 
95 
TEST_IMPL(multiple_listen)96 TEST_IMPL(multiple_listen) {
97   start_server();
98 
99   client_connect();
100 
101   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
102 
103   ASSERT_EQ(1, connection_cb_called);
104   ASSERT_EQ(1, connect_cb_called);
105   ASSERT_EQ(2, close_cb_called);
106 
107   MAKE_VALGRIND_HAPPY(uv_default_loop());
108   return 0;
109 }
110