task.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "uv-private/stdint-msvc2008.h"
  28. #else
  29. # include <stdint.h>
  30. #endif
  31. #define TEST_PORT 9123
  32. #define TEST_PORT_2 9124
  33. #ifdef _WIN32
  34. # define TEST_PIPENAME "\\\\.\\pipe\\uv-test"
  35. # define TEST_PIPENAME_2 "\\\\.\\pipe\\uv-test2"
  36. #else
  37. # define TEST_PIPENAME "/tmp/uv-test-sock"
  38. # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
  39. #endif
  40. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  41. #define container_of(ptr, type, member) \
  42. ((type *) ((char *) (ptr) - offsetof(type, member)))
  43. typedef enum {
  44. TCP = 0,
  45. UDP,
  46. PIPE
  47. } stream_type;
  48. /* Log to stderr. */
  49. #define LOG(...) \
  50. do { \
  51. fprintf(stderr, "%s", __VA_ARGS__); \
  52. fflush(stderr); \
  53. } while (0)
  54. #define LOGF(...) \
  55. do { \
  56. fprintf(stderr, __VA_ARGS__); \
  57. fflush(stderr); \
  58. } while (0)
  59. /* Die with fatal error. */
  60. #define FATAL(msg) \
  61. do { \
  62. fprintf(stderr, \
  63. "Fatal error in %s on line %d: %s\n", \
  64. __FILE__, \
  65. __LINE__, \
  66. msg); \
  67. fflush(stderr); \
  68. abort(); \
  69. } while (0)
  70. /* Have our own assert, so we are sure it does not get optimized away in
  71. * a release build.
  72. */
  73. #define ASSERT(expr) \
  74. do { \
  75. if (!(expr)) { \
  76. fprintf(stderr, \
  77. "Assertion failed in %s on line %d: %s\n", \
  78. __FILE__, \
  79. __LINE__, \
  80. #expr); \
  81. abort(); \
  82. } \
  83. } while (0)
  84. /* This macro cleans up the main loop. This is used to avoid valgrind
  85. * warnings about memory being "leaked" by the main event loop.
  86. */
  87. #define MAKE_VALGRIND_HAPPY() \
  88. uv_loop_delete(uv_default_loop())
  89. /* Just sugar for wrapping the main() for a task or helper. */
  90. #define TEST_IMPL(name) \
  91. int run_test_##name(void); \
  92. int run_test_##name(void)
  93. #define BENCHMARK_IMPL(name) \
  94. int run_benchmark_##name(void); \
  95. int run_benchmark_##name(void)
  96. #define HELPER_IMPL(name) \
  97. int run_helper_##name(void); \
  98. int run_helper_##name(void)
  99. /* Pause the calling thread for a number of milliseconds. */
  100. void uv_sleep(int msec);
  101. /* Format big numbers nicely. WARNING: leaks memory. */
  102. const char* fmt(double d);
  103. /* Reserved test exit codes. */
  104. enum test_status {
  105. TEST_OK = 0,
  106. TEST_TODO,
  107. TEST_SKIP
  108. };
  109. #define RETURN_OK() \
  110. do { \
  111. return TEST_OK; \
  112. } while (0)
  113. #define RETURN_TODO(explanation) \
  114. do { \
  115. LOGF("%s\n", explanation); \
  116. return TEST_TODO; \
  117. } while (0)
  118. #define RETURN_SKIP(explanation) \
  119. do { \
  120. LOGF("%s\n", explanation); \
  121. return TEST_SKIP; \
  122. } while (0)
  123. #endif /* TASK_H_ */