Home | History | Annotate | Line # | Download | only in test
task.h revision 1.1.1.1
      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,
     84   PIPE
     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     uv_library_shutdown();                          \
    234   } while (0)
    235 
    236 /* Just sugar for wrapping the main() for a task or helper. */
    237 #define TEST_IMPL(name)                                                       \
    238   int run_test_##name(void);                                                  \
    239   int run_test_##name(void)
    240 
    241 #define BENCHMARK_IMPL(name)                                                  \
    242   int run_benchmark_##name(void);                                             \
    243   int run_benchmark_##name(void)
    244 
    245 #define HELPER_IMPL(name)                                                     \
    246   int run_helper_##name(void);                                                \
    247   int run_helper_##name(void)
    248 
    249 /* Format big numbers nicely. WARNING: leaks memory. */
    250 const char* fmt(double d);
    251 
    252 /* Reserved test exit codes. */
    253 enum test_status {
    254   TEST_OK = 0,
    255   TEST_SKIP
    256 };
    257 
    258 #define RETURN_OK()                                                           \
    259   do {                                                                        \
    260     return TEST_OK;                                                           \
    261   } while (0)
    262 
    263 #define RETURN_SKIP(explanation)                                              \
    264   do {                                                                        \
    265     fprintf(stderr, "%s\n", explanation);                                     \
    266     fflush(stderr);                                                           \
    267     return TEST_SKIP;                                                         \
    268   } while (0)
    269 
    270 #if !defined(_WIN32)
    271 
    272 # define TEST_FILE_LIMIT(num)                                                 \
    273     do {                                                                      \
    274       struct rlimit lim;                                                      \
    275       lim.rlim_cur = (num);                                                   \
    276       lim.rlim_max = lim.rlim_cur;                                            \
    277       if (setrlimit(RLIMIT_NOFILE, &lim))                                     \
    278         RETURN_SKIP("File descriptor limit too low.");                        \
    279     } while (0)
    280 
    281 #else  /* defined(_WIN32) */
    282 
    283 # define TEST_FILE_LIMIT(num) do {} while (0)
    284 
    285 #endif
    286 
    287 #if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
    288 extern int snprintf(char*, size_t, const char*, ...);
    289 #endif
    290 
    291 #if defined(__clang__) ||                                \
    292     defined(__GNUC__) ||                                 \
    293     defined(__INTEL_COMPILER)
    294 # define UNUSED __attribute__((unused))
    295 #else
    296 # define UNUSED
    297 #endif
    298 
    299 #if defined(_WIN32)
    300 #define notify_parent_process() ((void) 0)
    301 #else
    302 extern void notify_parent_process(void);
    303 #endif
    304 
    305 /* Fully close a loop */
    306 static void close_walk_cb(uv_handle_t* handle, void* arg) {
    307   if (!uv_is_closing(handle))
    308     uv_close(handle, NULL);
    309 }
    310 
    311 UNUSED static void close_loop(uv_loop_t* loop) {
    312   uv_walk(loop, close_walk_cb, NULL);
    313   uv_run(loop, UV_RUN_DEFAULT);
    314 }
    315 
    316 UNUSED static int can_ipv6(void) {
    317   uv_interface_address_t* addr;
    318   int supported;
    319   int count;
    320   int i;
    321 
    322   if (uv_interface_addresses(&addr, &count))
    323     return 0;  /* Assume no IPv6 support on failure. */
    324 
    325   supported = 0;
    326   for (i = 0; supported == 0 && i < count; i += 1)
    327     supported = (AF_INET6 == addr[i].address.address6.sin6_family);
    328 
    329   uv_free_interface_addresses(addr, count);
    330   return supported;
    331 }
    332 
    333 #if defined(__CYGWIN__) || defined(__MSYS__) || defined(__PASE__)
    334 # define NO_FS_EVENTS "Filesystem watching not supported on this platform."
    335 #endif
    336 
    337 #if defined(__MSYS__)
    338 # define NO_SEND_HANDLE_ON_PIPE \
    339   "MSYS2 runtime does not support sending handles on pipes."
    340 #elif defined(__CYGWIN__)
    341 # define NO_SEND_HANDLE_ON_PIPE \
    342   "Cygwin runtime does not support sending handles on pipes."
    343 #endif
    344 
    345 #if defined(__MSYS__)
    346 # define NO_SELF_CONNECT \
    347   "MSYS2 runtime hangs on listen+connect in same process."
    348 #elif defined(__CYGWIN__)
    349 # define NO_SELF_CONNECT \
    350   "Cygwin runtime hangs on listen+connect in same process."
    351 #endif
    352 
    353 #endif /* TASK_H_ */
    354