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 #include <stdio.h>
26 #include <stdlib.h>
27 
28 
29 static int close_cb_called = 0;
30 
31 
close_cb(uv_handle_t * handle)32 static void close_cb(uv_handle_t* handle) {
33   ASSERT_NOT_NULL(handle);
34   close_cb_called++;
35 }
36 
37 
timer_cb(uv_timer_t * handle)38 static void timer_cb(uv_timer_t* handle) {
39   ASSERT(0 && "timer_cb should not have been called");
40 }
41 
42 
TEST_IMPL(active)43 TEST_IMPL(active) {
44   int r;
45   uv_timer_t timer;
46 
47   r = uv_timer_init(uv_default_loop(), &timer);
48   ASSERT(r == 0);
49 
50   /* uv_is_active() and uv_is_closing() should always return either 0 or 1. */
51   ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
52   ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
53 
54   r = uv_timer_start(&timer, timer_cb, 1000, 0);
55   ASSERT(r == 0);
56 
57   ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
58   ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
59 
60   r = uv_timer_stop(&timer);
61   ASSERT(r == 0);
62 
63   ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
64   ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
65 
66   r = uv_timer_start(&timer, timer_cb, 1000, 0);
67   ASSERT(r == 0);
68 
69   ASSERT(1 == uv_is_active((uv_handle_t*) &timer));
70   ASSERT(0 == uv_is_closing((uv_handle_t*) &timer));
71 
72   uv_close((uv_handle_t*) &timer, close_cb);
73 
74   ASSERT(0 == uv_is_active((uv_handle_t*) &timer));
75   ASSERT(1 == uv_is_closing((uv_handle_t*) &timer));
76 
77   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
78   ASSERT(r == 0);
79 
80   ASSERT(close_cb_called == 1);
81 
82   MAKE_VALGRIND_HAPPY();
83   return 0;
84 }
85