Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 233 lines (190 sloc) 7.743 kB
cf4fbc0 @ry Add copyright headers, license file.
ry authored
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
5275b03 @piscisaureus Benchmark runner
piscisaureus authored
22 #ifndef TASK_H_
23 #define TASK_H_
24
e4f28ad @saghul test: make sure all handles in the loop are closed
saghul authored
25 #include "uv.h"
26
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
27 #include <stdio.h>
1161d31 @bnoordhuis test: move container_of() macro into task.h
bnoordhuis authored
28 #include <stddef.h>
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
29 #include <stdlib.h>
30
61ecb34 @saghul win: support compilation with Visual Studio 2008
saghul authored
31 #if defined(_MSC_VER) && _MSC_VER < 1600
ddd7e04 @bnoordhuis build: switch to autotools
bnoordhuis authored
32 # include "stdint-msvc2008.h"
61ecb34 @saghul win: support compilation with Visual Studio 2008
saghul authored
33 #else
34 # include <stdint.h>
35 #endif
36
86b5c1e @indutny test: test fsevents error reporting
indutny authored
37 #if !defined(_WIN32)
38 # include <sys/time.h>
39 # include <sys/resource.h> /* setrlimit() */
40 #endif
41
452681f @ry fix port conflict
ry authored
42 #define TEST_PORT 9123
43 #define TEST_PORT_2 9124
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
44
b6a6dae Named pipes implementation for Windows
Igor Zinkovsky authored
45 #ifdef _WIN32
5ac214c @saghul unix, windows: add uv_pipe_getsockname
saghul authored
46 # define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
47 # define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
b6a6dae Named pipes implementation for Windows
Igor Zinkovsky authored
48 #else
4eff34d @bnoordhuis uv-unix: pipes API implementation
bnoordhuis authored
49 # define TEST_PIPENAME "/tmp/uv-test-sock"
afc9987 Add uv_pipe_bind tests
Igor Zinkovsky authored
50 # define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
b6a6dae Named pipes implementation for Windows
Igor Zinkovsky authored
51 #endif
52
ede8318 @bnoordhuis test: don't use S_IREAD and S_IWRITE
bnoordhuis authored
53 #ifdef _WIN32
54 # include <io.h>
55 # ifndef S_IRUSR
56 # define S_IRUSR _S_IREAD
57 # endif
58 # ifndef S_IWUSR
59 # define S_IWUSR _S_IWRITE
60 # endif
61 #endif
62
fbbc085 @bnoordhuis Rename COUNTOF() to ARRAY_SIZE().
bnoordhuis authored
63 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
54982a2 windows: stdio over non-overlapped pipes
Igor Zinkovsky authored
64
1161d31 @bnoordhuis test: move container_of() macro into task.h
bnoordhuis authored
65 #define container_of(ptr, type, member) \
66 ((type *) ((char *) (ptr) - offsetof(type, member)))
67
b6a6dae Named pipes implementation for Windows
Igor Zinkovsky authored
68 typedef enum {
69 TCP = 0,
dc3b80a @bnoordhuis test: add udp4_echo_server helper
bnoordhuis authored
70 UDP,
b6a6dae Named pipes implementation for Windows
Igor Zinkovsky authored
71 PIPE
72 } stream_type;
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
73
74 /* Log to stderr. */
6c8acb0 @bnoordhuis task: flush stderr after printing
bnoordhuis authored
75 #define LOG(...) \
76 do { \
77 fprintf(stderr, "%s", __VA_ARGS__); \
78 fflush(stderr); \
79 } while (0)
80
81 #define LOGF(...) \
82 do { \
83 fprintf(stderr, __VA_ARGS__); \
84 fflush(stderr); \
85 } while (0)
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
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__, \
59f1ce0 @ry Macro styling for ASSERT and FATAL.
ry authored
94 msg); \
6c8acb0 @bnoordhuis task: flush stderr after printing
bnoordhuis authored
95 fflush(stderr); \
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
96 abort(); \
59f1ce0 @ry Macro styling for ASSERT and FATAL.
ry authored
97 } while (0)
ad94c92 @piscisaureus First shot at test harness
piscisaureus authored
98
d4c5e59 @piscisaureus Define API for test threading helpers
piscisaureus authored
99 /* Have our own assert, so we are sure it does not get optimized away in
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
100 * a release build.
101 */
102 #define ASSERT(expr) \
59f1ce0 @ry Macro styling for ASSERT and FATAL.
ry authored
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)
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
113
47eb034 @piscisaureus test: move loop cleanup code to the individual tests
piscisaureus authored
114 /* This macro cleans up the main loop. This is used to avoid valgrind
115 * warnings about memory being "leaked" by the main event loop.
116 */
e4f28ad @saghul test: make sure all handles in the loop are closed
saghul authored
117 #define MAKE_VALGRIND_HAPPY() \
118 do { \
119 close_loop(uv_default_loop()); \
120 uv_loop_delete(uv_default_loop()); \
121 } while (0)
93dd5f7 @piscisaureus Clean up test error handling
piscisaureus authored
122
53f9d5a @piscisaureus Allow tests and benchmarks to use the same helpers.
piscisaureus authored
123 /* Just sugar for wrapping the main() for a task or helper. */
7ff6f29 @bnoordhuis test, bench: ANSI-fy function prototypes
bnoordhuis authored
124 #define TEST_IMPL(name) \
125 int run_test_##name(void); \
126 int run_test_##name(void)
ad94c92 @piscisaureus First shot at test harness
piscisaureus authored
127
7ff6f29 @bnoordhuis test, bench: ANSI-fy function prototypes
bnoordhuis authored
128 #define BENCHMARK_IMPL(name) \
129 int run_benchmark_##name(void); \
130 int run_benchmark_##name(void)
53f9d5a @piscisaureus Allow tests and benchmarks to use the same helpers.
piscisaureus authored
131
7ff6f29 @bnoordhuis test, bench: ANSI-fy function prototypes
bnoordhuis authored
132 #define HELPER_IMPL(name) \
133 int run_helper_##name(void); \
134 int run_helper_##name(void)
d4c5e59 @piscisaureus Define API for test threading helpers
piscisaureus authored
135
136 /* Pause the calling thread for a number of milliseconds. */
2ef3c6c @ry oio -> uv
ry authored
137 void uv_sleep(int msec);
d4c5e59 @piscisaureus Define API for test threading helpers
piscisaureus authored
138
123ca8b @bnoordhuis test: make fmt() function global
bnoordhuis authored
139 /* Format big numbers nicely. WARNING: leaks memory. */
140 const char* fmt(double d);
141
2c21050 @bajtos test: add RETURN_SKIP and RETURN_TODO macros
bajtos authored
142 /* Reserved test exit codes. */
143 enum test_status {
144 TEST_OK = 0,
145 TEST_TODO,
146 TEST_SKIP
147 };
148
149 #define RETURN_OK() \
150 do { \
151 return TEST_OK; \
152 } while (0)
153
154 #define RETURN_TODO(explanation) \
155 do { \
156 LOGF("%s\n", explanation); \
157 return TEST_TODO; \
158 } while (0)
159
160 #define RETURN_SKIP(explanation) \
161 do { \
162 LOGF("%s\n", explanation); \
163 return TEST_SKIP; \
164 } while (0)
165
86b5c1e @indutny test: test fsevents error reporting
indutny authored
166 #if !defined(_WIN32)
167
168 # define TEST_FILE_LIMIT(num) \
169 do { \
170 struct rlimit lim; \
171 lim.rlim_cur = (num); \
172 lim.rlim_max = lim.rlim_cur; \
173 if (setrlimit(RLIMIT_NOFILE, &lim)) \
174 RETURN_SKIP("File descriptor limit too low."); \
175 } while (0)
176
177 #else /* defined(_WIN32) */
178
179 # define TEST_FILE_LIMIT(num) do {} while (0)
180
181 #endif
182
183
ee434b3 @rioki test: remove replacement snprintf for mingw
rioki authored
184 #if defined _WIN32 && ! defined __GNUC__
fd08290 @bnoordhuis test: add windows-only snprintf() function
bnoordhuis authored
185
186 #include <stdarg.h>
187
188 /* Emulate snprintf() on Windows, _snprintf() doesn't zero-terminate the buffer
189 * on overflow...
190 */
191 static int snprintf(char* buf, size_t len, const char* fmt, ...) {
192 va_list ap;
193 int n;
194
195 va_start(ap, fmt);
196 n = _vsprintf_p(buf, len, fmt, ap);
197 va_end(ap);
198
199 /* It's a sad fact of life that no one ever checks the return value of
200 * snprintf(). Zero-terminating the buffer hopefully reduces the risk
201 * of gaping security holes.
202 */
203 if (n < 0)
204 if (len > 0)
205 buf[0] = '\0';
206
207 return n;
208 }
209
210 #endif
211
0e59136 @saghul test: fix compiation warning
saghul authored
212 #if defined(__clang__) || \
213 defined(__GNUC__) || \
214 defined(__INTEL_COMPILER) || \
215 defined(__SUNPRO_C)
216 # define UNUSED __attribute__((unused))
217 #else
218 # define UNUSED
219 #endif
220
e4f28ad @saghul test: make sure all handles in the loop are closed
saghul authored
221 /* Fully close a loop */
222 static void close_walk_cb(uv_handle_t* handle, void* arg) {
223 if (!uv_is_closing(handle))
224 uv_close(handle, NULL);
225 }
226
0e59136 @saghul test: fix compiation warning
saghul authored
227 UNUSED static void close_loop(uv_loop_t* loop) {
e4f28ad @saghul test: make sure all handles in the loop are closed
saghul authored
228 uv_walk(loop, close_walk_cb, NULL);
229 uv_run(loop, UV_RUN_DEFAULT);
230 }
231
5275b03 @piscisaureus Benchmark runner
piscisaureus authored
232 #endif /* TASK_H_ */
Something went wrong with that request. Please try again.