1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
30 #include <sys/resource.h> /* setrlimit() */
33 /* NOTE: Number should be big enough to trigger this problem */
34 static uv_udp_t sockets[2500];
35 static uv_udp_send_t reqs[ARRAY_SIZE(sockets)];
37 static unsigned int recv_cb_called;
38 static unsigned int send_cb_called;
39 static unsigned int close_cb_called;
41 static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
42 return uv_buf_init(buf, sizeof(buf));
46 static void recv_cb(uv_udp_t* handle,
49 struct sockaddr* addr,
55 static void send_cb(uv_udp_send_t* req, int status) {
60 static void close_cb(uv_handle_t* handle) {
65 TEST_IMPL(watcher_cross_stop) {
66 uv_loop_t* loop = uv_default_loop();
68 struct sockaddr_in addr;
70 char big_string[1024];
75 lim.rlim_cur = ARRAY_SIZE(sockets) + 32;
76 lim.rlim_max = ARRAY_SIZE(sockets) + 32;
77 if (setrlimit(RLIMIT_NOFILE, &lim))
78 RETURN_SKIP("File descriptor limit too low.");
82 addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
83 memset(big_string, 'A', sizeof(big_string));
84 buf = uv_buf_init(big_string, sizeof(big_string));
86 for (i = 0; i < ARRAY_SIZE(sockets); i++) {
87 ASSERT(0 == uv_udp_init(loop, &sockets[i]));
88 ASSERT(0 == uv_udp_bind(&sockets[i], addr, 0));
89 ASSERT(0 == uv_udp_recv_start(&sockets[i], alloc_cb, recv_cb));
90 ASSERT(0 == uv_udp_send(&reqs[i], &sockets[i], &buf, 1, addr, send_cb));
93 while (recv_cb_called == 0)
94 uv_run(loop, UV_RUN_ONCE);
96 for (i = 0; i < ARRAY_SIZE(sockets); i++)
97 uv_close((uv_handle_t*) &sockets[i], close_cb);
99 ASSERT(0 < recv_cb_called && recv_cb_called <= ARRAY_SIZE(sockets));
100 ASSERT(ARRAY_SIZE(sockets) == send_cb_called);
102 uv_run(loop, UV_RUN_DEFAULT);
104 ASSERT(ARRAY_SIZE(sockets) == close_cb_called);
106 MAKE_VALGRIND_HAPPY();