1 /* Copyright (c) 2016-2017 the Civetweb developers
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 deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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 FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * We include the source file so that we have access to the internal private
27 #ifndef _CRT_SECURE_NO_WARNINGS
28 #define _CRT_SECURE_NO_WARNINGS
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wunused-function"
37 #define CIVETWEB_API static
40 #include "../src/civetweb.c"
45 #include "timertest.h"
47 static int action_dec_ret
;
56 ck_abort_msg("Periodic timer called too often");
57 /* return 0 here would be unreachable code */
60 return (*p
>= -3) ? action_dec_ret
: 0;
65 action_dec_to_0(void *arg
)
71 ck_abort_msg("Periodic timer called too often");
72 /* return 0 here would be unreachable code */
79 START_TEST(test_timer_cyclic
)
81 struct mg_context ctx
;
83 memset(&ctx
, 0, sizeof(ctx
));
84 memset(c
, 0, sizeof(c
));
94 timer_add(&ctx
, 0.05, 0.1, 1, action_dec
, c
+ 0);
96 timer_add(&ctx
, 0.25, 0.5, 1, action_dec
, c
+ 2);
98 timer_add(&ctx
, 0.1, 0.2, 1, action_dec
, c
+ 1);
102 mg_sleep(10000); /* Sleep 10 second - timers will run */
105 ctx
.stop_flag
= 99; /* End timer thread */
108 mg_sleep(2000); /* Sleep 2 second - timers will not run */
116 /* If this test runs in a virtual environment, like the CI unit test
117 * containers, there might be some timing deviations, so check the
118 * counter with some tolerance. */
120 ck_assert_int_ge(c
[0], -1);
121 ck_assert_int_le(c
[0], +1);
122 ck_assert_int_ge(c
[1], -1);
123 ck_assert_int_le(c
[1], +1);
124 ck_assert_int_ge(c
[2], -1);
125 ck_assert_int_le(c
[2], +1);
130 START_TEST(test_timer_oneshot_by_callback_retval
)
132 struct mg_context ctx
;
134 memset(&ctx
, 0, sizeof(ctx
));
135 memset(c
, 0, sizeof(c
));
145 timer_add(&ctx
, 0, 0.1, 1, action_dec
, c
+ 0);
147 timer_add(&ctx
, 0, 0.5, 1, action_dec
, c
+ 2);
149 timer_add(&ctx
, 0, 0.2, 1, action_dec
, c
+ 1);
153 mg_sleep(1000); /* Sleep 1 second - timer will run */
156 ctx
.stop_flag
= 99; /* End timer thread */
159 mg_sleep(1000); /* Sleep 1 second - timer will not run */
168 ck_assert_int_eq(c
[0], 9);
169 ck_assert_int_eq(c
[1], 4);
170 ck_assert_int_eq(c
[2], 1);
175 START_TEST(test_timer_oneshot_by_timer_add
)
177 struct mg_context ctx
;
179 memset(&ctx
, 0, sizeof(ctx
));
180 memset(c
, 0, sizeof(c
));
190 timer_add(&ctx
, 0, 0, 1, action_dec
, c
+ 0);
192 timer_add(&ctx
, 0, 0, 1, action_dec
, c
+ 2);
194 timer_add(&ctx
, 0, 0, 1, action_dec
, c
+ 1);
198 mg_sleep(1000); /* Sleep 1 second - timer will run */
201 ctx
.stop_flag
= 99; /* End timer thread */
204 mg_sleep(1000); /* Sleep 1 second - timer will not run */
213 ck_assert_int_eq(c
[0], 9);
214 ck_assert_int_eq(c
[1], 4);
215 ck_assert_int_eq(c
[2], 1);
220 START_TEST(test_timer_mixed
)
222 struct mg_context ctx
;
224 memset(&ctx
, 0, sizeof(ctx
));
225 memset(c
, 0, sizeof(c
));
232 /* 3 --> 2, because it is a single shot timer */
234 timer_add(&ctx
, 0, 0, 1, action_dec_to_0
, &c
[0]);
236 /* 3 --> 0, because it will run until c[1] = 0 and then stop */
238 timer_add(&ctx
, 0, 0.2, 1, action_dec_to_0
, &c
[1]);
240 /* 3 --> 1, with 750 ms period, it will run once at start,
241 * then once 750 ms later, but not 1500 ms later, since the
242 * timer is already stopped then. */
244 timer_add(&ctx
, 0, 0.75, 1, action_dec_to_0
, &c
[2]);
246 /* 3 --> 2, will run at start, but no cyclic in 1 second */
248 timer_add(&ctx
, 0, 2.5, 1, action_dec_to_0
, &c
[3]);
250 /* 3 --> 3, will not run at start */
252 timer_add(&ctx
, 2.5, 0.1, 1, action_dec_to_0
, &c
[4]);
254 /* 3 --> 2, an absolute timer in the past (-123.456) will still
255 * run once at start, and then with the period */
257 timer_add(&ctx
, -123.456, 2.5, 0, action_dec_to_0
, &c
[5]);
259 /* 3 --> 1, an absolute timer in the past (-123.456) will still
260 * run once at start, and then with the period */
262 timer_add(&ctx
, -123.456, 0.75, 0, action_dec_to_0
, &c
[6]);
266 mg_sleep(1000); /* Sleep 1 second - timer will run */
269 ctx
.stop_flag
= 99; /* End timer thread */
272 mg_sleep(1000); /* Sleep 1 second - timer will not run */
281 ck_assert_int_eq(c
[0], 2);
282 ck_assert_int_eq(c
[1], 0);
283 ck_assert_int_eq(c
[2], 1);
284 ck_assert_int_eq(c
[3], 2);
285 ck_assert_int_eq(c
[4], 3);
286 ck_assert_int_eq(c
[5], 2);
287 ck_assert_int_eq(c
[6], 1);
292 #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
294 make_timertest_suite(void)
296 Suite
*const suite
= suite_create("Timer");
298 TCase
*const tcase_timer_cyclic
= tcase_create("Timer Periodic");
299 TCase
*const tcase_timer_oneshot
= tcase_create("Timer Single Shot");
300 TCase
*const tcase_timer_mixed
= tcase_create("Timer Mixed");
302 tcase_add_test(tcase_timer_cyclic
, test_timer_cyclic
);
303 tcase_set_timeout(tcase_timer_cyclic
, 30);
304 suite_add_tcase(suite
, tcase_timer_cyclic
);
306 tcase_add_test(tcase_timer_oneshot
, test_timer_oneshot_by_timer_add
);
307 tcase_add_test(tcase_timer_oneshot
, test_timer_oneshot_by_callback_retval
);
308 tcase_set_timeout(tcase_timer_oneshot
, 30);
309 suite_add_tcase(suite
, tcase_timer_oneshot
);
311 tcase_add_test(tcase_timer_mixed
, test_timer_mixed
);
312 tcase_set_timeout(tcase_timer_mixed
, 30);
313 suite_add_tcase(suite
, tcase_timer_mixed
);
320 #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
321 /* Used to debug test cases without using the check framework */
331 WSAStartup(MAKEWORD(2, 2), &data
);
334 f_avail
= mg_check_feature(0xFF);
335 f_ret
= mg_init_library(f_avail
);
336 ck_assert_uint_eq(f_ret
, f_avail
);
338 test_timer_cyclic(0);
339 test_timer_oneshot_by_timer_add(0);
340 test_timer_oneshot_by_callback_retval(0);