1 /* Copyright (c) 2016-2018 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 static int action_dec_ret;
48
49 static int
action_dec(void * arg)50 action_dec(void *arg)
51 {
52 int *p = (int *)arg;
53 (*p)--;
54
55 if (*p < -1) {
56 ck_abort_msg("Periodic timer called too often");
57 /* return 0 here would be unreachable code */
58 }
59
60 return (*p >= -3) ? action_dec_ret : 0;
61 }
62
63
64 static int
action_dec_to_0(void * arg)65 action_dec_to_0(void *arg)
66 {
67 int *p = (int *)arg;
68 (*p)--;
69
70 if (*p <= -1) {
71 ck_abort_msg("Periodic timer called too often");
72 /* return 0 here would be unreachable code */
73 }
74
75 return (*p > 0);
76 }
77
78
START_TEST(test_timer_cyclic)79 START_TEST(test_timer_cyclic)
80 {
81 struct mg_context ctx;
82 int c[10];
83 memset(&ctx, 0, sizeof(ctx));
84 memset(c, 0, sizeof(c));
85
86 action_dec_ret = 1;
87
88 mark_point();
89 timers_init(&ctx);
90 mg_sleep(100);
91 mark_point();
92
93 c[0] = 100;
94 timer_add(&ctx, 0.05, 0.1, 1, action_dec, c + 0);
95 c[2] = 20;
96 timer_add(&ctx, 0.25, 0.5, 1, action_dec, c + 2);
97 c[1] = 50;
98 timer_add(&ctx, 0.1, 0.2, 1, action_dec, c + 1);
99
100 mark_point();
101
102 mg_sleep(10000); /* Sleep 10 second - timers will run */
103
104 mark_point();
105 ctx.stop_flag = 99; /* End timer thread */
106 mark_point();
107
108 mg_sleep(2000); /* Sleep 2 second - timers will not run */
109
110 mark_point();
111
112 timers_exit(&ctx);
113
114 mark_point();
115
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. */
119
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);
126 }
127 END_TEST
128
129
START_TEST(test_timer_oneshot_by_callback_retval)130 START_TEST(test_timer_oneshot_by_callback_retval)
131 {
132 struct mg_context ctx;
133 int c[10];
134 memset(&ctx, 0, sizeof(ctx));
135 memset(c, 0, sizeof(c));
136
137 action_dec_ret = 0;
138
139 mark_point();
140 timers_init(&ctx);
141 mg_sleep(100);
142 mark_point();
143
144 c[0] = 10;
145 timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
146 c[2] = 2;
147 timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
148 c[1] = 5;
149 timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);
150
151 mark_point();
152
153 mg_sleep(1000); /* Sleep 1 second - timer will run */
154
155 mark_point();
156 ctx.stop_flag = 99; /* End timer thread */
157 mark_point();
158
159 mg_sleep(1000); /* Sleep 1 second - timer will not run */
160
161 mark_point();
162
163 timers_exit(&ctx);
164
165 mark_point();
166 mg_sleep(100);
167
168 ck_assert_int_eq(c[0], 9);
169 ck_assert_int_eq(c[1], 4);
170 ck_assert_int_eq(c[2], 1);
171 }
172 END_TEST
173
174
START_TEST(test_timer_oneshot_by_timer_add)175 START_TEST(test_timer_oneshot_by_timer_add)
176 {
177 struct mg_context ctx;
178 int c[10];
179 memset(&ctx, 0, sizeof(ctx));
180 memset(c, 0, sizeof(c));
181
182 action_dec_ret = 1;
183
184 mark_point();
185 timers_init(&ctx);
186 mg_sleep(100);
187 mark_point();
188
189 c[0] = 10;
190 timer_add(&ctx, 0, 0, 1, action_dec, c + 0);
191 c[2] = 2;
192 timer_add(&ctx, 0, 0, 1, action_dec, c + 2);
193 c[1] = 5;
194 timer_add(&ctx, 0, 0, 1, action_dec, c + 1);
195
196 mark_point();
197
198 mg_sleep(1000); /* Sleep 1 second - timer will run */
199
200 mark_point();
201 ctx.stop_flag = 99; /* End timer thread */
202 mark_point();
203
204 mg_sleep(1000); /* Sleep 1 second - timer will not run */
205
206 mark_point();
207
208 timers_exit(&ctx);
209
210 mark_point();
211 mg_sleep(100);
212
213 ck_assert_int_eq(c[0], 9);
214 ck_assert_int_eq(c[1], 4);
215 ck_assert_int_eq(c[2], 1);
216 }
217 END_TEST
218
219
START_TEST(test_timer_mixed)220 START_TEST(test_timer_mixed)
221 {
222 struct mg_context ctx;
223 int c[10];
224 memset(&ctx, 0, sizeof(ctx));
225 memset(c, 0, sizeof(c));
226
227 mark_point();
228 timers_init(&ctx);
229 mg_sleep(100);
230 mark_point();
231
232 /* 3 --> 2, because it is a single shot timer */
233 c[0] = 3;
234 timer_add(&ctx, 0, 0, 1, action_dec_to_0, &c[0]);
235
236 /* 3 --> 0, because it will run until c[1] = 0 and then stop */
237 c[1] = 3;
238 timer_add(&ctx, 0, 0.2, 1, action_dec_to_0, &c[1]);
239
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. */
243 c[2] = 3;
244 timer_add(&ctx, 0, 0.75, 1, action_dec_to_0, &c[2]);
245
246 /* 3 --> 2, will run at start, but no cyclic in 1 second */
247 c[3] = 3;
248 timer_add(&ctx, 0, 2.5, 1, action_dec_to_0, &c[3]);
249
250 /* 3 --> 3, will not run at start */
251 c[4] = 3;
252 timer_add(&ctx, 2.5, 0.1, 1, action_dec_to_0, &c[4]);
253
254 /* 3 --> 2, an absolute timer in the past (-123.456) will still
255 * run once at start, and then with the period */
256 c[5] = 3;
257 timer_add(&ctx, -123.456, 2.5, 0, action_dec_to_0, &c[5]);
258
259 /* 3 --> 1, an absolute timer in the past (-123.456) will still
260 * run once at start, and then with the period */
261 c[6] = 3;
262 timer_add(&ctx, -123.456, 0.75, 0, action_dec_to_0, &c[6]);
263
264 mark_point();
265
266 mg_sleep(1000); /* Sleep 1 second - timer will run */
267
268 mark_point();
269 ctx.stop_flag = 99; /* End timer thread */
270 mark_point();
271
272 mg_sleep(1000); /* Sleep 1 second - timer will not run */
273
274 mark_point();
275
276 timers_exit(&ctx);
277
278 mark_point();
279 mg_sleep(100);
280
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);
288 }
289 END_TEST
290
291
292 #if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
293 Suite *
make_timertest_suite(void)294 make_timertest_suite(void)
295 {
296 Suite *const suite = suite_create("Timer");
297
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");
301
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);
305
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);
310
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);
314
315 return suite;
316 }
317 #endif
318
319
320 #ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
321 /* Used to debug test cases without using the check framework */
322
323 void
TIMER_PRIVATE(void)324 TIMER_PRIVATE(void)
325 {
326 unsigned f_avail;
327 unsigned f_ret;
328
329 #if defined(_WIN32)
330 WSADATA data;
331 WSAStartup(MAKEWORD(2, 2), &data);
332 #endif
333
334 f_avail = mg_check_feature(0xFF);
335 f_ret = mg_init_library(f_avail);
336 ck_assert_uint_eq(f_ret, f_avail);
337
338 test_timer_cyclic(0);
339 test_timer_oneshot_by_timer_add(0);
340 test_timer_oneshot_by_callback_retval(0);
341 test_timer_mixed(0);
342
343 mg_exit_library();
344
345 #if defined(_WIN32)
346 WSACleanup();
347 #endif
348 }
349
350 #endif
351