]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_timer_correctness.c
2 * Test program to verify that scheduled timers are executed in the
5 * Copyright (C) 2013 by Open Source Routing.
6 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of Quagga
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
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.
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
35 #define SCHEDULE_TIMERS 800
36 #define REMOVE_TIMERS 200
38 #define TIMESTR_LEN strlen("4294967296.999999")
40 struct thread_master
*master
;
42 static size_t log_buf_len
;
43 static size_t log_buf_pos
;
46 static size_t expected_buf_len
;
47 static size_t expected_buf_pos
;
48 static char *expected_buf
;
50 static struct prng
*prng
;
52 static struct thread
**timers
;
54 static int timers_pending
;
56 static void terminate_test(void)
60 if (strcmp(log_buf
, expected_buf
)) {
62 "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
);
67 printf("Expected output and actual output match.\n");
71 thread_master_free(master
);
72 XFREE(MTYPE_TMP
, log_buf
);
73 XFREE(MTYPE_TMP
, expected_buf
);
75 XFREE(MTYPE_TMP
, timers
);
80 static int timer_func(struct thread
*thread
)
84 rv
= snprintf(log_buf
+ log_buf_pos
, log_buf_len
- log_buf_pos
, "%s\n",
88 assert(log_buf_pos
< log_buf_len
);
89 XFREE(MTYPE_TMP
, thread
->arg
);
98 static int cmp_timeval(const void *a
, const void *b
)
100 const struct timeval
*ta
= *(struct timeval
* const *)a
;
101 const struct timeval
*tb
= *(struct timeval
* const *)b
;
103 if (timercmp(ta
, tb
, <))
105 if (timercmp(ta
, tb
, >))
110 int main(int argc
, char **argv
)
114 struct timeval
**alarms
;
116 master
= thread_master_create(NULL
);
118 log_buf_len
= SCHEDULE_TIMERS
* (TIMESTR_LEN
+ 1) + 1;
120 log_buf
= XMALLOC(MTYPE_TMP
, log_buf_len
);
122 expected_buf_len
= SCHEDULE_TIMERS
* (TIMESTR_LEN
+ 1) + 1;
123 expected_buf_pos
= 0;
124 expected_buf
= XMALLOC(MTYPE_TMP
, expected_buf_len
);
128 timers
= XMALLOC(MTYPE_TMP
, SCHEDULE_TIMERS
* sizeof(*timers
));
130 for (i
= 0; i
< SCHEDULE_TIMERS
; i
++) {
135 /* Schedule timers to expire in 0..5 seconds */
136 interval_msec
= prng_rand(prng
) % 5000;
137 arg
= XMALLOC(MTYPE_TMP
, TIMESTR_LEN
+ 1);
139 thread_add_timer_msec(master
, timer_func
, arg
, interval_msec
,
141 ret
= snprintf(arg
, TIMESTR_LEN
+ 1, "%lld.%06lld",
142 (long long)timers
[i
]->u
.sands
.tv_sec
,
143 (long long)timers
[i
]->u
.sands
.tv_usec
);
145 assert((size_t)ret
< TIMESTR_LEN
+ 1);
149 for (i
= 0; i
< REMOVE_TIMERS
; i
++) {
152 index
= prng_rand(prng
) % SCHEDULE_TIMERS
;
156 XFREE(MTYPE_TMP
, timers
[index
]->arg
);
157 thread_cancel(timers
[index
]);
158 timers
[index
] = NULL
;
162 /* We create an array of pointers to the alarm times and sort
163 * that array. That sorted array is used to generate a string
164 * representing the expected "output" of the timers when they
167 alarms
= XMALLOC(MTYPE_TMP
, timers_pending
* sizeof(*alarms
));
168 for (i
= 0; i
< SCHEDULE_TIMERS
; i
++) {
171 alarms
[j
++] = &timers
[i
]->u
.sands
;
173 qsort(alarms
, j
, sizeof(*alarms
), cmp_timeval
);
174 for (i
= 0; i
< j
; i
++) {
177 ret
= snprintf(expected_buf
+ expected_buf_pos
,
178 expected_buf_len
- expected_buf_pos
,
179 "%lld.%06lld\n", (long long)alarms
[i
]->tv_sec
,
180 (long long)alarms
[i
]->tv_usec
);
182 expected_buf_pos
+= ret
;
183 assert(expected_buf_pos
< expected_buf_len
);
185 XFREE(MTYPE_TMP
, alarms
);
187 while (thread_fetch(master
, &t
))