uv: Upgrade to v0.10.19
[platform/upstream/nodejs.git] / deps / uv / test / test-watcher-cross-stop.c
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 #if !defined(_WIN32)
29 #include <sys/time.h>
30 #include <sys/resource.h>  /* setrlimit() */
31 #endif
32
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)];
36 static char buf[1];
37 static unsigned int recv_cb_called;
38 static unsigned int send_cb_called;
39 static unsigned int close_cb_called;
40
41 static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
42   return uv_buf_init(buf, sizeof(buf));
43 }
44
45
46 static void recv_cb(uv_udp_t* handle,
47                     ssize_t nread,
48                     uv_buf_t buf,
49                     struct sockaddr* addr,
50                     unsigned flags) {
51   recv_cb_called++;
52 }
53
54
55 static void send_cb(uv_udp_send_t* req, int status) {
56   send_cb_called++;
57 }
58
59
60 static void close_cb(uv_handle_t* handle) {
61   close_cb_called++;
62 }
63
64
65 TEST_IMPL(watcher_cross_stop) {
66   uv_loop_t* loop = uv_default_loop();
67   unsigned int i;
68   struct sockaddr_in addr;
69   uv_buf_t buf;
70   char big_string[1024];
71
72 #if !defined(_WIN32)
73   {
74     struct rlimit lim;
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.");
79   }
80 #endif
81
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));
85
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));
91   }
92
93   while (recv_cb_called == 0)
94     uv_run(loop, UV_RUN_ONCE);
95
96   for (i = 0; i < ARRAY_SIZE(sockets); i++)
97     uv_close((uv_handle_t*) &sockets[i], close_cb);
98
99   ASSERT(0 < recv_cb_called && recv_cb_called <= ARRAY_SIZE(sockets));
100   ASSERT(ARRAY_SIZE(sockets) == send_cb_called);
101
102   uv_run(loop, UV_RUN_DEFAULT);
103
104   ASSERT(ARRAY_SIZE(sockets) == close_cb_called);
105
106   MAKE_VALGRIND_HAPPY();
107   return 0;
108 }