File: test-timer-list.c

package info (click to toggle)
corosync-qdevice 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,516 kB
  • sloc: ansic: 22,520; sh: 11,882; makefile: 391
file content (431 lines) | stat: -rw-r--r-- 11,515 bytes parent folder | download | duplicates (3)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (c) 2020-2021 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Jan Friesse (jfriesse@redhat.com)
 *
 * This software licensed under BSD license, the text of which follows:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the Red Hat, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <poll.h>

#include "timer-list.h"

#define SHORT_TIMEOUT			100
#define LONG_TIMEOUT			(60 * 1000)

#define SPEED_TEST_NO_ITEMS		10000

#define HEAP_TEST_NO_ITEMS		20
/*
 * Valid heap checking is slow
 */
#define HEAP_SPEED_TEST_NO_ITEMS	1000

static int timer_list_fn1_called = 0;

/*
 * Reimplementation of timer_list_entry_time_to_expire
 */
static uint8_t
time_to_expire(uint8_t expire_time, uint8_t current_time)
{
	uint8_t diff, half_interval;

	diff = expire_time - current_time;
	half_interval = ~0;
	half_interval /= 2;

	if (diff > half_interval) {
		return (0);
	}

	return (diff);
}

static void
check_time_to_expire(void)
{
	unsigned int current_time, delta, expire_time, res;

	for (current_time = 0; current_time <= 255 * 2; current_time++) {
		for (delta = 0; delta < 255; delta++) {
			expire_time = current_time + delta;

			res = time_to_expire((uint8_t)expire_time, (uint8_t)current_time);

			if (delta < 128) {
				assert(res == delta);
			} else {
				assert(res == 0);
			}
		}
	}
}

static int
timer_list_fn1(void *data1, void *data2)
{

	timer_list_fn1_called++;
	assert(data1 == &timer_list_fn1_called);
	assert(data2 == timer_list_fn1);

	return (0);
}

static void
check_timer_list_basics(void)
{
	struct timer_list tlist;
	struct timer_list_entry *tlist_entry;
	struct timer_list_entry *tlist_speed_entry[SPEED_TEST_NO_ITEMS];
	int i;

	timer_list_init(&tlist);

	assert(timer_list_add(&tlist, 0, timer_list_fn1, NULL, NULL) == NULL);
	assert(timer_list_add(&tlist, TIMER_LIST_MAX_INTERVAL + 1, timer_list_fn1, NULL, NULL) == NULL);
	assert(timer_list_add(&tlist, 1, NULL, NULL, NULL) == NULL);

	/*
	 * callback is called
	 */
	timer_list_fn1_called = 0;
	tlist_entry = timer_list_add(&tlist, SHORT_TIMEOUT / 2, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
	assert(tlist_entry != NULL);
	(void)poll(NULL, 0, SHORT_TIMEOUT);
	assert(timer_list_time_to_expire(&tlist) == 0);
	assert(timer_list_time_to_expire_ms(&tlist) == 0);
	timer_list_expire(&tlist);
	assert(timer_list_fn1_called == 1);

	assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
	assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));
	timer_list_expire(&tlist);
	assert(timer_list_fn1_called == 1);

	/*
	 * Callback is not called
	 */
	timer_list_fn1_called = 0;
	tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
	assert(tlist_entry != NULL);
	(void)poll(NULL, 0, SHORT_TIMEOUT);
	assert(timer_list_time_to_expire(&tlist) > 0);
	assert(timer_list_time_to_expire_ms(&tlist) > 0);
	timer_list_expire(&tlist);
	assert(timer_list_fn1_called == 0);

	/*
	 * Delete entry
	 */
	timer_list_entry_delete(&tlist, tlist_entry);
	assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);
	assert(timer_list_time_to_expire_ms(&tlist) == ~((uint32_t)0));

	/*
	 * Check changing of interval
	 */
	timer_list_fn1_called = 0;
	tlist_entry = timer_list_add(&tlist, LONG_TIMEOUT, timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
	assert(tlist_entry != NULL);
	assert(timer_list_entry_set_interval(&tlist, tlist_entry, SHORT_TIMEOUT) == 0);
	(void)poll(NULL, 0, SHORT_TIMEOUT);
	assert(timer_list_time_to_expire(&tlist) == 0);
	assert(timer_list_time_to_expire_ms(&tlist) == 0);
	timer_list_expire(&tlist);
	assert(timer_list_fn1_called == 1);

	/*
	 * Test speed and more entries
	 */
	timer_list_fn1_called = 0;

	for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
		tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
		    timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
		assert(tlist_speed_entry[i] != NULL);
	}

	for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
		timer_list_entry_reschedule(&tlist, tlist_speed_entry[i]);
	}

	(void)poll(NULL, 0, SHORT_TIMEOUT);
	timer_list_expire(&tlist);
	assert(timer_list_fn1_called == SPEED_TEST_NO_ITEMS);

	timer_list_free(&tlist);
}

