• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TASK_H_
23 #define TASK_H_
24 
25 #include "uv.h"
26 
27 #include <stdio.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 
31 #if defined(_MSC_VER) && _MSC_VER < 1600
32 # include "uv/stdint-msvc2008.h"
33 #else
34 # include <stdint.h>
35 #endif
36 
37 #if !defined(_WIN32)
38 # include <sys/time.h>
39 # include <sys/resource.h>  /* setrlimit() */
40 #endif
41 
42 #ifdef __clang__
43 # pragma clang diagnostic ignored "-Wvariadic-macros"
44 # pragma clang diagnostic ignored "-Wc99-extensions"
45 #endif
46 
47 #ifdef __GNUC__
48 # pragma GCC diagnostic ignored "-Wvariadic-macros"
49 #endif
50 
51 #define TEST_PORT 9123
52 #define TEST_PORT_2 9124
53 
54 #ifdef _WIN32
55 # define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
56 # define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
57 # define TEST_PIPENAME_3 "\\\\?\\pipe\\uv-test3"
58 #else
59 # define TEST_PIPENAME "/tmp/uv-test-sock"
60 # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
61 # define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
62 #endif
63 
64 #ifdef _WIN32
65 # include <io.h>
66 # ifndef S_IRUSR
67 #  define S_IRUSR _S_IREAD
68 # endif
69 # ifndef S_IWUSR
70 #  define S_IWUSR _S_IWRITE
71 # endif
72 #endif
73 
74 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
75 
76 #define container_of(ptr, type, member) \
77   ((type *) ((char *) (ptr) - offsetof(type, member)))
78 
79 typedef enum {
80   TCP = 0,
81   UDP,
82   PIPE
83 } stream_type;
84 
85 /* Die with fatal error. */
86 #define FATAL(msg)                                        \
87   do {                                                    \
88     fprintf(stderr,                                       \
89             "Fatal error in %s on line %d: %s\n",         \
90             __FILE__,                                     \
91             __LINE__,                                     \
92             msg);                                         \
93     fflush(stderr);                                       \
94     abort();                                              \
95   } while (0)
96 
97 /* Have our own assert, so we are sure it does not get optimized away in
98  * a release build.
99  */
100 #define ASSERT(expr)                                      \
101  do {                                                     \
102   if (!(expr)) {                                          \
103     fprintf(stderr,                                       \
104             "Assertion failed in %s on line %d: %s\n",    \
105             __FILE__,                                     \
106             __LINE__,                                     \
107             #expr);                                       \
108     abort();                                              \
109   }                                                       \
110  } while (0)
111 
112 /* This macro cleans up the main loop. This is used to avoid valgrind
113  * warnings about memory being "leaked" by the main event loop.
114  */
115 #define MAKE_VALGRIND_HAPPY()                       \
116   do {                                              \
117     close_loop(uv_default_loop());                  \
118     ASSERT(0 == uv_loop_close(uv_default_loop()));  \
119   } while (0)
120 
121 /* Just sugar for wrapping the main() for a task or helper. */
122 #define TEST_IMPL(name)                                                       \
123   int run_test_##name(void);                                                  \
124   int run_test_##name(void)
125 
126 #define BENCHMARK_IMPL(name)                                                  \
127   int run_benchmark_##name(void);                                             \
128   int run_benchmark_##name(void)
129 
130 #define HELPER_IMPL(name)                                                     \
131   int run_helper_##name(void);                                                \
132   int run_helper_##name(void)
133 
134 /* Format big numbers nicely. WARNING: leaks memory. */
135 const char* fmt(double d);
136 
137 /* Reserved test exit codes. */
138 enum test_status {
139   TEST_OK = 0,
140   TEST_SKIP
141 };
142 
143 #define RETURN_OK()                                                           \
144   do {                                                                        \
145     return TEST_OK;                                                           \
146   } while (0)
147 
148 #define RETURN_SKIP(explanation)                                              \
149   do {                                                                        \
150     fprintf(stderr, "%s\n", explanation);                                     \
151     fflush(stderr);                                                           \
152     return TEST_SKIP;                                                         \
153   } while (0)
154 
155 #if !defined(_WIN32)
156 
157 # define TEST_FILE_LIMIT(num)                                                 \
158     do {                                                                      \
159       struct rlimit lim;                                                      \
160       lim.rlim_cur = (num);                                                   \
161       lim.rlim_max = lim.rlim_cur;                                            \
162       if (setrlimit(RLIMIT_NOFILE, &lim))                                     \
163         RETURN_SKIP("File descriptor limit too low.");                        \
164     } while (0)
165 
166 #else  /* defined(_WIN32) */
167 
168 # define TEST_FILE_LIMIT(num) do {} while (0)
169 
170 #endif
171 
172 #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
173 extern int snprintf(char*, size_t, const char*, ...);
174 #endif
175 
176 #if defined(__clang__) ||                                \
177     defined(__GNUC__) ||                                 \
178     defined(__INTEL_COMPILER)
179 # define UNUSED __attribute__((unused))
180 #else
181 # define UNUSED
182 #endif
183 
184 #if defined(_WIN32)
185 #define notify_parent_process() ((void) 0)
186 #else
187 extern void notify_parent_process(void);
188 #endif
189 
190 /* Fully close a loop */
close_walk_cb(uv_handle_t * handle,void * arg)191 static void close_walk_cb(uv_handle_t* handle, void* arg) {
192   if (!uv_is_closing(handle))
193     uv_close(handle, NULL);
194 }
195 
close_loop(uv_loop_t * loop)196 UNUSED static void close_loop(uv_loop_t* loop) {
197   uv_walk(loop, close_walk_cb, NULL);
198   uv_run(loop, UV_RUN_DEFAULT);
199 }
200 
can_ipv6(void)201 UNUSED static int can_ipv6(void) {
202   uv_interface_address_t* addr;
203   int supported;
204   int count;
205   int i;
206 
207   if (uv_interface_addresses(&addr, &count))
208     return 0;  /* Assume no IPv6 support on failure. */
209 
210   supported = 0;
211   for (i = 0; supported == 0 && i < count; i += 1)
212     supported = (AF_INET6 == addr[i].address.address6.sin6_family);
213 
214   uv_free_interface_addresses(addr, count);
215   return supported;
216 }
217 
218 #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
219 # define NO_FS_EVENTS "Filesystem watching not supported on this platform."
220 #endif
221 
222 #if defined(__MSYS__)
223 # define NO_SEND_HANDLE_ON_PIPE \
224   "MSYS2 runtime does not support sending handles on pipes."
225 #elif defined(__CYGWIN__)
226 # define NO_SEND_HANDLE_ON_PIPE \
227   "Cygwin runtime does not support sending handles on pipes."
228 #endif
229 
230 #if defined(__MSYS__)
231 # define NO_SELF_CONNECT \
232   "MSYS2 runtime hangs on listen+connect in same process."
233 #elif defined(__CYGWIN__)
234 # define NO_SELF_CONNECT \
235   "Cygwin runtime hangs on listen+connect in same process."
236 #endif
237 
238 #endif /* TASK_H_ */
239