]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_timer_correctness.c
Merge pull request #770 from donaldsharp/zclient
[mirror_frr.git] / tests / lib / test_timer_correctness.c
1 /*
2 * Test program to verify that scheduled timers are executed in the
3 * correct order.
4 *
5 * Copyright (C) 2013 by Open Source Routing.
6 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
7 *
8 * This file is part of Quagga
9 *
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <zebra.h>
26
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include "memory.h"
31 #include "pqueue.h"
32 #include "prng.h"
33 #include "thread.h"
34
35 #define SCHEDULE_TIMERS 800
36 #define REMOVE_TIMERS 200
37
38 #define TIMESTR_LEN strlen("4294967296.999999")
39
40 struct thread_master *master;
41
42 static size_t log_buf_len;
43 static size_t log_buf_pos;
44 static char *log_buf;
45
46 static size_t expected_buf_len;
47 static size_t expected_buf_pos;
48 static char *expected_buf;
49
50 static struct prng *prng;
51
52 static struct thread **timers;
53
54 static int timers_pending;
55
56 static void terminate_test(void)
57 {
58 int exit_code;
59
60 if (strcmp(log_buf, expected_buf))
61 {
62 fprintf(stderr, "Expected output and received output differ.\n");
63 fprintf(stderr, "---Expected output: ---\n%s", expected_buf);
64 fprintf(stderr, "---Actual output: ---\n%s", log_buf);
65 exit_code = 1;
66 }
67 else
68 {
69 printf("Expected output and actual output match.\n");
70 exit_code = 0;
71 }
72
73 thread_master_free(master);
74 XFREE(MTYPE_TMP, log_buf);
75 XFREE(MTYPE_TMP, expected_buf);
76 prng_free(prng);
77 XFREE(MTYPE_TMP, timers);
78
79 exit(exit_code);
80 }
81
82 static int timer_func(struct thread *thread)
83 {
84 int rv;
85
86 rv = snprintf(log_buf + log_buf_pos, log_buf_len - log_buf_pos,
87 "%s\n", (char*)thread->arg);
88 assert(rv >= 0);
89 log_buf_pos += rv;
90 assert(log_buf_pos < log_buf_len);
91 XFREE(MTYPE_TMP, thread->arg);
92
93 timers_pending--;
94 if (!timers_pending)
95 terminate_test();
96
97 return 0;
98 }
99
100 static int cmp_timeval(const void* a, const void *b)
101 {
102 const struct timeval *ta = *(struct timeval * const *)a;
103 const struct timeval *tb = *(struct timeval * const *)b;
104
105 if (timercmp(ta, tb, <))
106 return -1;
107 if (timercmp(ta, tb, >))
108 return 1;
109 return 0;
110 }
111
112 int main(int argc, char **argv)
113 {
114 int i, j;
115 struct thread t;
116 struct timeval **alarms;
117
118 master = thread_master_create(NULL);
119
120 log_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1;
121 log_buf_pos = 0;
122 log_buf = XMALLOC(MTYPE_TMP, log_buf_len);
123
124 expected_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1;
125 expected_buf_pos = 0;
126 expected_buf = XMALLOC(MTYPE_TMP, expected_buf_len);
127
128 prng = prng_new(0);
129
130 timers = XMALLOC(MTYPE_TMP, SCHEDULE_TIMERS * sizeof(*timers));
131
132 for (i = 0; i < SCHEDULE_TIMERS; i++)
133 {
134 long interval_msec;
135 int ret;
136 char *arg;
137
138 /* Schedule timers to expire in 0..5 seconds */
139 interval_msec = prng_rand(prng) % 5000;
140 arg = XMALLOC(MTYPE_TMP, TIMESTR_LEN + 1);
141 timers[i] = NULL;
142 thread_add_timer_msec(master, timer_func, arg, interval_msec,
143 &timers[i]);
144 ret = snprintf(arg, TIMESTR_LEN + 1, "%lld.%06lld",
145 (long long)timers[i]->u.sands.tv_sec,
146 (long long)timers[i]->u.sands.tv_usec);
147 assert(ret > 0);
148 assert((size_t)ret < TIMESTR_LEN + 1);
149 timers_pending++;
150 }
151
152 for (i = 0; i < REMOVE_TIMERS; i++)
153 {
154 int index;
155
156 index = prng_rand(prng) % SCHEDULE_TIMERS;
157 if (!timers[index])
158 continue;
159
160 XFREE(MTYPE_TMP, timers[index]->arg);
161 thread_cancel(timers[index]);
162 timers[index] = NULL;
163 timers_pending--;
164 }
165
166 /* We create an array of pointers to the alarm times and sort
167 * that array. That sorted array is used to generate a string
168 * representing the expected "output" of the timers when they
169 * are run. */
170 j = 0;
171 alarms = XMALLOC(MTYPE_TMP, timers_pending * sizeof(*alarms));
172 for (i = 0; i < SCHEDULE_TIMERS; i++)
173 {
174 if (!timers[i])
175 continue;
176 alarms[j++] = &timers[i]->u.sands;
177 }
178 qsort(alarms, j, sizeof(*alarms), cmp_timeval);
179 for (i = 0; i < j; i++)
180 {
181 int ret;
182
183 ret = snprintf(expected_buf + expected_buf_pos,
184 expected_buf_len - expected_buf_pos,
185 "%lld.%06lld\n",
186 (long long)alarms[i]->tv_sec,
187 (long long)alarms[i]->tv_usec);
188 assert(ret > 0);
189 expected_buf_pos += ret;
190 assert(expected_buf_pos < expected_buf_len);
191 }
192 XFREE(MTYPE_TMP, alarms);
193
194 while (thread_fetch(master, &t))
195 thread_call(&t);
196
197 return 0;
198 }