]>
Commit | Line | Data |
---|---|---|
ba32db1e CF |
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 | * | |
896014f4 DL |
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 | |
ba32db1e CF |
23 | */ |
24 | ||
7a2fbbf0 DL |
25 | #include <zebra.h> |
26 | ||
ba32db1e CF |
27 | #include <stdio.h> |
28 | #include <unistd.h> | |
29 | ||
ba32db1e | 30 | #include "memory.h" |
ba32db1e CF |
31 | #include "prng.h" |
32 | #include "thread.h" | |
33 | ||
34 | #define SCHEDULE_TIMERS 800 | |
35 | #define REMOVE_TIMERS 200 | |
36 | ||
37 | #define TIMESTR_LEN strlen("4294967296.999999") | |
38 | ||
39 | struct thread_master *master; | |
40 | ||
41 | static size_t log_buf_len; | |
42 | static size_t log_buf_pos; | |
43 | static char *log_buf; | |
44 | ||
45 | static size_t expected_buf_len; | |
46 | static size_t expected_buf_pos; | |
47 | static char *expected_buf; | |
48 | ||
49 | static struct prng *prng; | |
50 | ||
51 | static struct thread **timers; | |
52 | ||
53 | static int timers_pending; | |
54 | ||
55 | static void terminate_test(void) | |
56 | { | |
d62a17ae | 57 | int exit_code; |
58 | ||
59 | if (strcmp(log_buf, expected_buf)) { | |
60 | fprintf(stderr, | |
61 | "Expected output and received output differ.\n"); | |
62 | fprintf(stderr, "---Expected output: ---\n%s", expected_buf); | |
63 | fprintf(stderr, "---Actual output: ---\n%s", log_buf); | |
64 | exit_code = 1; | |
65 | } else { | |
66 | printf("Expected output and actual output match.\n"); | |
67 | exit_code = 0; | |
68 | } | |
69 | ||
70 | thread_master_free(master); | |
71 | XFREE(MTYPE_TMP, log_buf); | |
72 | XFREE(MTYPE_TMP, expected_buf); | |
73 | prng_free(prng); | |
74 | XFREE(MTYPE_TMP, timers); | |
75 | ||
76 | exit(exit_code); | |
ba32db1e CF |
77 | } |
78 | ||
cc9f21da | 79 | static void timer_func(struct thread *thread) |
ba32db1e | 80 | { |
d62a17ae | 81 | int rv; |
ba32db1e | 82 | |
d62a17ae | 83 | rv = snprintf(log_buf + log_buf_pos, log_buf_len - log_buf_pos, "%s\n", |
84 | (char *)thread->arg); | |
85 | assert(rv >= 0); | |
86 | log_buf_pos += rv; | |
87 | assert(log_buf_pos < log_buf_len); | |
88 | XFREE(MTYPE_TMP, thread->arg); | |
ba32db1e | 89 | |
d62a17ae | 90 | timers_pending--; |
91 | if (!timers_pending) | |
92 | terminate_test(); | |
ba32db1e CF |
93 | } |
94 | ||
d62a17ae | 95 | static int cmp_timeval(const void *a, const void *b) |
ba32db1e | 96 | { |
9d303b37 DL |
97 | const struct timeval *ta = *(struct timeval * const *)a; |
98 | const struct timeval *tb = *(struct timeval * const *)b; | |
d62a17ae | 99 | |
100 | if (timercmp(ta, tb, <)) | |
101 | return -1; | |
102 | if (timercmp(ta, tb, >)) | |
103 | return 1; | |
104 | return 0; | |
ba32db1e CF |
105 | } |
106 | ||
107 | int main(int argc, char **argv) | |
108 | { | |
d62a17ae | 109 | int i, j; |
110 | struct thread t; | |
111 | struct timeval **alarms; | |
112 | ||
113 | master = thread_master_create(NULL); | |
114 | ||
115 | log_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1; | |
116 | log_buf_pos = 0; | |
117 | log_buf = XMALLOC(MTYPE_TMP, log_buf_len); | |
118 | ||
119 | expected_buf_len = SCHEDULE_TIMERS * (TIMESTR_LEN + 1) + 1; | |
120 | expected_buf_pos = 0; | |
121 | expected_buf = XMALLOC(MTYPE_TMP, expected_buf_len); | |
122 | ||
123 | prng = prng_new(0); | |
124 | ||
b7597a85 | 125 | timers = XCALLOC(MTYPE_TMP, SCHEDULE_TIMERS * sizeof(*timers)); |
d62a17ae | 126 | |
127 | for (i = 0; i < SCHEDULE_TIMERS; i++) { | |
128 | long interval_msec; | |
129 | int ret; | |
130 | char *arg; | |
131 | ||
132 | /* Schedule timers to expire in 0..5 seconds */ | |
133 | interval_msec = prng_rand(prng) % 5000; | |
134 | arg = XMALLOC(MTYPE_TMP, TIMESTR_LEN + 1); | |
d62a17ae | 135 | thread_add_timer_msec(master, timer_func, arg, interval_msec, |
136 | &timers[i]); | |
137 | ret = snprintf(arg, TIMESTR_LEN + 1, "%lld.%06lld", | |
138 | (long long)timers[i]->u.sands.tv_sec, | |
139 | (long long)timers[i]->u.sands.tv_usec); | |
140 | assert(ret > 0); | |
141 | assert((size_t)ret < TIMESTR_LEN + 1); | |
142 | timers_pending++; | |
143 | } | |
144 | ||
145 | for (i = 0; i < REMOVE_TIMERS; i++) { | |
146 | int index; | |
147 | ||
148 | index = prng_rand(prng) % SCHEDULE_TIMERS; | |
149 | if (!timers[index]) | |
150 | continue; | |
151 | ||
152 | XFREE(MTYPE_TMP, timers[index]->arg); | |
b3d6bc6e | 153 | thread_cancel(&timers[index]); |
d62a17ae | 154 | timers_pending--; |
155 | } | |
156 | ||
157 | /* We create an array of pointers to the alarm times and sort | |
158 | * that array. That sorted array is used to generate a string | |
159 | * representing the expected "output" of the timers when they | |
160 | * are run. */ | |
161 | j = 0; | |
b7597a85 | 162 | alarms = XCALLOC(MTYPE_TMP, timers_pending * sizeof(*alarms)); |
d62a17ae | 163 | for (i = 0; i < SCHEDULE_TIMERS; i++) { |
164 | if (!timers[i]) | |
165 | continue; | |
166 | alarms[j++] = &timers[i]->u.sands; | |
167 | } | |
168 | qsort(alarms, j, sizeof(*alarms), cmp_timeval); | |
169 | for (i = 0; i < j; i++) { | |
170 | int ret; | |
171 | ||
172 | ret = snprintf(expected_buf + expected_buf_pos, | |
173 | expected_buf_len - expected_buf_pos, | |
174 | "%lld.%06lld\n", (long long)alarms[i]->tv_sec, | |
175 | (long long)alarms[i]->tv_usec); | |
176 | assert(ret > 0); | |
177 | expected_buf_pos += ret; | |
178 | assert(expected_buf_pos < expected_buf_len); | |
179 | } | |
180 | XFREE(MTYPE_TMP, alarms); | |
181 | ||
182 | while (thread_fetch(master, &t)) | |
183 | thread_call(&t); | |
184 | ||
185 | return 0; | |
ba32db1e | 186 | } |