1*eabc0478Schristos /* $NetBSD: test-time.c,v 1.8 2024/08/18 20:47:23 christos Exp $ */ 28585484eSchristos 38585484eSchristos /* 48585484eSchristos * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu> 58585484eSchristos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 68585484eSchristos * 78585484eSchristos * Redistribution and use in source and binary forms, with or without 88585484eSchristos * modification, are permitted provided that the following conditions 98585484eSchristos * are met: 108585484eSchristos * 1. Redistributions of source code must retain the above copyright 118585484eSchristos * notice, this list of conditions and the following disclaimer. 128585484eSchristos * 2. Redistributions in binary form must reproduce the above copyright 138585484eSchristos * notice, this list of conditions and the following disclaimer in the 148585484eSchristos * documentation and/or other materials provided with the distribution. 158585484eSchristos * 3. The name of the author may not be used to endorse or promote products 168585484eSchristos * derived from this software without specific prior written permission. 178585484eSchristos * 188585484eSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198585484eSchristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208585484eSchristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218585484eSchristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228585484eSchristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238585484eSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248585484eSchristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258585484eSchristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268585484eSchristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278585484eSchristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288585484eSchristos */ 298585484eSchristos #include "event2/event-config.h" 30*eabc0478Schristos #include "util-internal.h" 318585484eSchristos 328585484eSchristos #include <sys/types.h> 338585484eSchristos #include <sys/stat.h> 348585484eSchristos #include <fcntl.h> 358585484eSchristos #include <stdlib.h> 368585484eSchristos #include <stdio.h> 378585484eSchristos #include <string.h> 388585484eSchristos #ifndef _WIN32 398585484eSchristos #include <unistd.h> 408585484eSchristos #include <sys/time.h> 418585484eSchristos #endif 428585484eSchristos #include <errno.h> 438585484eSchristos 448585484eSchristos #include "event2/event.h" 458585484eSchristos #include "event2/event_compat.h" 468585484eSchristos #include "event2/event_struct.h" 478585484eSchristos 488585484eSchristos int called = 0; 498585484eSchristos 508585484eSchristos #define NEVENT 20000 518585484eSchristos 528585484eSchristos struct event *ev[NEVENT]; 538585484eSchristos 547476e6e4Schristos struct evutil_weakrand_state weakrand_state; 557476e6e4Schristos 568585484eSchristos static int 578585484eSchristos rand_int(int n) 588585484eSchristos { 597476e6e4Schristos return evutil_weakrand_(&weakrand_state) % n; 608585484eSchristos } 618585484eSchristos 628585484eSchristos static void 638585484eSchristos time_cb(evutil_socket_t fd, short event, void *arg) 648585484eSchristos { 658585484eSchristos struct timeval tv; 668585484eSchristos int i, j; 678585484eSchristos 688585484eSchristos called++; 698585484eSchristos 708585484eSchristos if (called < 10*NEVENT) { 718585484eSchristos for (i = 0; i < 10; i++) { 728585484eSchristos j = rand_int(NEVENT); 738585484eSchristos tv.tv_sec = 0; 748585484eSchristos tv.tv_usec = rand_int(50000); 757476e6e4Schristos if (tv.tv_usec % 2 || called < NEVENT) 768585484eSchristos evtimer_add(ev[j], &tv); 778585484eSchristos else 788585484eSchristos evtimer_del(ev[j]); 798585484eSchristos } 808585484eSchristos } 818585484eSchristos } 828585484eSchristos 838585484eSchristos int 848585484eSchristos main(int argc, char **argv) 858585484eSchristos { 86*eabc0478Schristos struct event_base *base; 878585484eSchristos struct timeval tv; 888585484eSchristos int i; 89*eabc0478Schristos 908585484eSchristos #ifdef _WIN32 918585484eSchristos WORD wVersionRequested; 928585484eSchristos WSADATA wsaData; 938585484eSchristos 948585484eSchristos wVersionRequested = MAKEWORD(2, 2); 958585484eSchristos 968585484eSchristos (void) WSAStartup(wVersionRequested, &wsaData); 978585484eSchristos #endif 988585484eSchristos 997476e6e4Schristos evutil_weakrand_seed_(&weakrand_state, 0); 1007476e6e4Schristos 101*eabc0478Schristos if (getenv("EVENT_DEBUG_LOGGING_ALL")) { 102*eabc0478Schristos event_enable_debug_logging(EVENT_DBG_ALL); 103*eabc0478Schristos } 104*eabc0478Schristos 105*eabc0478Schristos base = event_base_new(); 1068585484eSchristos 1078585484eSchristos for (i = 0; i < NEVENT; i++) { 108*eabc0478Schristos ev[i] = evtimer_new(base, time_cb, event_self_cbarg()); 1098585484eSchristos tv.tv_sec = 0; 1108585484eSchristos tv.tv_usec = rand_int(50000); 1118585484eSchristos evtimer_add(ev[i], &tv); 1128585484eSchristos } 1138585484eSchristos 114*eabc0478Schristos i = event_base_dispatch(base); 1158585484eSchristos 116*eabc0478Schristos printf("event_base_dispatch=%d, called=%d, EVENT=%d\n", 117*eabc0478Schristos i, called, NEVENT); 1187476e6e4Schristos 119*eabc0478Schristos if (i == 1 && called >= NEVENT) { 120*eabc0478Schristos return EXIT_SUCCESS; 121*eabc0478Schristos } else { 122*eabc0478Schristos return EXIT_FAILURE; 123*eabc0478Schristos } 1248585484eSchristos } 1258585484eSchristos 126