task.h 13 KB

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