xref: /freebsd/contrib/ntp/sntp/libevent/test/test-time.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert #include "event2/event-config.h"
28*a466cc55SCy Schubert #include "util-internal.h"
292b15cb3dSCy Schubert 
302b15cb3dSCy Schubert #include <sys/types.h>
312b15cb3dSCy Schubert #include <sys/stat.h>
322b15cb3dSCy Schubert #include <fcntl.h>
332b15cb3dSCy Schubert #include <stdlib.h>
342b15cb3dSCy Schubert #include <stdio.h>
352b15cb3dSCy Schubert #include <string.h>
362b15cb3dSCy Schubert #ifndef _WIN32
372b15cb3dSCy Schubert #include <unistd.h>
382b15cb3dSCy Schubert #include <sys/time.h>
392b15cb3dSCy Schubert #endif
402b15cb3dSCy Schubert #include <errno.h>
412b15cb3dSCy Schubert 
422b15cb3dSCy Schubert #include "event2/event.h"
432b15cb3dSCy Schubert #include "event2/event_compat.h"
442b15cb3dSCy Schubert #include "event2/event_struct.h"
452b15cb3dSCy Schubert 
462b15cb3dSCy Schubert int called = 0;
472b15cb3dSCy Schubert 
482b15cb3dSCy Schubert #define NEVENT	20000
492b15cb3dSCy Schubert 
502b15cb3dSCy Schubert struct event *ev[NEVENT];
512b15cb3dSCy Schubert 
52a25439b6SCy Schubert struct evutil_weakrand_state weakrand_state;
53a25439b6SCy Schubert 
542b15cb3dSCy Schubert static int
rand_int(int n)552b15cb3dSCy Schubert rand_int(int n)
562b15cb3dSCy Schubert {
57a25439b6SCy Schubert 	return evutil_weakrand_(&weakrand_state) % n;
582b15cb3dSCy Schubert }
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert static void
time_cb(evutil_socket_t fd,short event,void * arg)612b15cb3dSCy Schubert time_cb(evutil_socket_t fd, short event, void *arg)
622b15cb3dSCy Schubert {
632b15cb3dSCy Schubert 	struct timeval tv;
642b15cb3dSCy Schubert 	int i, j;
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert 	called++;
672b15cb3dSCy Schubert 
682b15cb3dSCy Schubert 	if (called < 10*NEVENT) {
692b15cb3dSCy Schubert 		for (i = 0; i < 10; i++) {
702b15cb3dSCy Schubert 			j = rand_int(NEVENT);
712b15cb3dSCy Schubert 			tv.tv_sec = 0;
722b15cb3dSCy Schubert 			tv.tv_usec = rand_int(50000);
73a25439b6SCy Schubert 			if (tv.tv_usec % 2 || called < NEVENT)
742b15cb3dSCy Schubert 				evtimer_add(ev[j], &tv);
752b15cb3dSCy Schubert 			else
762b15cb3dSCy Schubert 				evtimer_del(ev[j]);
772b15cb3dSCy Schubert 		}
782b15cb3dSCy Schubert 	}
792b15cb3dSCy Schubert }
802b15cb3dSCy Schubert 
812b15cb3dSCy Schubert int
main(int argc,char ** argv)822b15cb3dSCy Schubert main(int argc, char **argv)
832b15cb3dSCy Schubert {
84*a466cc55SCy Schubert 	struct event_base *base;
852b15cb3dSCy Schubert 	struct timeval tv;
862b15cb3dSCy Schubert 	int i;
87*a466cc55SCy Schubert 
882b15cb3dSCy Schubert #ifdef _WIN32
892b15cb3dSCy Schubert 	WORD wVersionRequested;
902b15cb3dSCy Schubert 	WSADATA wsaData;
912b15cb3dSCy Schubert 
922b15cb3dSCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
932b15cb3dSCy Schubert 
942b15cb3dSCy Schubert 	(void) WSAStartup(wVersionRequested, &wsaData);
952b15cb3dSCy Schubert #endif
962b15cb3dSCy Schubert 
97a25439b6SCy Schubert 	evutil_weakrand_seed_(&weakrand_state, 0);
98a25439b6SCy Schubert 
99*a466cc55SCy Schubert 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
100*a466cc55SCy Schubert 		event_enable_debug_logging(EVENT_DBG_ALL);
101*a466cc55SCy Schubert 	}
102*a466cc55SCy Schubert 
103*a466cc55SCy Schubert 	base = event_base_new();
1042b15cb3dSCy Schubert 
1052b15cb3dSCy Schubert 	for (i = 0; i < NEVENT; i++) {
106*a466cc55SCy Schubert 		ev[i] = evtimer_new(base, time_cb, event_self_cbarg());
1072b15cb3dSCy Schubert 		tv.tv_sec = 0;
1082b15cb3dSCy Schubert 		tv.tv_usec = rand_int(50000);
1092b15cb3dSCy Schubert 		evtimer_add(ev[i], &tv);
1102b15cb3dSCy Schubert 	}
1112b15cb3dSCy Schubert 
112*a466cc55SCy Schubert 	i = event_base_dispatch(base);
1132b15cb3dSCy Schubert 
114*a466cc55SCy Schubert 	printf("event_base_dispatch=%d, called=%d, EVENT=%d\n",
115*a466cc55SCy Schubert 		i, called, NEVENT);
116a25439b6SCy Schubert 
117*a466cc55SCy Schubert 	if (i == 1 && called >= NEVENT) {
118*a466cc55SCy Schubert 		return EXIT_SUCCESS;
119*a466cc55SCy Schubert 	} else {
120*a466cc55SCy Schubert 		return EXIT_FAILURE;
121*a466cc55SCy Schubert 	}
1222b15cb3dSCy Schubert }
1232b15cb3dSCy Schubert 
124