libuv-svace-build
141 строка · 4.1 Кб
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
26static int close_cb_called = 0;
27static int repeat_1_cb_called = 0;
28static int repeat_2_cb_called = 0;
29
30static int repeat_2_cb_allowed = 0;
31
32static uv_timer_t dummy, repeat_1, repeat_2;
33
34static uint64_t start_time;
35
36
37static void close_cb(uv_handle_t* handle) {
38ASSERT_NOT_NULL(handle);
39
40close_cb_called++;
41}
42
43
44static void repeat_1_cb(uv_timer_t* handle) {
45int r;
46
47ASSERT_PTR_EQ(handle, &repeat_1);
48ASSERT_EQ(50, uv_timer_get_repeat((uv_timer_t*)handle));
49
50fprintf(stderr, "repeat_1_cb called after %ld ms\n",
51(long int)(uv_now(uv_default_loop()) - start_time));
52fflush(stderr);
53
54repeat_1_cb_called++;
55
56r = uv_timer_again(&repeat_2);
57ASSERT_OK(r);
58
59if (repeat_1_cb_called == 10) {
60uv_close((uv_handle_t*)handle, close_cb);
61/* We're not calling uv_timer_again on repeat_2 any more, so after this
62* timer_2_cb is expected. */
63repeat_2_cb_allowed = 1;
64return;
65}
66}
67
68
69static void repeat_2_cb(uv_timer_t* handle) {
70ASSERT_PTR_EQ(handle, &repeat_2);
71ASSERT(repeat_2_cb_allowed);
72
73fprintf(stderr, "repeat_2_cb called after %ld ms\n",
74(long int)(uv_now(uv_default_loop()) - start_time));
75fflush(stderr);
76
77repeat_2_cb_called++;
78
79if (uv_timer_get_repeat(&repeat_2) == 0) {
80ASSERT_OK(uv_is_active((uv_handle_t*) handle));
81uv_close((uv_handle_t*)handle, close_cb);
82return;
83}
84
85fprintf(stderr, "uv_timer_get_repeat %ld ms\n",
86(long int)uv_timer_get_repeat(&repeat_2));
87fflush(stderr);
88ASSERT_EQ(100, uv_timer_get_repeat(&repeat_2));
89
90/* This shouldn't take effect immediately. */
91uv_timer_set_repeat(&repeat_2, 0);
92}
93
94
95TEST_IMPL(timer_again) {
96int r;
97
98start_time = uv_now(uv_default_loop());
99ASSERT_LT(0, start_time);
100
101/* Verify that it is not possible to uv_timer_again a never-started timer. */
102r = uv_timer_init(uv_default_loop(), &dummy);
103ASSERT_OK(r);
104r = uv_timer_again(&dummy);
105ASSERT_EQ(r, UV_EINVAL);
106uv_unref((uv_handle_t*)&dummy);
107
108/* Start timer repeat_1. */
109r = uv_timer_init(uv_default_loop(), &repeat_1);
110ASSERT_OK(r);
111r = uv_timer_start(&repeat_1, repeat_1_cb, 50, 0);
112ASSERT_OK(r);
113ASSERT_OK(uv_timer_get_repeat(&repeat_1));
114
115/* Actually make repeat_1 repeating. */
116uv_timer_set_repeat(&repeat_1, 50);
117ASSERT_EQ(50, uv_timer_get_repeat(&repeat_1));
118
119/*
120* Start another repeating timer. It'll be again()ed by the repeat_1 so
121* it should not time out until repeat_1 stops.
122*/
123r = uv_timer_init(uv_default_loop(), &repeat_2);
124ASSERT_OK(r);
125r = uv_timer_start(&repeat_2, repeat_2_cb, 100, 100);
126ASSERT_OK(r);
127ASSERT_EQ(100, uv_timer_get_repeat(&repeat_2));
128
129uv_run(uv_default_loop(), UV_RUN_DEFAULT);
130
131ASSERT_EQ(10, repeat_1_cb_called);
132ASSERT_EQ(2, repeat_2_cb_called);
133ASSERT_EQ(2, close_cb_called);
134
135fprintf(stderr, "Test took %ld ms (expected ~700 ms)\n",
136(long int)(uv_now(uv_default_loop()) - start_time));
137fflush(stderr);
138
139MAKE_VALGRIND_HAPPY(uv_default_loop());
140return 0;
141}
142