summaryrefslogtreecommitdiffstats
path: root/src/civetweb/test/timertest.c
blob: 3ee98dce63d93bf4b8f9d76ea0abbdc1cb24b9ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* Copyright (c) 2016-2017 the Civetweb developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

/**
 * We include the source file so that we have access to the internal private
 * static functions
 */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif

#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif

#define CIVETWEB_API static
#define USE_TIMERS

#include "../src/civetweb.c"

#include <stdlib.h>
#include <time.h>

#include "timertest.h"

static int action_dec_ret;

static int
action_dec(void *arg)
{
	int *p = (int *)arg;
	(*p)--;

	if (*p < -1) {
		ck_abort_msg("Periodic timer called too often");
		/* return 0 here would be unreachable code */
	}

	return (*p >= -3) ? action_dec_ret : 0;
}


static int
action_dec_to_0(void *arg)
{
	int *p = (int *)arg;
	(*p)--;

	if (*p <= -1) {
		ck_abort_msg("Periodic timer called too often");
		/* return 0 here would be unreachable code */
	}

	return (*p > 0);
}


START_TEST(test_timer_cyclic)
{
	struct mg_context ctx;
	int c[10];
	memset(&ctx, 0, sizeof(ctx));
	memset(c, 0, sizeof(c));

	action_dec_ret = 1;

	mark_point();
	timers_init(&ctx);
	mg_sleep(100);
	mark_point();

	c[0] = 100;
	timer_add(&ctx, 0.05, 0.1, 1, action_dec, c + 0);
	c[2] = 20;
	timer_add(&ctx, 0.25, 0.5, 1, action_dec, c + 2);
	c[1] = 50;
	timer_add(&ctx, 0.1, 0.2, 1, action_dec, c + 1);

	mark_point();

	mg_sleep(10000); /* Sleep 10 second - timers will run */

	mark_point();
	ctx.stop_flag = 99; /* End timer thread */
	mark_point();

	mg_sleep(2000); /* Sleep 2 second - timers will not run */

	mark_point();

	timers_exit(&ctx);

	mark_point();

	/* If this test runs in a virtual environment, like the CI unit test
	 * containers, there might be some timing deviations, so check the
	 * counter with some tolerance. */

	ck_assert_int_ge(c[0], -1);
	ck_assert_int_le(c[0], +1);
	ck_assert_int_ge(c[1], -1);
	ck_assert_int_le(c[1], +1);
	ck_assert_int_ge(c[2], -1);
	ck_assert_int_le(c[2], +1);
}
END_TEST


START_TEST(test_timer_oneshot_by_callback_retval)
{
	struct mg_context ctx;
	int c[10];
	memset(&ctx, 0, sizeof(ctx));
	memset(c, 0, sizeof(c));

	action_dec_ret = 0;

	mark_point();
	timers_init(&ctx);
	mg_sleep(100);
	mark_point();

	c[0] = 10;
	timer_add(&ctx, 0, 0.1, 1, action_dec, c + 0);
	c[2] = 2;
	timer_add(&ctx, 0, 0.5, 1, action_dec, c + 2);
	c[1] = 5;
	timer_add(&ctx, 0, 0.2, 1, action_dec, c + 1);

	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will run */

	mark_point();
	ctx.stop_flag = 99; /* End timer thread */
	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will not run */

	mark_point();

	timers_exit(&ctx);

	mark_point();
	mg_sleep(100);

	ck_assert_int_eq(c[0], 9);
	ck_assert_int_eq(c[1], 4);
	ck_assert_int_eq(c[2], 1);
}
END_TEST


START_TEST(test_timer_oneshot_by_timer_add)
{
	struct mg_context ctx;
	int c[10];
	memset(&ctx, 0, sizeof(ctx));
	memset(c, 0, sizeof(c));

	action_dec_ret = 1;

	mark_point();
	timers_init(&ctx);
	mg_sleep(100);
	mark_point();

	c[0] = 10;
	timer_add(&ctx, 0, 0, 1, action_dec, c + 0);
	c[2] = 2;
	timer_add(&ctx, 0, 0, 1, action_dec, c + 2);
	c[1] = 5;
	timer_add(&ctx, 0, 0, 1, action_dec, c + 1);

	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will run */

	mark_point();
	ctx.stop_flag = 99; /* End timer thread */
	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will not run */

	mark_point();

	timers_exit(&ctx);

	mark_point();
	mg_sleep(100);

	ck_assert_int_eq(c[0], 9);
	ck_assert_int_eq(c[1], 4);
	ck_assert_int_eq(c[2], 1);
}
END_TEST


