xref: /libuv/test/test-watcher-cross-stop.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 
25 #include <string.h>
26 #include <errno.h>
27 
28 /* NOTE: Number should be big enough to trigger this problem */
29 #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
30 /* Cygwin crashes or hangs in socket() with too many AF_INET sockets.  */
31 /* IBMi PASE timeout with too many AF_INET sockets.  */
32 static uv_udp_t sockets[1250];
33 #else
34 static uv_udp_t sockets[2500];
35 #endif
36 static uv_udp_send_t reqs[ARRAY_SIZE(sockets)];
37 static char slab[1];
38 static unsigned int recv_cb_called;
39 static unsigned int send_cb_called;
40 static unsigned int close_cb_called;
41 
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)42 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
43   buf->base = slab;
44   buf->len = sizeof(slab);
45 }
46 
47 
recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * buf,const struct sockaddr * addr,unsigned flags)48 static void recv_cb(uv_udp_t* handle,
49                     ssize_t nread,
50                     const uv_buf_t* buf,
51                     const struct sockaddr* addr,
52                     unsigned flags) {
53   recv_cb_called++;
54 }
55 
56 
send_cb(uv_udp_send_t * req,int status)57 static void send_cb(uv_udp_send_t* req, int status) {
58   send_cb_called++;
59 }
60 
61 
close_cb(uv_handle_t * handle)62 static void close_cb(uv_handle_t* handle) {
63   close_cb_called++;
64 }
65 
66 
TEST_IMPL(watcher_cross_stop)67 TEST_IMPL(watcher_cross_stop) {
68 #if defined(__MVS__)
69   RETURN_SKIP("zOS does not allow address or port reuse when using UDP sockets");
70 #endif
71   uv_loop_t* loop = uv_default_loop();
72   unsigned int i;
73   struct sockaddr_in addr;
74   uv_buf_t buf;
75   char big_string[1024];
76 
77   TEST_FILE_LIMIT(ARRAY_SIZE(sockets) + 32);
78 
79   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
80   memset(big_string, 'A', sizeof(big_string));
81   buf = uv_buf_init(big_string, sizeof(big_string));
82 
83   for (i = 0; i < ARRAY_SIZE(sockets); i++) {
84     ASSERT_OK(uv_udp_init(loop, &sockets[i]));
85     ASSERT_OK(uv_udp_bind(&sockets[i],
86                           (const struct sockaddr*) &addr,
87                           UV_UDP_REUSEADDR));
88     ASSERT_OK(uv_udp_recv_start(&sockets[i], alloc_cb, recv_cb));
89     ASSERT_OK(uv_udp_send(&reqs[i],
90                           &sockets[i],
91                           &buf,
92                           1,
93                           (const struct sockaddr*) &addr,
94                           send_cb));
95   }
96 
97   while (recv_cb_called == 0)
98     uv_run(loop, UV_RUN_ONCE);
99 
100   for (i = 0; i < ARRAY_SIZE(sockets); i++)
101     uv_close((uv_handle_t*) &sockets[i], close_cb);
102 
103   ASSERT_GT(recv_cb_called, 0);
104 
105   uv_run(loop, UV_RUN_DEFAULT);
106 
107   ASSERT_EQ(ARRAY_SIZE(sockets), send_cb_called);
108   ASSERT_EQ(ARRAY_SIZE(sockets), close_cb_called);
109 
110   MAKE_VALGRIND_HAPPY(loop);
111   return 0;
112 }
113