test-threadpool-cancel.c revision 1.1 1 1.1 christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 1.1 christos *
3 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy
4 1.1 christos * of this software and associated documentation files (the "Software"), to
5 1.1 christos * deal in the Software without restriction, including without limitation the
6 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is
8 1.1 christos * furnished to do so, subject to the following conditions:
9 1.1 christos *
10 1.1 christos * The above copyright notice and this permission notice shall be included in
11 1.1 christos * all copies or substantial portions of the Software.
12 1.1 christos *
13 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 1.1 christos * IN THE SOFTWARE.
20 1.1 christos */
21 1.1 christos
22 1.1 christos #include "uv.h"
23 1.1 christos #include "task.h"
24 1.1 christos
25 1.1 christos #define INIT_CANCEL_INFO(ci, what) \
26 1.1 christos do { \
27 1.1 christos (ci)->reqs = (what); \
28 1.1 christos (ci)->nreqs = ARRAY_SIZE(what); \
29 1.1 christos (ci)->stride = sizeof((what)[0]); \
30 1.1 christos } \
31 1.1 christos while (0)
32 1.1 christos
33 1.1 christos struct cancel_info {
34 1.1 christos void* reqs;
35 1.1 christos unsigned nreqs;
36 1.1 christos unsigned stride;
37 1.1 christos uv_timer_t timer_handle;
38 1.1 christos };
39 1.1 christos
40 1.1 christos struct random_info {
41 1.1 christos uv_random_t random_req;
42 1.1 christos char buf[1];
43 1.1 christos };
44 1.1 christos
45 1.1 christos static unsigned fs_cb_called;
46 1.1 christos static unsigned done_cb_called;
47 1.1 christos static unsigned done2_cb_called;
48 1.1 christos static unsigned timer_cb_called;
49 1.1 christos static uv_work_t pause_reqs[4];
50 1.1 christos static uv_sem_t pause_sems[ARRAY_SIZE(pause_reqs)];
51 1.1 christos
52 1.1 christos
53 1.1 christos static void work_cb(uv_work_t* req) {
54 1.1 christos uv_sem_wait(pause_sems + (req - pause_reqs));
55 1.1 christos }
56 1.1 christos
57 1.1 christos
58 1.1 christos static void done_cb(uv_work_t* req, int status) {
59 1.1 christos uv_sem_destroy(pause_sems + (req - pause_reqs));
60 1.1 christos }
61 1.1 christos
62 1.1 christos
63 1.1 christos static void saturate_threadpool(void) {
64 1.1 christos uv_loop_t* loop;
65 1.1 christos char buf[64];
66 1.1 christos size_t i;
67 1.1 christos
68 1.1 christos snprintf(buf,
69 1.1 christos sizeof(buf),
70 1.1 christos "UV_THREADPOOL_SIZE=%lu",
71 1.1 christos (unsigned long)ARRAY_SIZE(pause_reqs));
72 1.1 christos putenv(buf);
73 1.1 christos
74 1.1 christos loop = uv_default_loop();
75 1.1 christos for (i = 0; i < ARRAY_SIZE(pause_reqs); i += 1) {
76 1.1 christos ASSERT(0 == uv_sem_init(pause_sems + i, 0));
77 1.1 christos ASSERT(0 == uv_queue_work(loop, pause_reqs + i, work_cb, done_cb));
78 1.1 christos }
79 1.1 christos }
80 1.1 christos
81 1.1 christos
82 1.1 christos static void unblock_threadpool(void) {
83 1.1 christos size_t i;
84 1.1 christos
85 1.1 christos for (i = 0; i < ARRAY_SIZE(pause_reqs); i += 1)
86 1.1 christos uv_sem_post(pause_sems + i);
87 1.1 christos }
88 1.1 christos
89 1.1 christos
90 1.1 christos static void fs_cb(uv_fs_t* req) {
91 1.1 christos ASSERT(req->result == UV_ECANCELED);
92 1.1 christos uv_fs_req_cleanup(req);
93 1.1 christos fs_cb_called++;
94 1.1 christos }
95 1.1 christos
96 1.1 christos
97 1.1 christos static void getaddrinfo_cb(uv_getaddrinfo_t* req,
98 1.1 christos int status,
99 1.1 christos struct addrinfo* res) {
100 1.1 christos ASSERT(status == UV_EAI_CANCELED);
101 1.1 christos ASSERT(res == NULL);
102 1.1 christos uv_freeaddrinfo(res); /* Should not crash. */
103 1.1 christos }
104 1.1 christos
105 1.1 christos
106 1.1 christos static void getnameinfo_cb(uv_getnameinfo_t* handle,
107 1.1 christos int status,
108 1.1 christos const char* hostname,
109 1.1 christos const char* service) {
110 1.1 christos ASSERT(status == UV_EAI_CANCELED);
111 1.1 christos ASSERT(hostname == NULL);
112 1.1 christos ASSERT(service == NULL);
113 1.1 christos }
114 1.1 christos
115 1.1 christos
116 1.1 christos static void work2_cb(uv_work_t* req) {
117 1.1 christos ASSERT(0 && "work2_cb called");
118 1.1 christos }
119 1.1 christos
120 1.1 christos
121 1.1 christos static void done2_cb(uv_work_t* req, int status) {
122 1.1 christos ASSERT(status == UV_ECANCELED);
123 1.1 christos done2_cb_called++;
124 1.1 christos }
125 1.1 christos
126 1.1 christos
127 1.1 christos static void timer_cb(uv_timer_t* handle) {
128 1.1 christos struct cancel_info* ci;
129 1.1 christos uv_req_t* req;
130 1.1 christos unsigned i;
131 1.1 christos
132 1.1 christos ci = container_of(handle, struct cancel_info, timer_handle);
133 1.1 christos
134 1.1 christos for (i = 0; i < ci->nreqs; i++) {
135 1.1 christos req = (uv_req_t*) ((char*) ci->reqs + i * ci->stride);
136 1.1 christos ASSERT(0 == uv_cancel(req));
137 1.1 christos }
138 1.1 christos
139 1.1 christos uv_close((uv_handle_t*) &ci->timer_handle, NULL);
140 1.1 christos unblock_threadpool();
141 1.1 christos timer_cb_called++;
142 1.1 christos }
143 1.1 christos
144 1.1 christos
145 1.1 christos static void nop_done_cb(uv_work_t* req, int status) {
146 1.1 christos ASSERT(status == UV_ECANCELED);
147 1.1 christos done_cb_called++;
148 1.1 christos }
149 1.1 christos
150 1.1 christos
151 1.1 christos static void nop_random_cb(uv_random_t* req, int status, void* buf, size_t len) {
152 1.1 christos struct random_info* ri;
153 1.1 christos
154 1.1 christos ri = container_of(req, struct random_info, random_req);
155 1.1 christos
156 1.1 christos ASSERT(status == UV_ECANCELED);
157 1.1 christos ASSERT(buf == (void*) ri->buf);
158 1.1 christos ASSERT(len == sizeof(ri->buf));
159 1.1 christos
160 1.1 christos done_cb_called++;
161 1.1 christos }
162 1.1 christos
163 1.1 christos
164 1.1 christos TEST_IMPL(threadpool_cancel_getaddrinfo) {
165 1.1 christos uv_getaddrinfo_t reqs[4];
166 1.1 christos struct cancel_info ci;
167 1.1 christos struct addrinfo hints;
168 1.1 christos uv_loop_t* loop;
169 1.1 christos int r;
170 1.1 christos
171 1.1 christos INIT_CANCEL_INFO(&ci, reqs);
172 1.1 christos loop = uv_default_loop();
173 1.1 christos saturate_threadpool();
174 1.1 christos
175 1.1 christos r = uv_getaddrinfo(loop, reqs + 0, getaddrinfo_cb, "fail", NULL, NULL);
176 1.1 christos ASSERT(r == 0);
177 1.1 christos
178 1.1 christos r = uv_getaddrinfo(loop, reqs + 1, getaddrinfo_cb, NULL, "fail", NULL);
179 1.1 christos ASSERT(r == 0);
180 1.1 christos
181 1.1 christos r = uv_getaddrinfo(loop, reqs + 2, getaddrinfo_cb, "fail", "fail", NULL);
182 1.1 christos ASSERT(r == 0);
183 1.1 christos
184 1.1 christos r = uv_getaddrinfo(loop, reqs + 3, getaddrinfo_cb, "fail", NULL, &hints);
185 1.1 christos ASSERT(r == 0);
186 1.1 christos
187 1.1 christos ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
188 1.1 christos ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
189 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
190 1.1 christos ASSERT(1 == timer_cb_called);
191 1.1 christos
192 1.1 christos MAKE_VALGRIND_HAPPY();
193 1.1 christos return 0;
194 1.1 christos }
195 1.1 christos
196 1.1 christos
197 1.1 christos TEST_IMPL(threadpool_cancel_getnameinfo) {
198 1.1 christos uv_getnameinfo_t reqs[4];
199 1.1 christos struct sockaddr_in addr4;
200 1.1 christos struct cancel_info ci;
201 1.1 christos uv_loop_t* loop;
202 1.1 christos int r;
203 1.1 christos
204 1.1 christos r = uv_ip4_addr("127.0.0.1", 80, &addr4);
205 1.1 christos ASSERT(r == 0);
206 1.1 christos
207 1.1 christos INIT_CANCEL_INFO(&ci, reqs);
208 1.1 christos loop = uv_default_loop();
209 1.1 christos saturate_threadpool();
210 1.1 christos
211 1.1 christos r = uv_getnameinfo(loop, reqs + 0, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
212 1.1 christos ASSERT(r == 0);
213 1.1 christos
214 1.1 christos r = uv_getnameinfo(loop, reqs + 1, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
215 1.1 christos ASSERT(r == 0);
216 1.1 christos
217 1.1 christos r = uv_getnameinfo(loop, reqs + 2, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
218 1.1 christos ASSERT(r == 0);
219 1.1 christos
220 1.1 christos r = uv_getnameinfo(loop, reqs + 3, getnameinfo_cb, (const struct sockaddr*)&addr4, 0);
221 1.1 christos ASSERT(r == 0);
222 1.1 christos
223 1.1 christos ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
224 1.1 christos ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
225 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
226 1.1 christos ASSERT(1 == timer_cb_called);
227 1.1 christos
228 1.1 christos MAKE_VALGRIND_HAPPY();
229 1.1 christos return 0;
230 1.1 christos }
231 1.1 christos
232 1.1 christos
233 1.1 christos TEST_IMPL(threadpool_cancel_random) {
234 1.1 christos struct random_info req;
235 1.1 christos uv_loop_t* loop;
236 1.1 christos
237 1.1 christos saturate_threadpool();
238 1.1 christos loop = uv_default_loop();
239 1.1 christos ASSERT(0 == uv_random(loop,
240 1.1 christos &req.random_req,
241 1.1 christos &req.buf,
242 1.1 christos sizeof(req.buf),
243 1.1 christos 0,
244 1.1 christos nop_random_cb));
245 1.1 christos ASSERT(0 == uv_cancel((uv_req_t*) &req));
246 1.1 christos ASSERT(0 == done_cb_called);
247 1.1 christos unblock_threadpool();
248 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
249 1.1 christos ASSERT(1 == done_cb_called);
250 1.1 christos
251 1.1 christos MAKE_VALGRIND_HAPPY();
252 1.1 christos return 0;
253 1.1 christos }
254 1.1 christos
255 1.1 christos
256 1.1 christos TEST_IMPL(threadpool_cancel_work) {
257 1.1 christos struct cancel_info ci;
258 1.1 christos uv_work_t reqs[16];
259 1.1 christos uv_loop_t* loop;
260 1.1 christos unsigned i;
261 1.1 christos
262 1.1 christos INIT_CANCEL_INFO(&ci, reqs);
263 1.1 christos loop = uv_default_loop();
264 1.1 christos saturate_threadpool();
265 1.1 christos
266 1.1 christos for (i = 0; i < ARRAY_SIZE(reqs); i++)
267 1.1 christos ASSERT(0 == uv_queue_work(loop, reqs + i, work2_cb, done2_cb));
268 1.1 christos
269 1.1 christos ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
270 1.1 christos ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
271 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
272 1.1 christos ASSERT(1 == timer_cb_called);
273 1.1 christos ASSERT(ARRAY_SIZE(reqs) == done2_cb_called);
274 1.1 christos
275 1.1 christos MAKE_VALGRIND_HAPPY();
276 1.1 christos return 0;
277 1.1 christos }
278 1.1 christos
279 1.1 christos
280 1.1 christos TEST_IMPL(threadpool_cancel_fs) {
281 1.1 christos struct cancel_info ci;
282 1.1 christos uv_fs_t reqs[26];
283 1.1 christos uv_loop_t* loop;
284 1.1 christos unsigned n;
285 1.1 christos uv_buf_t iov;
286 1.1 christos
287 1.1 christos INIT_CANCEL_INFO(&ci, reqs);
288 1.1 christos loop = uv_default_loop();
289 1.1 christos saturate_threadpool();
290 1.1 christos iov = uv_buf_init(NULL, 0);
291 1.1 christos
292 1.1 christos /* Needs to match ARRAY_SIZE(fs_reqs). */
293 1.1 christos n = 0;
294 1.1 christos ASSERT(0 == uv_fs_chmod(loop, reqs + n++, "/", 0, fs_cb));
295 1.1 christos ASSERT(0 == uv_fs_chown(loop, reqs + n++, "/", 0, 0, fs_cb));
296 1.1 christos ASSERT(0 == uv_fs_close(loop, reqs + n++, 0, fs_cb));
297 1.1 christos ASSERT(0 == uv_fs_fchmod(loop, reqs + n++, 0, 0, fs_cb));
298 1.1 christos ASSERT(0 == uv_fs_fchown(loop, reqs + n++, 0, 0, 0, fs_cb));
299 1.1 christos ASSERT(0 == uv_fs_fdatasync(loop, reqs + n++, 0, fs_cb));
300 1.1 christos ASSERT(0 == uv_fs_fstat(loop, reqs + n++, 0, fs_cb));
301 1.1 christos ASSERT(0 == uv_fs_fsync(loop, reqs + n++, 0, fs_cb));
302 1.1 christos ASSERT(0 == uv_fs_ftruncate(loop, reqs + n++, 0, 0, fs_cb));
303 1.1 christos ASSERT(0 == uv_fs_futime(loop, reqs + n++, 0, 0, 0, fs_cb));
304 1.1 christos ASSERT(0 == uv_fs_link(loop, reqs + n++, "/", "/", fs_cb));
305 1.1 christos ASSERT(0 == uv_fs_lstat(loop, reqs + n++, "/", fs_cb));
306 1.1 christos ASSERT(0 == uv_fs_mkdir(loop, reqs + n++, "/", 0, fs_cb));
307 1.1 christos ASSERT(0 == uv_fs_open(loop, reqs + n++, "/", 0, 0, fs_cb));
308 1.1 christos ASSERT(0 == uv_fs_read(loop, reqs + n++, 0, &iov, 1, 0, fs_cb));
309 1.1 christos ASSERT(0 == uv_fs_scandir(loop, reqs + n++, "/", 0, fs_cb));
310 1.1 christos ASSERT(0 == uv_fs_readlink(loop, reqs + n++, "/", fs_cb));
311 1.1 christos ASSERT(0 == uv_fs_realpath(loop, reqs + n++, "/", fs_cb));
312 1.1 christos ASSERT(0 == uv_fs_rename(loop, reqs + n++, "/", "/", fs_cb));
313 1.1 christos ASSERT(0 == uv_fs_mkdir(loop, reqs + n++, "/", 0, fs_cb));
314 1.1 christos ASSERT(0 == uv_fs_sendfile(loop, reqs + n++, 0, 0, 0, 0, fs_cb));
315 1.1 christos ASSERT(0 == uv_fs_stat(loop, reqs + n++, "/", fs_cb));
316 1.1 christos ASSERT(0 == uv_fs_symlink(loop, reqs + n++, "/", "/", 0, fs_cb));
317 1.1 christos ASSERT(0 == uv_fs_unlink(loop, reqs + n++, "/", fs_cb));
318 1.1 christos ASSERT(0 == uv_fs_utime(loop, reqs + n++, "/", 0, 0, fs_cb));
319 1.1 christos ASSERT(0 == uv_fs_write(loop, reqs + n++, 0, &iov, 1, 0, fs_cb));
320 1.1 christos ASSERT(n == ARRAY_SIZE(reqs));
321 1.1 christos
322 1.1 christos ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
323 1.1 christos ASSERT(0 == uv_timer_start(&ci.timer_handle, timer_cb, 10, 0));
324 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
325 1.1 christos ASSERT(n == fs_cb_called);
326 1.1 christos ASSERT(1 == timer_cb_called);
327 1.1 christos
328 1.1 christos
329 1.1 christos MAKE_VALGRIND_HAPPY();
330 1.1 christos return 0;
331 1.1 christos }
332 1.1 christos
333 1.1 christos
334 1.1 christos TEST_IMPL(threadpool_cancel_single) {
335 1.1 christos uv_loop_t* loop;
336 1.1 christos uv_work_t req;
337 1.1 christos
338 1.1 christos saturate_threadpool();
339 1.1 christos loop = uv_default_loop();
340 1.1 christos ASSERT(0 == uv_queue_work(loop, &req, (uv_work_cb) abort, nop_done_cb));
341 1.1 christos ASSERT(0 == uv_cancel((uv_req_t*) &req));
342 1.1 christos ASSERT(0 == done_cb_called);
343 1.1 christos unblock_threadpool();
344 1.1 christos ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
345 1.1 christos ASSERT(1 == done_cb_called);
346 1.1 christos
347 1.1 christos MAKE_VALGRIND_HAPPY();
348 1.1 christos return 0;
349 1.1 christos }
350