]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_timer_correctness.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / lib / test_timer_correctness.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
ba32db1e
CF
2/*
3 * Test program to verify that scheduled timers are executed in the
4 * correct order.
5 *
6 * Copyright (C) 2013 by Open Source Routing.
7 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
8 *
9 * This file is part of Quagga
ba32db1e
CF
10 */
11
7a2fbbf0
DL
12#include <zebra.h>
13
ba32db1e
CF
14#include <stdio.h>
15#include <unistd.h>
16
ba32db1e 17#include "memory.h"
ba32db1e
CF
18#include "prng.h"
19#include "thread.h"
20
21#define SCHEDULE_TIMERS 800
22#define REMOVE_TIMERS 200
23
24#define TIMESTR_LEN strlen("4294967296.999999")
25
26struct thread_master *master;
27
28static size_t log_buf_len;
29static size_t log_buf_pos;
30static char *log_buf;
31
32static size_t expected_buf_len;
33static size_t expected_buf_pos;
34static char *expected_buf;
35
36static struct prng *prng;
37
38static struct thread **timers;
39
40static int timers_pending;
41
42static void terminate_test(void)
43{
d62a17ae 44 int exit_code;
45
46 if (strcmp(log_buf, expected_buf)) {
47 fprintf(stderr,
48 "Expected output and received output differ.\n");
49 fprintf(stderr, "---Expected output: ---\n%s", expected_buf);
50 fprintf(stderr, "---Actual output: ---\n%s", log_buf);
51 exit_code = 1;
52 } else {
53 printf("Expected output and actual output match.\n");
54 exit_code = 0;
55 }
56
57 thread_master_free(master);
58 XFREE(MTYPE_TMP, log_buf);
59 XFREE(MTYPE_TMP, expected_buf);
60 prng_free(prng);
61 XFREE(MTYPE_TMP, timers);
62
63 exit(exit_code);
ba32db1e
CF
64}
65
cc9f21da 66static void timer_func(struct thread *thread)
ba32db1e 67{
d62a17ae 68 int rv;
ba32db1e 69
d62a17ae 70 rv = snprintf(log_buf + log_buf_pos, log_buf_len - log_buf_pos, "%s\n",
71 (char *)thread->arg);
72 assert(rv >= 0);
73 log_buf_pos += rv;
74 assert(log_buf_pos < log_buf_len);
75 XFREE(MTYPE_TMP, thread->arg);
ba32db1e 76
d62a17ae 77 timers_pending--;
78 if (!timers_pending)
79 terminate_test();
ba32db1e
CF
80}
81
d62a17ae 82static int cmp_timeval(const void *a, const void *b)
ba32db1e 83{
9d303b37
DL
84 const struct timeval *ta = *(struct timeval * const *)a;
85 const struct timeval *tb = *(struct timeval * const *)b;
d62a17ae 86
87 if (timercmp(ta, tb, <))
88 return -1;
89 if (timercmp(ta, tb, >))
90 return 1;
91 return 0;
ba32db1e
CF
92}
93
94int main(int argc, char **argv)
95{
d62a17ae 96 int i, j;
97 struct thread t;
98 struct timeval **alarms;
99
100 master = thread_master_create(NULL);
101
102 log_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1;
103 log_buf_pos = 0;
104 log_buf = XMALLOC(MTYPE_TMP, log_buf_len);
105
106 expected_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1;
107 expected_buf_pos = 0;
108 expected_buf = XMALLOC(MTYPE_TMP, expected_buf_len);
109
110 prng = prng_new(0);
111
b7597a85 112 timers = XCALLOC(MTYPE_TMP, SCHEDULE_TIMERS * sizeof(*timers));
d62a17ae 113
114 for (i = 0; i < SCHEDULE_TIMERS; i++) {
115 long interval_msec;
116 int ret;
117 char *arg;
118
119 /* Schedule timers to expire in 0..5 seconds */
120 interval_msec = prng_rand(prng) % 5000;
121 arg = XMALLOC(MTYPE_TMP, TIMESTR_LEN + 1);
d62a17ae 122 thread_add_timer_msec(master, timer_func, arg, interval_msec,
123 &timers[i]);
124 ret = snprintf(arg, TIMESTR_LEN + 1, "%lld.%06lld",
125 (long long)timers[i]->u.sands.tv_sec,
126 (long long)timers[i]->u.sands.tv_usec);
127 assert(ret > 0);
128 assert((size_t)ret < TIMESTR_LEN + 1);
129 timers_pending++;
130 }
131
132 for (i = 0; i < REMOVE_TIMERS; i++) {
133 int index;
134
135 index = prng_rand(prng) % SCHEDULE_TIMERS;
136 if (!timers[index])
137 continue;
138
139 XFREE(MTYPE_TMP, timers[index]->arg);
b3d6bc6e 140 thread_cancel(&timers[index]);
d62a17ae 141 timers_pending--;
142 }
143
144 /* We create an array of pointers to the alarm times and sort
145 * that array. That sorted array is used to generate a string
146 * representing the expected "output" of the timers when they
147 * are run. */
148 j = 0;
b7597a85 149 alarms = XCALLOC(MTYPE_TMP, timers_pending * sizeof(*alarms));
d62a17ae 150 for (i = 0; i < SCHEDULE_TIMERS; i++) {
151 if (!timers[i])
152 continue;
153 alarms[j++] = &timers[i]->u.sands;
154 }
155 qsort(alarms, j, sizeof(*alarms), cmp_timeval);
156 for (i = 0; i < j; i++) {
157 int ret;
158
159 ret = snprintf(expected_buf + expected_buf_pos,
160 expected_buf_len - expected_buf_pos,
161 "%lld.%06lld\n", (long long)alarms[i]->tv_sec,
162 (long long)alarms[i]->tv_usec);
163 assert(ret > 0);
164 expected_buf_pos += ret;
165 assert(expected_buf_pos < expected_buf_len);
166 }
167 XFREE(MTYPE_TMP, alarms);
168
169 while (thread_fetch(master, &t))
170 thread_call(&t);
171
172 return 0;
ba32db1e 173}