static void
check_timer_heap(void)
{
	struct timer_list tlist;
	uint32_t u32;
	int i;
	int j;
	struct timer_list_entry *tlist_entry[HEAP_TEST_NO_ITEMS];
	struct timer_list_entry *tlist_entry_small;
	struct timer_list_entry *tlist_speed_entry[HEAP_SPEED_TEST_NO_ITEMS];

	timer_list_init(&tlist);

	/*
	 * Empty tlist
	 */
	assert(tlist.allocated == 0);
	assert(tlist.size == 0);

	u32 = ~((uint32_t)0);
	assert(timer_list_time_to_expire_ms(&tlist) == u32);
	assert(timer_list_time_to_expire(&tlist) == PR_INTERVAL_NO_TIMEOUT);

	/*
	 * Adding increasing numbers keeps heap property so there should be no reshufling
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i] != NULL);
		assert(tlist.size == i + 1);

		assert(timer_list_debug_is_valid_heap(&tlist));

		for (j = 0; j < i + 1; j++) {
			assert(tlist.entries[j] == tlist_entry[j]);
		}
	}

	/*
	 * Add small item which should become first item in tlist entries to keep heap
	 * property
	 */
	tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
	assert(timer_list_debug_is_valid_heap(&tlist));
	assert(tlist.size == i + 1);
	assert(tlist.entries[0] == tlist_entry_small);
	assert(timer_list_entry_get_interval(tlist_entry_small) == SHORT_TIMEOUT);

	/*
	 * Remove all items
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		timer_list_entry_delete(&tlist, tlist_entry[i]);

		assert(timer_list_debug_is_valid_heap(&tlist));
		assert(tlist.entries[0] == tlist_entry_small);
		assert(timer_list_entry_get_interval(tlist_entry[i]) == LONG_TIMEOUT * (i + 1));
	}

	/*
	 * Remove small item
	 */
	timer_list_entry_delete(&tlist, tlist_entry_small);
	assert(timer_list_debug_is_valid_heap(&tlist));

	/*
	 * Add items in reverse order
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i] != NULL);
		assert(tlist.size == i + 1);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	/*
	 * Remove all items
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		timer_list_entry_delete(&tlist, tlist_entry[i]);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	/*
	 * Add items in standard and reverse order
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
		tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i * 2 + 1] != NULL);
		assert(tlist.size == (i * 2) + 1);

		tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i * 2 + 1] != NULL);
		assert(tlist.size == (i * 2) + 2);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	/*
	 * Remove items
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		timer_list_entry_delete(&tlist, tlist_entry[i]);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	assert(tlist.size == 0);

	/*
	 * Add items again
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS / 2; i++) {
		tlist_entry[i * 2] = timer_list_add(&tlist, LONG_TIMEOUT * ((HEAP_TEST_NO_ITEMS - i) + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i * 2] != NULL);
		assert(tlist.size == (i * 2) + 1);

		tlist_entry[i * 2 + 1] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i * 2 + 1] != NULL);
		assert(tlist.size == (i * 2) + 2);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	tlist_entry_small = timer_list_add(&tlist, SHORT_TIMEOUT, timer_list_fn1, NULL, NULL);
	assert(timer_list_debug_is_valid_heap(&tlist));
	assert(tlist.entries[0] == tlist_entry_small);

	/*
	 * And try reschedule
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		timer_list_entry_reschedule(&tlist, tlist_entry[i]);

		assert(timer_list_debug_is_valid_heap(&tlist));

		assert(tlist.entries[0] == tlist_entry_small);
	}

	/*
	 * Try delete
	 */
	timer_list_entry_delete(&tlist, tlist_entry_small);
	assert(timer_list_debug_is_valid_heap(&tlist));

	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		timer_list_entry_delete(&tlist, tlist_entry[i]);

		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	assert(tlist.size == 0);

	/*
	 * Add items again in increasing order
	 */
	for (i = 0; i < HEAP_TEST_NO_ITEMS; i++) {
		tlist_entry[i] = timer_list_add(&tlist, LONG_TIMEOUT * (i + 1),
		    timer_list_fn1, NULL, NULL);

		assert(tlist_entry[i] != NULL);
		assert(tlist.size == i + 1);

		assert(timer_list_debug_is_valid_heap(&tlist));

		for (j = 0; j < i + 1; j++) {
			assert(tlist.entries[j] == tlist_entry[j]);
		}
	}


	/*
	 * Try delete every third item and test if heap property is kept
	 */
	i = 0;
	while (tlist.size > 0) {
		i = (i + 3) % HEAP_TEST_NO_ITEMS;

		while (tlist_entry[i] == NULL) {
			i = (i + 1) % HEAP_TEST_NO_ITEMS;
		}

		timer_list_entry_delete(&tlist, tlist_entry[i]);
		tlist_entry[i] = NULL;
		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	assert(tlist.size == 0);

	/*
	 * Speed test
	 */
	for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
		tlist_speed_entry[i] = timer_list_add(&tlist, SHORT_TIMEOUT / 2,
		    timer_list_fn1, &timer_list_fn1_called, timer_list_fn1);
		assert(tlist_speed_entry[i] != NULL);
		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	for (i = 0; i < HEAP_SPEED_TEST_NO_ITEMS; i++) {
		timer_list_entry_reschedule(&tlist, tlist_speed_entry[i]);
		assert(timer_list_debug_is_valid_heap(&tlist));
	}

	/*
	 * Free list
	 */
	timer_list_free(&tlist);
}

int
main(void)
{

	PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);

	check_time_to_expire();

	check_timer_heap();

	check_timer_list_basics();

	assert(PR_Cleanup() == PR_SUCCESS);

	return (0);
}