xref: /netbsd-src/external/bsd/libevent/dist/test/test-time.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1*657871a7Schristos /*	$NetBSD: test-time.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
26ecf6635Schristos /*
36ecf6635Schristos  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
46ecf6635Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
286ecf6635Schristos #include "event2/event-config.h"
296ecf6635Schristos #include <sys/cdefs.h>
30*657871a7Schristos __RCSID("$NetBSD: test-time.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $");
31805a1ce9Schristos #include "util-internal.h"
326ecf6635Schristos 
336ecf6635Schristos #include <sys/types.h>
346ecf6635Schristos #include <sys/stat.h>
356ecf6635Schristos #include <fcntl.h>
366ecf6635Schristos #include <stdlib.h>
376ecf6635Schristos #include <stdio.h>
386ecf6635Schristos #include <string.h>
39805a1ce9Schristos #ifndef _WIN32
406ecf6635Schristos #include <unistd.h>
416ecf6635Schristos #include <sys/time.h>
426ecf6635Schristos #endif
436ecf6635Schristos #include <errno.h>
446ecf6635Schristos 
456ecf6635Schristos #include "event2/event.h"
466ecf6635Schristos #include "event2/event_compat.h"
476ecf6635Schristos #include "event2/event_struct.h"
486ecf6635Schristos 
496ecf6635Schristos int called = 0;
506ecf6635Schristos 
516ecf6635Schristos #define NEVENT	20000
526ecf6635Schristos 
536ecf6635Schristos struct event *ev[NEVENT];
546ecf6635Schristos 
55805a1ce9Schristos struct evutil_weakrand_state weakrand_state;
56805a1ce9Schristos 
576ecf6635Schristos static int
rand_int(int n)586ecf6635Schristos rand_int(int n)
596ecf6635Schristos {
60805a1ce9Schristos 	return evutil_weakrand_(&weakrand_state) % n;
616ecf6635Schristos }
626ecf6635Schristos 
636ecf6635Schristos static void
time_cb(evutil_socket_t fd,short event,void * arg)646ecf6635Schristos time_cb(evutil_socket_t fd, short event, void *arg)
656ecf6635Schristos {
666ecf6635Schristos 	struct timeval tv;
676ecf6635Schristos 	int i, j;
686ecf6635Schristos 
696ecf6635Schristos 	called++;
706ecf6635Schristos 
716ecf6635Schristos 	if (called < 10*NEVENT) {
726ecf6635Schristos 		for (i = 0; i < 10; i++) {
736ecf6635Schristos 			j = rand_int(NEVENT);
746ecf6635Schristos 			tv.tv_sec = 0;
756ecf6635Schristos 			tv.tv_usec = rand_int(50000);
76805a1ce9Schristos 			if (tv.tv_usec % 2 || called < NEVENT)
776ecf6635Schristos 				evtimer_add(ev[j], &tv);
786ecf6635Schristos 			else
796ecf6635Schristos 				evtimer_del(ev[j]);
806ecf6635Schristos 		}
816ecf6635Schristos 	}
826ecf6635Schristos }
836ecf6635Schristos 
846ecf6635Schristos int
main(int argc,char ** argv)856ecf6635Schristos main(int argc, char **argv)
866ecf6635Schristos {
87*657871a7Schristos 	struct event_base *base;
886ecf6635Schristos 	struct timeval tv;
896ecf6635Schristos 	int i;
90*657871a7Schristos 
91805a1ce9Schristos #ifdef _WIN32
926ecf6635Schristos 	WORD wVersionRequested;
936ecf6635Schristos 	WSADATA wsaData;
946ecf6635Schristos 
956ecf6635Schristos 	wVersionRequested = MAKEWORD(2, 2);
966ecf6635Schristos 
976ecf6635Schristos 	(void) WSAStartup(wVersionRequested, &wsaData);
986ecf6635Schristos #endif
996ecf6635Schristos 
100805a1ce9Schristos 	evutil_weakrand_seed_(&weakrand_state, 0);
101805a1ce9Schristos 
102*657871a7Schristos 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
103*657871a7Schristos 		event_enable_debug_logging(EVENT_DBG_ALL);
104*657871a7Schristos 	}
105*657871a7Schristos 
106*657871a7Schristos 	base = event_base_new();
1076ecf6635Schristos 
1086ecf6635Schristos 	for (i = 0; i < NEVENT; i++) {
109*657871a7Schristos 		ev[i] = evtimer_new(base, time_cb, event_self_cbarg());
1106ecf6635Schristos 		tv.tv_sec = 0;
1116ecf6635Schristos 		tv.tv_usec = rand_int(50000);
1126ecf6635Schristos 		evtimer_add(ev[i], &tv);
1136ecf6635Schristos 	}
1146ecf6635Schristos 
115*657871a7Schristos 	i = event_base_dispatch(base);
1166ecf6635Schristos 
117*657871a7Schristos 	printf("event_base_dispatch=%d, called=%d, EVENT=%d\n",
118*657871a7Schristos 		i, called, NEVENT);
119805a1ce9Schristos 
120*657871a7Schristos 	if (i == 1 && called >= NEVENT) {
121*657871a7Schristos 		return EXIT_SUCCESS;
122*657871a7Schristos 	} else {
123*657871a7Schristos 		return EXIT_FAILURE;
124*657871a7Schristos 	}
1256ecf6635Schristos }
1266ecf6635Schristos 
127