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