1 /* Copyright (c) 2016-2020 the Civetweb developers
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 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:
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 FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 /**
23  * We include the source file so that we have access to the internal private
24  * static functions
25  */
26 #ifdef _MSC_VER
27 #ifndef _CRT_SECURE_NO_WARNINGS
28 #define _CRT_SECURE_NO_WARNINGS
29 #endif
30 #endif
31 
32 #if defined(__GNUC__)
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wunused-function"
35 #endif
36 
37 #define CIVETWEB_API static
38 #define USE_TIMERS
39 
40 #include "../src/civetweb.c"
41 
42 #include <stdlib.h>
43 #include <time.h>
44 
45 #include "timertest.h"
46 
47 #define TIMERS_IN_TEST 10
48 static int action_dec_ret;
49 
50 
51 static int
action_dec(void * arg)52 action_dec(void *arg)
53 {
54 	int *p = (int *)arg;
55 	(*p)--;
56 
57 	if (*p < -1) {
58 		ck_abort_msg("Periodic timer called too often");
59 		/* return 0 here would be unreachable code */
60 	}
61 
62 	return (*p >= -3) ? action_dec_ret : 0;
63 }
64 
65 
66 static void
action_cancel(void * arg)67 action_cancel(void *arg)
68 {
69 	int *p = (int *)arg;
70 
71 	/* test convention: store cancel counter after timer counter */
72 	p += TIMERS_IN_TEST;
73 
74 	if (*p != 0) {
75 		ck_abort_msg("Double call of timer cancel action");
76 		/* return 0 here would be unreachable code */
77 	}
78 
79 	(*p)++;
80 }
81 
82 
83 static int
action_dec_to_0(void * arg)84 action_dec_to_0(void *arg)
85 {
86 	int *p = (int *)arg;
87 	(*p)--;
88 
89 	if (*p <= -1) {
90 		ck_abort_msg("Periodic timer called too often");
91 		/* return 0 here would be unreachable code */
92 	}
93 
94 	return (*p > 0);
95 }
96 
97 
START_TEST(test_timer_cyclic)98 START_TEST(test_timer_cyclic)
99 {
100 	struct mg_context ctx;
101 	int c[TIMERS_IN_TEST * 2];
102 	memset(&ctx, 0, sizeof(ctx));
103 	memset(c, 0, sizeof(c));
104 
105 	action_dec_ret = 1;
106 
107 	mark_point();
108 	timers_init(&ctx);
109 	mg_sleep(100);
110 	mark_point();
111 
112 	c[0] = 100;
113 	timer_add(&ctx, 0.05, 0.1, 1, action_dec, c + 0, action_cancel);
114 	c[2] = 20;
115 	timer_add(&ctx, 0.25, 0.5, 1, action_dec, c + 2, action_cancel);
116 	c[1] = 50;
117 	timer_add(&ctx, 0.1, 0.2, 1, action_dec, c + 1, action_cancel);
118 
119 	mark_point();
120 
121 	mg_sleep(10000); /* Sleep 10 second - timers will run */
122 
123 	mark_point();
124 	ctx.stop_flag = 99; /* End timer thread */
125 	mark_point();
126 
127 	mg_sleep(2000); /* Sleep 2 second - timers will not run */
128 
129 	mark_point();
130 
131 	timers_exit(&ctx);
132 
133 	mark_point();
134 
135 	mg_sleep(2000); /* Sleep 2 second - timers will not run */
136 
137 	mark_point();
138 
139 	/* If this test runs in a virtual environment, like the CI unit test
140 	 * containers, there might be some timing deviations, so check the
141 	 * counter with some tolerance. */
142 
143 	ck_assert_int_ge(c[0], -1);
144 	ck_assert_int_le(c[0], +1);
145 	ck_assert_int_ge(c[1], -1);
146 	ck_assert_int_le(c[1], +1);
147 	ck_assert_int_ge(c[2], -1);
148 	ck_assert_int_le(c[2], +1);
149 
150 	/* Every cancel action must be called once */
151 	ck_assert_int_eq(c[0 + TIMERS_IN_TEST], 1);
152 	ck_assert_int_eq(c[1 + TIMERS_IN_TEST], 1);
153 	ck_assert_int_eq(c[2 + TIMERS_IN_TEST], 1);
154 }
155 END_TEST
156 
157 
START_TEST(test_timer_oneshot_by_callback_retval)158 START_TEST(test_timer_oneshot_by_callback_retval)
159 {
160 	struct mg_context ctx;
161 	int c[TIMERS_IN_TEST * 2];
162 	memset(&ctx, 0, sizeof(ctx));
163 	memset(c, 0, sizeof(c));
164 
165 	action_dec_ret = 0;
166 
167 	mark_point();
168 	timers_init(&ctx);
169 	mg_sleep(100);
170 	mark_point();
171 
172 	c[0] = 10;
173 	timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0, action_cancel);
174 	c[2] = 2;
175 	timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2, action_cancel);
176 	c[1] = 5;
177 	timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1, action_cancel);
178 
179 	mark_point();
180 
181 	mg_sleep(1000); /* Sleep 1 second - timer will run */
182 
183 	mark_point();
184 	ctx.stop_flag = 99; /* End timer thread */
185 	mark_point();
186 
187 	mg_sleep(1000); /* Sleep 1 second - timer will not run */
188 
189 	mark_point();
190 
191 	timers_exit(&ctx);
192 
193 	mark_point();
194 
195 	mg_sleep(2000); /* Sleep 2 second - timers will not run */
196 
197 	mark_point();
198 
199 	/* Every decrement action must be called once */
200 	ck_assert_int_eq(c[0], 9);
201 	ck_assert_int_eq(c[1], 4);
202 	ck_assert_int_eq(c[2], 1);
203 
204 	/* Every cancel action must be called once */
205 	ck_assert_int_eq(c[0 + TIMERS_IN_TEST], 1);
206 	ck_assert_int_eq(c[1 + TIMERS_IN_TEST], 1);
207 	ck_assert_int_eq(c[2 + TIMERS_IN_TEST], 1);
208 }
209 END_TEST
210 
211 
START_TEST(test_timer_oneshot_by_timer_add)212 START_TEST(test_timer_oneshot_by_timer_add)
213 {
214 	struct mg_context ctx;
215 	int c[TIMERS_IN_TEST * 2];
216 	memset(&ctx, 0, sizeof(ctx));
217 	memset(c, 0, sizeof(c));
218 
219 	action_dec_ret = 1;
220 
221 	mark_point();
222 	timers_init(&ctx);
223 	mg_sleep(100);
224 	mark_point();
225 
226 	c[0] = 10;
227 	timer_add(&ctx, 0, 0, 1, action_dec, c + 0, action_cancel);
228 	c[2] = 2;
229 	timer_add(&ctx, 0, 0, 1, action_dec, c + 2, action_cancel);
230 	c[1] = 5;
231 	timer_add(&ctx, 0, 0, 1, action_dec, c + 1, action_cancel);
232 
233 	mark_point();
234 
235 	mg_sleep(1000); /* Sleep 1 second - timer will run */
236 
237 	mark_point();
238 	ctx.stop_flag = 99; /* End timer thread */
239 	mark_point();
240 
241 	mg_sleep(1000); /* Sleep 1 second - timer will not run */
242 
243 	mark_point();
244 
245 	timers_exit(&ctx);
246 
247 	mark_point();
248 
249 	mg_sleep(2000); /* Sleep 2 second - timers will not run */
250 
251 	mark_point();
252 
253 	/* Every decrement action was called once */
254 	ck_assert_int_eq(c[0], 9);
255 	ck_assert_int_eq(c[1], 4);
256 	ck_assert_int_eq(c[2], 1);
257 
258 	/* Every cancel action must be called once */
259 	ck_assert_int_eq(c[0 + TIMERS_IN_TEST], 1);
260 	ck_assert_int_eq(c[1 + TIMERS_IN_TEST], 1);
261 	ck_assert_int_eq(c[2 + TIMERS_IN_TEST], 1);
262 }
263 END_TEST
264 
265 
START_TEST(test_timer_mixed)266 START_TEST(test_timer_mixed)
267 {
268 	struct mg_context ctx;
269 	int c[TIMERS_IN_TEST];
270 	memset(&ctx, 0, sizeof(ctx));
271 	memset(c, 0, sizeof(c));
272 
273 	mark_point();
274 	timers_init(&ctx);
275 	mg_sleep(100);
276 	mark_point();
277 
278 	/* 3 --> 2, because it is a single shot timer */
279 	c[0] = 3;
280 	timer_add(&ctx, 0, 0, 1, action_dec_to_0, &c[0], NULL);
281 
282 	/* 3 --> 0, because it will run until c[1] = 0 and then stop */
283 	c[1] = 3;
284 	timer_add(&ctx, 0, 0.2, 1, action_dec_to_0, &c[1], NULL);
285 
286 	/* 3 --> 1, with 750 ms period, it will run once at start,
287 	 * then once 750 ms later, but not 1500 ms later, since the
288 	 * timer is already stopped then. */
289 	c[2] = 3;
290 	timer_add(&ctx, 0, 0.75, 1, action_dec_to_0, &c[2], NULL);
291 
292 	/* 3 --> 2, will run at start, but no cyclic in 1 second */
293 	c[3] = 3;
294 	timer_add(&ctx, 0, 2.5, 1, action_dec_to_0, &c[3], NULL);
295 
296 	/* 3 --> 3, will not run at start */
297 	c[4] = 3;
298 	timer_add(&ctx, 2.5, 0.1, 1, action_dec_to_0, &c[4], NULL);
299 
300 	/* 3 --> 2, an absolute timer in the past (-123.456) will still
301 	 * run once at start, and then with the period */
302 	c[5] = 3;
303 	timer_add(&ctx, -123.456, 2.5, 0, action_dec_to_0, &c[5], NULL);
304 
305 	/* 3 --> 1, an absolute timer in the past (-123.456) will still
306 	 * run once at start, and then with the period */
307 	c[6] = 3;
308 	timer_add(&ctx, -123.456, 0.75, 0, action_dec_to_0, &c[6], NULL);
309 
310 	mark_point();
311 
312 	mg_sleep(1000); /* Sleep 1 second - timer will run */
313 
314 	mark_point();
315 	ctx.stop_flag = 99; /* End timer thread */
316 	mark_point();
317 
318 	mg_sleep(1000); /* Sleep 1 second - timer will not run */
319 
320 	mark_point();
321 
322 	timers_exit(&ctx);
323 
324 	mark_point();
325 
326 	mg_sleep(2000); /* Sleep 2 second - timers will not run */
327 
328 	mark_point();
329 
330 	ck_assert_int_eq(c[0], 2);
331 	ck_assert_int_eq(c[1], 0);
332 	ck_assert_int_eq(c[2], 1);
333 	ck_assert_int_eq(c[3], 2);
334 	ck_assert_int_eq(c[4], 3);
335 	ck_assert_int_eq(c[5], 2);
336 	ck_assert_int_eq(c[6], 1);
337 }
338 END_TEST
339 
340 
341 #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
342 Suite *
make_timertest_suite(void)343 make_timertest_suite(void)
344 {
345 	Suite *const suite = suite_create("Timer");
346 
347 	TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
348 	TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
349 	TCase *const tcase_timer_mixed = tcase_create("Timer Mixed");
350 
351 	tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
352 	tcase_set_timeout(tcase_timer_cyclic, 30);
353 	suite_add_tcase(suite, tcase_timer_cyclic);
354 
355 	tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_timer_add);
356 	tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_callback_retval);
357 	tcase_set_timeout(tcase_timer_oneshot, 30);
358 	suite_add_tcase(suite, tcase_timer_oneshot);
359 
360 	tcase_add_test(tcase_timer_mixed, test_timer_mixed);
361 	tcase_set_timeout(tcase_timer_mixed, 30);
362 	suite_add_tcase(suite, tcase_timer_mixed);
363 
364 	return suite;
365 }
366 
367 #else
368 
369 /* Used to debug test cases without using the check framework */
370 void
371 MAIN_TIMER_PRIVATE(void)
372 {
373 	unsigned f_avail;
374 	unsigned f_ret;
375 
376 #if defined(_WIN32)
377 	WSADATA data;
378 	WSAStartup(MAKEWORD(2, 2), &data);
379 #endif
380 
381 	f_avail = mg_check_feature(0xFF);
382 	f_ret = mg_init_library(f_avail);
383 	ck_assert_uint_eq(f_ret, f_avail);
384 
385 	test_timer_cyclic(0);
386 	test_timer_oneshot_by_timer_add(0);
387 	test_timer_oneshot_by_callback_retval(0);
388 	test_timer_mixed(0);
389 
390 	mg_exit_library();
391 
392 #if defined(_WIN32)
393 	WSACleanup();
394 #endif
395 }
396 
397 #endif
398