START_TEST(test_timer_mixed)
{
	struct mg_context ctx;
	int c[10];
	memset(&ctx, 0, sizeof(ctx));
	memset(c, 0, sizeof(c));

	mark_point();
	timers_init(&ctx);
	mg_sleep(100);
	mark_point();

	/* 3 --> 2, because it is a single shot timer */
	c[0] = 3;
	timer_add(&ctx, 0, 0, 1, action_dec_to_0, &c[0]);

	/* 3 --> 0, because it will run until c[1] = 0 and then stop */
	c[1] = 3;
	timer_add(&ctx, 0, 0.2, 1, action_dec_to_0, &c[1]);

	/* 3 --> 1, with 750 ms period, it will run once at start,
	 * then once 750 ms later, but not 1500 ms later, since the
	 * timer is already stopped then. */
	c[2] = 3;
	timer_add(&ctx, 0, 0.75, 1, action_dec_to_0, &c[2]);

	/* 3 --> 2, will run at start, but no cyclic in 1 second */
	c[3] = 3;
	timer_add(&ctx, 0, 2.5, 1, action_dec_to_0, &c[3]);

	/* 3 --> 3, will not run at start */
	c[4] = 3;
	timer_add(&ctx, 2.5, 0.1, 1, action_dec_to_0, &c[4]);

	/* 3 --> 2, an absolute timer in the past (-123.456) will still
	 * run once at start, and then with the period */
	c[5] = 3;
	timer_add(&ctx, -123.456, 2.5, 0, action_dec_to_0, &c[5]);

	/* 3 --> 1, an absolute timer in the past (-123.456) will still
	 * run once at start, and then with the period */
	c[6] = 3;
	timer_add(&ctx, -123.456, 0.75, 0, action_dec_to_0, &c[6]);

	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will run */

	mark_point();
	ctx.stop_flag = 99; /* End timer thread */
	mark_point();

	mg_sleep(1000); /* Sleep 1 second - timer will not run */

	mark_point();

	timers_exit(&ctx);

	mark_point();
	mg_sleep(100);

	ck_assert_int_eq(c[0], 2);
	ck_assert_int_eq(c[1], 0);
	ck_assert_int_eq(c[2], 1);
	ck_assert_int_eq(c[3], 2);
	ck_assert_int_eq(c[4], 3);
	ck_assert_int_eq(c[5], 2);
	ck_assert_int_eq(c[6], 1);
}
END_TEST


#if !defined(REPLACE_CHECK_FOR_LOCAL_DEBUGGING)
Suite *
make_timertest_suite(void)
{
	Suite *const suite = suite_create("Timer");

	TCase *const tcase_timer_cyclic = tcase_create("Timer Periodic");
	TCase *const tcase_timer_oneshot = tcase_create("Timer Single Shot");
	TCase *const tcase_timer_mixed = tcase_create("Timer Mixed");

	tcase_add_test(tcase_timer_cyclic, test_timer_cyclic);
	tcase_set_timeout(tcase_timer_cyclic, 30);
	suite_add_tcase(suite, tcase_timer_cyclic);

	tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_timer_add);
	tcase_add_test(tcase_timer_oneshot, test_timer_oneshot_by_callback_retval);
	tcase_set_timeout(tcase_timer_oneshot, 30);
	suite_add_tcase(suite, tcase_timer_oneshot);

	tcase_add_test(tcase_timer_mixed, test_timer_mixed);
	tcase_set_timeout(tcase_timer_mixed, 30);
	suite_add_tcase(suite, tcase_timer_mixed);

	return suite;
}
#endif


#ifdef REPLACE_CHECK_FOR_LOCAL_DEBUGGING
/* Used to debug test cases without using the check framework */

void
TIMER_PRIVATE(void)
{
	unsigned f_avail;
	unsigned f_ret;

#if defined(_WIN32)
	WSADATA data;
	WSAStartup(MAKEWORD(2, 2), &data);
#endif

	f_avail = mg_check_feature(0xFF);
	f_ret = mg_init_library(f_avail);
	ck_assert_uint_eq(f_ret, f_avail);

	test_timer_cyclic(0);
	test_timer_oneshot_by_timer_add(0);
	test_timer_oneshot_by_callback_retval(0);
	test_timer_mixed(0);

	mg_exit_library();

#if defined(_WIN32)
	WSACleanup();
#endif
}

#endif