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