1 /* $NetBSD: test-time.c,v 1.1.1.2 2017/01/31 21:14:53 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.2 2017/01/31 21:14:53 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 49 int called = 0; 50 51 #define NEVENT 20000 52 53 struct event *ev[NEVENT]; 54 55 struct evutil_weakrand_state weakrand_state; 56 57 static int 58 rand_int(int n) 59 { 60 return evutil_weakrand_(&weakrand_state) % n; 61 } 62 63 static void 64 time_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 84 int 85 main(int argc, char **argv) 86 { 87 struct timeval tv; 88 int i; 89 #ifdef _WIN32 90 WORD wVersionRequested; 91 WSADATA wsaData; 92 93 wVersionRequested = MAKEWORD(2, 2); 94 95 (void) WSAStartup(wVersionRequested, &wsaData); 96 #endif 97 98 evutil_weakrand_seed_(&weakrand_state, 0); 99 100 /* Initalize the event library */ 101 event_init(); 102 103 for (i = 0; i < NEVENT; i++) { 104 ev[i] = malloc(sizeof(struct event)); 105 106 /* Initalize one event */ 107 evtimer_set(ev[i], time_cb, ev[i]); 108 tv.tv_sec = 0; 109 tv.tv_usec = rand_int(50000); 110 evtimer_add(ev[i], &tv); 111 } 112 113 event_dispatch(); 114 115 116 printf("%d, %d\n", called, NEVENT); 117 return (called < NEVENT); 118 } 119 120