xref: /freebsd/contrib/libevent/test/test-time.c (revision b50261e2)
1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3c43e99fdSEd Maste  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4c43e99fdSEd Maste  *
5c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
6c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
7c43e99fdSEd Maste  * are met:
8c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
9c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
10c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
12c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
13c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
14c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
15c43e99fdSEd Maste  *
16c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c43e99fdSEd Maste  */
27c43e99fdSEd Maste #include "event2/event-config.h"
28c43e99fdSEd Maste #include "util-internal.h"
29c43e99fdSEd Maste 
30c43e99fdSEd Maste #include <sys/types.h>
31c43e99fdSEd Maste #include <sys/stat.h>
32c43e99fdSEd Maste #include <fcntl.h>
33c43e99fdSEd Maste #include <stdlib.h>
34c43e99fdSEd Maste #include <stdio.h>
35c43e99fdSEd Maste #include <string.h>
36c43e99fdSEd Maste #ifndef _WIN32
37c43e99fdSEd Maste #include <unistd.h>
38c43e99fdSEd Maste #include <sys/time.h>
39c43e99fdSEd Maste #endif
40c43e99fdSEd Maste #include <errno.h>
41c43e99fdSEd Maste 
42c43e99fdSEd Maste #include "event2/event.h"
43c43e99fdSEd Maste #include "event2/event_compat.h"
44c43e99fdSEd Maste #include "event2/event_struct.h"
45c43e99fdSEd Maste 
46c43e99fdSEd Maste int called = 0;
47c43e99fdSEd Maste 
48c43e99fdSEd Maste #define NEVENT	20000
49c43e99fdSEd Maste 
50c43e99fdSEd Maste struct event *ev[NEVENT];
51c43e99fdSEd Maste 
52c43e99fdSEd Maste struct evutil_weakrand_state weakrand_state;
53c43e99fdSEd Maste 
54c43e99fdSEd Maste static int
rand_int(int n)55c43e99fdSEd Maste rand_int(int n)
56c43e99fdSEd Maste {
57c43e99fdSEd Maste 	return evutil_weakrand_(&weakrand_state) % n;
58c43e99fdSEd Maste }
59c43e99fdSEd Maste 
60c43e99fdSEd Maste static void
time_cb(evutil_socket_t fd,short event,void * arg)61c43e99fdSEd Maste time_cb(evutil_socket_t fd, short event, void *arg)
62c43e99fdSEd Maste {
63c43e99fdSEd Maste 	struct timeval tv;
64c43e99fdSEd Maste 	int i, j;
65c43e99fdSEd Maste 
66c43e99fdSEd Maste 	called++;
67c43e99fdSEd Maste 
68c43e99fdSEd Maste 	if (called < 10*NEVENT) {
69c43e99fdSEd Maste 		for (i = 0; i < 10; i++) {
70c43e99fdSEd Maste 			j = rand_int(NEVENT);
71c43e99fdSEd Maste 			tv.tv_sec = 0;
72c43e99fdSEd Maste 			tv.tv_usec = rand_int(50000);
73c43e99fdSEd Maste 			if (tv.tv_usec % 2 || called < NEVENT)
74c43e99fdSEd Maste 				evtimer_add(ev[j], &tv);
75c43e99fdSEd Maste 			else
76c43e99fdSEd Maste 				evtimer_del(ev[j]);
77c43e99fdSEd Maste 		}
78c43e99fdSEd Maste 	}
79c43e99fdSEd Maste }
80c43e99fdSEd Maste 
81c43e99fdSEd Maste int
main(int argc,char ** argv)82c43e99fdSEd Maste main(int argc, char **argv)
83c43e99fdSEd Maste {
84*b50261e2SCy Schubert 	struct event_base *base;
85c43e99fdSEd Maste 	struct timeval tv;
86c43e99fdSEd Maste 	int i;
87*b50261e2SCy Schubert 
88c43e99fdSEd Maste #ifdef _WIN32
89c43e99fdSEd Maste 	WORD wVersionRequested;
90c43e99fdSEd Maste 	WSADATA wsaData;
91c43e99fdSEd Maste 
92c43e99fdSEd Maste 	wVersionRequested = MAKEWORD(2, 2);
93c43e99fdSEd Maste 
94c43e99fdSEd Maste 	(void) WSAStartup(wVersionRequested, &wsaData);
95c43e99fdSEd Maste #endif
96c43e99fdSEd Maste 
97c43e99fdSEd Maste 	evutil_weakrand_seed_(&weakrand_state, 0);
98c43e99fdSEd Maste 
99*b50261e2SCy Schubert 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
100*b50261e2SCy Schubert 		event_enable_debug_logging(EVENT_DBG_ALL);
101*b50261e2SCy Schubert 	}
102*b50261e2SCy Schubert 
103*b50261e2SCy Schubert 	base = event_base_new();
104c43e99fdSEd Maste 
105c43e99fdSEd Maste 	for (i = 0; i < NEVENT; i++) {
106*b50261e2SCy Schubert 		ev[i] = evtimer_new(base, time_cb, event_self_cbarg());
107c43e99fdSEd Maste 		tv.tv_sec = 0;
108c43e99fdSEd Maste 		tv.tv_usec = rand_int(50000);
109c43e99fdSEd Maste 		evtimer_add(ev[i], &tv);
110c43e99fdSEd Maste 	}
111c43e99fdSEd Maste 
112*b50261e2SCy Schubert 	i = event_base_dispatch(base);
113c43e99fdSEd Maste 
114*b50261e2SCy Schubert 	printf("event_base_dispatch=%d, called=%d, EVENT=%d\n",
115*b50261e2SCy Schubert 		i, called, NEVENT);
116c43e99fdSEd Maste 
117*b50261e2SCy Schubert 	if (i == 1 && called >= NEVENT) {
118*b50261e2SCy Schubert 		return EXIT_SUCCESS;
119*b50261e2SCy Schubert 	} else {
120*b50261e2SCy Schubert 		return EXIT_FAILURE;
121*b50261e2SCy Schubert 	}
122c43e99fdSEd Maste }
123c43e99fdSEd Maste 
124