]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_timer_correctness.c
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / tests / lib / test_timer_correctness.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
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
10 */
11
12 #include <zebra.h>
13
14 #include <stdio.h>
15 #include <unistd.h>
16
17 #include "memory.h"
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
26 struct thread_master *master;
27
28 static size_t log_buf_len;
29 static size_t log_buf_pos;
30 static char *log_buf;
31
32 static size_t expected_buf_len;
33 static size_t expected_buf_pos;
34 static char *expected_buf;
35
36 static struct prng *prng;
37
38 static struct thread **timers;
39
40 static int timers_pending;
41
42 static void terminate_test(void)
43 {
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);
64 }
65
66 static void timer_func(struct thread *thread)
67 {
68 int rv;
69
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);
76
77 timers_pending--;
78 if (!timers_pending)
79 terminate_test();
80 }
81
82 static int cmp_timeval(const void *a, const void *b)
83 {
84 const struct timeval *ta = *(struct timeval * const *)a;
85 const struct timeval *tb = *(struct timeval * const *)b;
86
87 if (timercmp(ta, tb, <))
88 return -1;
89 if (timercmp(ta, tb, >))
90 return 1;
91 return 0;
92 }
93
94 int main(int argc, char **argv)
95 {
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
112 timers = XCALLOC(MTYPE_TMP, SCHEDULE_TIMERS * sizeof(*timers));
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);
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);
140 thread_cancel(&timers[index]);
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;
149 alarms = XCALLOC(MTYPE_TMP, timers_pending * sizeof(*alarms));
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;
173 }