1
0

task.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <stdio.h>
  24. #include <stddef.h>
  25. #include <stdlib.h>
  26. #if defined(_MSC_VER) && _MSC_VER < 1600
  27. # include "stdint-msvc2008.h"
  28. #else
  29. # include <stdint.h>
  30. #endif
  31. #if !defined(_WIN32)
  32. # include <sys/time.h>
  33. # include <sys/resource.h> /* setrlimit() */
  34. #endif
  35. #define TEST_PORT 9123
  36. #define TEST_PORT_2 9124
  37. #ifdef _WIN32
  38. # define TEST_PIPENAME "\\\\.\\pipe\\uv-test"
  39. # define TEST_PIPENAME_2 "\\\\.\\pipe\\uv-test2"
  40. #else
  41. # define TEST_PIPENAME "/tmp/uv-test-sock"
  42. # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
  43. #endif
  44. #ifdef _WIN32
  45. # include <io.h>
  46. # ifndef S_IRUSR
  47. # define S_IRUSR _S_IREAD
  48. # endif
  49. # ifndef S_IWUSR
  50. # define S_IWUSR _S_IWRITE
  51. # endif
  52. #endif
  53. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  54. #define container_of(ptr, type, member) \
  55. ((type *) ((char *) (ptr) - offsetof(type, member)))
  56. typedef enum {
  57. TCP = 0,
  58. UDP,
  59. PIPE
  60. } stream_type;
  61. /* Log to stderr. */
  62. #define LOG(...) \
  63. do { \
  64. fprintf(stderr, "%s", __VA_ARGS__); \
  65. fflush(stderr); \
  66. } while (0)
  67. #define LOGF(...) \
  68. do { \
  69. fprintf(stderr, __VA_ARGS__); \
  70. fflush(stderr); \
  71. } while (0)
  72. /* Die with fatal error. */
  73. #define FATAL(msg) \
  74. do { \
  75. fprintf(stderr, \
  76. "Fatal error in %s on line %d: %s\n", \
  77. __FILE__, \
  78. __LINE__, \
  79. msg); \
  80. fflush(stderr); \
  81. abort(); \
  82. } while (0)
  83. /* Have our own assert, so we are sure it does not get optimized away in
  84. * a release build.
  85. */
  86. #define ASSERT(expr) \
  87. do { \
  88. if (!(expr)) { \
  89. fprintf(stderr, \
  90. "Assertion failed in %s on line %d: %s\n", \
  91. __FILE__, \
  92. __LINE__, \
  93. #expr); \
  94. abort(); \
  95. } \
  96. } while (0)
  97. /* This macro cleans up the main loop. This is used to avoid valgrind
  98. * warnings about memory being "leaked" by the main event loop.
  99. */
  100. #define MAKE_VALGRIND_HAPPY() \
  101. uv_loop_delete(uv_default_loop())
  102. /* Just sugar for wrapping the main() for a task or helper. */
  103. #define TEST_IMPL(name) \
  104. int run_test_##name(void); \
  105. int run_test_##name(void)
  106. #define BENCHMARK_IMPL(name) \
  107. int run_benchmark_##name(void); \
  108. int run_benchmark_##name(void)
  109. #define HELPER_IMPL(name) \
  110. int run_helper_##name(void); \
  111. int run_helper_##name(void)
  112. /* Pause the calling thread for a number of milliseconds. */
  113. void uv_sleep(int msec);
  114. /* Format big numbers nicely. WARNING: leaks memory. */
  115. const char* fmt(double d);
  116. /* Reserved test exit codes. */
  117. enum test_status {
  118. TEST_OK = 0,
  119. TEST_TODO,
  120. TEST_SKIP
  121. };
  122. #define RETURN_OK() \
  123. do { \
  124. return TEST_OK; \
  125. } while (0)
  126. #define RETURN_TODO(explanation) \
  127. do { \
  128. LOGF("%s\n", explanation); \
  129. return TEST_TODO; \
  130. } while (0)
  131. #define RETURN_SKIP(explanation) \
  132. do { \
  133. LOGF("%s\n", explanation); \
  134. return TEST_SKIP; \
  135. } while (0)
  136. #if !defined(_WIN32)
  137. # define TEST_FILE_LIMIT(num) \
  138. do { \
  139. struct rlimit lim; \
  140. lim.rlim_cur = (num); \
  141. lim.rlim_max = lim.rlim_cur; \
  142. if (setrlimit(RLIMIT_NOFILE, &lim)) \
  143. RETURN_SKIP("File descriptor limit too low."); \
  144. } while (0)
  145. #else /* defined(_WIN32) */
  146. # define TEST_FILE_LIMIT(num) do {} while (0)
  147. #endif
  148. #if defined _WIN32 && ! defined __GNUC__
  149. #include <stdarg.h>
  150. /* Emulate snprintf() on Windows, _snprintf() doesn't zero-terminate the buffer
  151. * on overflow...
  152. */
  153. static int snprintf(char* buf, size_t len, const char* fmt, ...) {
  154. va_list ap;
  155. int n;
  156. va_start(ap, fmt);
  157. n = _vsprintf_p(buf, len, fmt, ap);
  158. va_end(ap);
  159. /* It's a sad fact of life that no one ever checks the return value of
  160. * snprintf(). Zero-terminating the buffer hopefully reduces the risk
  161. * of gaping security holes.
  162. */
  163. if (n < 0)
  164. if (len > 0)
  165. buf[0] = '\0';
  166. return n;
  167. }
  168. #endif
  169. #endif /* TASK_H_ */