xref: /libuv/test/test-loop-close.c (revision 011a1ac1)
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 #include "uv.h"
23 #include "task.h"
24 
25 static uv_timer_t timer_handle;
26 
timer_cb(uv_timer_t * handle)27 static void timer_cb(uv_timer_t* handle) {
28   ASSERT(handle);
29   uv_stop(handle->loop);
30 }
31 
32 
TEST_IMPL(loop_close)33 TEST_IMPL(loop_close) {
34   int r;
35   uv_loop_t loop;
36 
37   loop.data = &loop;
38   ASSERT_OK(uv_loop_init(&loop));
39   ASSERT_PTR_EQ(loop.data, (void*) &loop);
40 
41   uv_timer_init(&loop, &timer_handle);
42   uv_timer_start(&timer_handle, timer_cb, 100, 100);
43 
44   ASSERT_EQ(UV_EBUSY, uv_loop_close(&loop));
45 
46   uv_run(&loop, UV_RUN_DEFAULT);
47 
48   uv_close((uv_handle_t*) &timer_handle, NULL);
49   r = uv_run(&loop, UV_RUN_DEFAULT);
50   ASSERT_OK(r);
51 
52   ASSERT_PTR_EQ(loop.data, (void*) &loop);
53   ASSERT_OK(uv_loop_close(&loop));
54   ASSERT_PTR_EQ(loop.data, (void*) &loop);
55 
56   return 0;
57 }
58 
loop_instant_close_work_cb(uv_work_t * req)59 static void loop_instant_close_work_cb(uv_work_t* req) {
60 }
61 
loop_instant_close_after_work_cb(uv_work_t * req,int status)62 static void loop_instant_close_after_work_cb(uv_work_t* req, int status) {
63 }
64 
65 /* It's impossible to properly cleanup after this test because loop can't be
66  * closed while work has been queued. */
TEST_IMPL(loop_instant_close)67 TEST_IMPL(loop_instant_close) {
68   static uv_loop_t loop;
69   static uv_work_t req;
70   ASSERT_OK(uv_loop_init(&loop));
71   ASSERT_OK(uv_queue_work(&loop,
72                           &req,
73                           loop_instant_close_work_cb,
74                           loop_instant_close_after_work_cb));
75   MAKE_VALGRIND_HAPPY(uv_default_loop());
76   return 0;
77 }
78