task.h
Go to the documentation of this file.
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 #include <string.h>
31 #include <inttypes.h>
32 
33 #if defined(_MSC_VER) && _MSC_VER < 1600
34 # include "uv/stdint-msvc2008.h"
35 #else
36 # include <stdint.h>
37 #endif
38 
39 #if !defined(_WIN32)
40 # include <sys/time.h>
41 # include <sys/resource.h> /* setrlimit() */
42 #endif
43 
44 #ifdef __clang__
45 # pragma clang diagnostic ignored "-Wvariadic-macros"
46 # pragma clang diagnostic ignored "-Wc99-extensions"
47 #endif
48 
49 #ifdef __GNUC__
50 # pragma GCC diagnostic ignored "-Wvariadic-macros"
51 #endif
52 
53 #define TEST_PORT 9123
54 #define TEST_PORT_2 9124
55 
56 #ifdef _WIN32
57 # define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
58 # define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
59 # define TEST_PIPENAME_3 "\\\\?\\pipe\\uv-test3"
60 #else
61 # define TEST_PIPENAME "/tmp/uv-test-sock"
62 # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
63 # define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
64 #endif
65 
66 #ifdef _WIN32
67 # include <io.h>
68 # ifndef S_IRUSR
69 # define S_IRUSR _S_IREAD
70 # endif
71 # ifndef S_IWUSR
72 # define S_IWUSR _S_IWRITE
73 # endif
74 #endif
75 
76 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
77 
78 #define container_of(ptr, type, member) \
79  ((type *) ((char *) (ptr) - offsetof(type, member)))
80 
81 typedef enum {
82  TCP = 0,
83  UDP,
85 } stream_type;
86 
87 /* Die with fatal error. */
88 #define FATAL(msg) \
89  do { \
90  fprintf(stderr, \
91  "Fatal error in %s on line %d: %s\n", \
92  __FILE__, \
93  __LINE__, \
94  msg); \
95  fflush(stderr); \
96  abort(); \
97  } while (0)
98 
99 /* Have our own assert, so we are sure it does not get optimized away in
100  * a release build.
101  */
102 #define ASSERT(expr) \
103  do { \
104  if (!(expr)) { \
105  fprintf(stderr, \
106  "Assertion failed in %s on line %d: %s\n", \
107  __FILE__, \
108  __LINE__, \
109  #expr); \
110  abort(); \
111  } \
112  } while (0)
113 
114 #define ASSERT_BASE(expr, a, operator, b, type, conv) \
115  do { \
116  if (!(expr)) { \
117  fprintf(stderr, \
118  "Assertion failed in %s on line %d: `%s %s %s` " \
119  "(%"conv" %s %"conv")\n", \
120  __FILE__, \
121  __LINE__, \
122  #a, \
123  #operator, \
124  #b, \
125  (type)a, \
126  #operator, \
127  (type)b); \
128  abort(); \
129  } \
130  } while (0)
131 
132 #define ASSERT_BASE_LEN(expr, a, operator, b, conv, len) \
133  do { \
134  if (!(expr)) { \
135  fprintf(stderr, \
136  "Assertion failed in %s on line %d: `%s %s %s` " \
137  "(%.*"#conv" %s %.*"#conv")\n", \
138  __FILE__, \
139  __LINE__, \
140  #a, \
141  #operator, \
142  #b, \
143  (int)len, \
144  a, \
145  #operator, \
146  (int)len, \
147  b); \
148  abort(); \
149  } \
150  } while (0)
151 
152 #define ASSERT_BASE_HEX(expr, a, operator, b, size) \
153  do { \
154  if (!(expr)) { \
155  int i; \
156  unsigned char* a_ = (unsigned char*)a; \
157  unsigned char* b_ = (unsigned char*)b; \
158  fprintf(stderr, \
159  "Assertion failed in %s on line %d: `%s %s %s` (", \
160  __FILE__, \
161  __LINE__, \
162  #a, \
163  #operator, \
164  #b); \
165  for (i = 0; i < size; ++i) { \
166  if (i > 0) fprintf(stderr, ":"); \
167  fprintf(stderr, "%02X", a_[i]); \
168  } \
169  fprintf(stderr, " %s ", #operator); \
170  for (i = 0; i < size; ++i) { \
171  if (i > 0) fprintf(stderr, ":"); \
172  fprintf(stderr, "%02X", b_[i]); \
173  } \
174  fprintf(stderr, ")\n"); \
175  abort(); \
176  } \
177  } while (0)
178 
179 #define ASSERT_INT_BASE(a, operator, b, type, conv) \
180  ASSERT_BASE(a operator b, a, operator, b, type, conv)
181 
182 #define ASSERT_EQ(a, b) ASSERT_INT_BASE(a, ==, b, int64_t, PRId64)
183 #define ASSERT_GE(a, b) ASSERT_INT_BASE(a, >=, b, int64_t, PRId64)
184 #define ASSERT_GT(a, b) ASSERT_INT_BASE(a, >, b, int64_t, PRId64)
185 #define ASSERT_LE(a, b) ASSERT_INT_BASE(a, <=, b, int64_t, PRId64)
186 #define ASSERT_LT(a, b) ASSERT_INT_BASE(a, <, b, int64_t, PRId64)
187 #define ASSERT_NE(a, b) ASSERT_INT_BASE(a, !=, b, int64_t, PRId64)
188 
189 #define ASSERT_UINT64_EQ(a, b) ASSERT_INT_BASE(a, ==, b, uint64_t, PRIu64)
190 #define ASSERT_UINT64_GE(a, b) ASSERT_INT_BASE(a, >=, b, uint64_t, PRIu64)
191 #define ASSERT_UINT64_GT(a, b) ASSERT_INT_BASE(a, >, b, uint64_t, PRIu64)
192 #define ASSERT_UINT64_LE(a, b) ASSERT_INT_BASE(a, <=, b, uint64_t, PRIu64)
193 #define ASSERT_UINT64_LT(a, b) ASSERT_INT_BASE(a, <, b, uint64_t, PRIu64)
194 #define ASSERT_UINT64_NE(a, b) ASSERT_INT_BASE(a, !=, b, uint64_t, PRIu64)
195 
196 #define ASSERT_STR_EQ(a, b) \
197  ASSERT_BASE(strcmp(a, b) == 0, a, ==, b, char*, "s")
198 
199 #define ASSERT_STR_NE(a, b) \
200  ASSERT_BASE(strcmp(a, b) != 0, a, !=, b, char*, "s")
201 
202 #define ASSERT_MEM_EQ(a, b, size) \
203  ASSERT_BASE_LEN(memcmp(a, b, size) == 0, a, ==, b, s, size)
204 
205 #define ASSERT_MEM_NE(a, b, size) \
206  ASSERT_BASE_LEN(memcmp(a, b, size) != 0, a, !=, b, s, size)
207 
208 #define ASSERT_MEM_HEX_EQ(a, b, size) \
209  ASSERT_BASE_HEX(memcmp(a, b, size) == 0, a, ==, b, size)
210 
211 #define ASSERT_MEM_HEX_NE(a, b, size) \
212  ASSERT_BASE_HEX(memcmp(a, b, size) != 0, a, !=, b, size)
213 
214 #define ASSERT_NULL(a) \
215  ASSERT_BASE(a == NULL, a, ==, NULL, void*, "p")
216 
217 #define ASSERT_NOT_NULL(a) \
218  ASSERT_BASE(a != NULL, a, !=, NULL, void*, "p")
219 
220 #define ASSERT_PTR_EQ(a, b) \
221  ASSERT_BASE((void*)a == (void*)b, a, ==, b, void*, "p")
222 
223 #define ASSERT_PTR_NE(a, b) \
224  ASSERT_BASE((void*)a != (void*)b, a, !=, b, void*, "p")
225 
226 /* This macro cleans up the main loop. This is used to avoid valgrind
227  * warnings about memory being "leaked" by the main event loop.
228  */
229 #define MAKE_VALGRIND_HAPPY() \
230  do { \
231  close_loop(uv_default_loop()); \
232  ASSERT(0 == uv_loop_close(uv_default_loop())); \
233  } while (0)
234 
235 /* Just sugar for wrapping the main() for a task or helper. */
236 #define TEST_IMPL(name) \
237  int run_test_##name(void); \
238  int run_test_##name(void)
239 
240 #define BENCHMARK_IMPL(name) \
241  int run_benchmark_##name(void); \
242  int run_benchmark_##name(void)
243 
244 #define HELPER_IMPL(name) \
245  int run_helper_##name(void); \
246  int run_helper_##name(void)
247 
248 /* Format big numbers nicely. WARNING: leaks memory. */
249 const char* fmt(double d);
250 
251 /* Reserved test exit codes. */
253  TEST_OK = 0,
255 };
256 
257 #define RETURN_OK() \
258  do { \
259  return TEST_OK; \
260  } while (0)
261 
262 #define RETURN_SKIP(explanation) \
263  do { \
264  fprintf(stderr, "%s\n", explanation); \
265  fflush(stderr); \
266  return TEST_SKIP; \
267  } while (0)
268 
269 #if !defined(_WIN32)
270 
271 # define TEST_FILE_LIMIT(num) \
272  do { \
273  struct rlimit lim; \
274  lim.rlim_cur = (num); \
275  lim.rlim_max = lim.rlim_cur; \
276  if (setrlimit(RLIMIT_NOFILE, &lim)) \
277  RETURN_SKIP("File descriptor limit too low."); \
278  } while (0)
279 
280 #else /* defined(_WIN32) */
281 
282 # define TEST_FILE_LIMIT(num) do {} while (0)
283 
284 #endif
285 
286 #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
287 extern int snprintf(char*, size_t, const char*, ...);
288 #endif
289 
290 #if defined(__clang__) || \
291  defined(__GNUC__) || \
292  defined(__INTEL_COMPILER)
293 # define UNUSED __attribute__((unused))
294 #else
295 # define UNUSED
296 #endif
297 
298 #if defined(_WIN32)
299 #define notify_parent_process() ((void) 0)
300 #else
301 extern void notify_parent_process(void);
302 #endif
303 
304 /* Fully close a loop */
305 static void close_walk_cb(uv_handle_t* handle, void* arg) {
306  if (!uv_is_closing(handle))
307  uv_close(handle, NULL);
308 }
309 
311  uv_walk(loop, close_walk_cb, NULL);
313 }
314 
315 UNUSED static int can_ipv6(void) {
317  int supported;
318  int count;
319  int i;
320 
322  return 0; /* Assume no IPv6 support on failure. */
323 
324  supported = 0;
325  for (i = 0; supported == 0 && i < count; i += 1)
326  supported = (AF_INET6 == addr[i].address.address6.sin6_family);
327 
329  return supported;
330 }
331 
332 #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
333 # define NO_FS_EVENTS "Filesystem watching not supported on this platform."
334 #endif
335 
336 #if defined(__MSYS__)
337 # define NO_SEND_HANDLE_ON_PIPE \
338  "MSYS2 runtime does not support sending handles on pipes."
339 #elif defined(__CYGWIN__)
340 # define NO_SEND_HANDLE_ON_PIPE \
341  "Cygwin runtime does not support sending handles on pipes."
342 #endif
343 
344 #if defined(__MSYS__)
345 # define NO_SELF_CONNECT \
346  "MSYS2 runtime hangs on listen+connect in same process."
347 #elif defined(__CYGWIN__)
348 # define NO_SELF_CONNECT \
349  "Cygwin runtime hangs on listen+connect in same process."
350 #endif
351 
352 #endif /* TASK_H_ */
async_greeter_server_with_graceful_shutdown.loop
loop
Definition: async_greeter_server_with_graceful_shutdown.py:59
AF_INET6
#define AF_INET6
Definition: ares_setup.h:208
close_loop
static UNUSED void close_loop(uv_loop_t *loop)
Definition: task.h:310
string.h
UNUSED
#define UNUSED
Definition: task.h:295
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_interface_addresses
UV_EXTERN int uv_interface_addresses(uv_interface_address_t **addresses, int *count)
Definition: aix.c:1042
uv_is_closing
UV_EXTERN int uv_is_closing(const uv_handle_t *handle)
Definition: unix/core.c:319
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_walk
UV_EXTERN void uv_walk(uv_loop_t *loop, uv_walk_cb walk_cb, void *arg)
Definition: uv-common.c:456
uv_free_interface_addresses
UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t *addresses, int count)
Definition: aix.c:1210
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
fmt
const char * fmt(double d)
Definition: runner.c:40
UDP
@ UDP
Definition: task.h:83
arg
Definition: cmdline.cc:40
TCP
@ TCP
Definition: task.h:82
test_status
test_status
Definition: task.h:252
d
static const fe d
Definition: curve25519_tables.h:19
notify_parent_process
void notify_parent_process(void)
Definition: runner-unix.c:54
stdint.h
close_walk_cb
static void close_walk_cb(uv_handle_t *handle, void *arg)
Definition: task.h:305
uv_interface_address_s
Definition: uv.h:1085
uv.h
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
PIPE
@ PIPE
Definition: task.h:84
TEST_OK
@ TEST_OK
Definition: task.h:253
TEST_SKIP
@ TEST_SKIP
Definition: task.h:254
can_ipv6
static UNUSED int can_ipv6(void)
Definition: task.h:315
stream_type
stream_type
Definition: task.h:81
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
stdint-msvc2008.h
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:29