diff options
author | Brad Smith <brad@cvs.openbsd.org> | 2004-11-25 16:48:48 +0000 |
---|---|---|
committer | Brad Smith <brad@cvs.openbsd.org> | 2004-11-25 16:48:48 +0000 |
commit | a757e9989d967347b29a7cbb4b2a73f26c276221 (patch) | |
tree | 945858070b7ad17e40601d01d49c1056213be6e9 /regress | |
parent | efa7f80a8e26aa5abb117e8d1a417472e5287c7d (diff) |
add regress test support for bufferevents
From NetBSD
ok markus@
Diffstat (limited to 'regress')
-rw-r--r-- | regress/lib/libevent/eventtest.c | 292 |
1 files changed, 196 insertions, 96 deletions
diff --git a/regress/lib/libevent/eventtest.c b/regress/lib/libevent/eventtest.c index 89765f96241..93dbbfd3a58 100644 --- a/regress/lib/libevent/eventtest.c +++ b/regress/lib/libevent/eventtest.c @@ -1,8 +1,8 @@ -/* $OpenBSD: eventtest.c,v 1.3 2004/01/05 19:23:04 markus Exp $ */ -/* $NetBSD: eventtest.c,v 1.2 2003/06/13 04:09:18 itojun Exp $ */ +/* $OpenBSD: eventtest.c,v 1.4 2004/11/25 16:48:47 brad Exp $ */ +/* $NetBSD: eventtest.c,v 1.3 2004/08/07 21:09:47 provos Exp $ */ /* - * Copyright 2003 Niels Provos <provos@citi.umich.edu> + * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,10 +31,6 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> @@ -63,7 +59,7 @@ static struct timeval tcalled; #define TEST1 "this is a test" #define SECONDS 1 -static void +void simple_read_cb(int fd, short event, void *arg) { char buf[256]; @@ -72,15 +68,17 @@ simple_read_cb(int fd, short event, void *arg) len = read(fd, buf, sizeof(buf)); if (len) { - if (!called) - event_add(arg, NULL); + if (!called) { + if (event_add(arg, NULL) == -1) + exit(1); + } } else if (called == 1) test_ok = 1; called++; } -static void +void simple_write_cb(int fd, short event, void *arg) { int len; @@ -92,7 +90,7 @@ simple_write_cb(int fd, short event, void *arg) test_ok = 1; } -static void +void multiple_write_cb(int fd, short event, void *arg) { struct event *ev = arg; @@ -119,11 +117,13 @@ multiple_write_cb(int fd, short event, void *arg) return; } - if (!usepersist) - event_add(ev, NULL); + if (!usepersist) { + if (event_add(ev, NULL) == -1) + exit(1); + } } -static void +void multiple_read_cb(int fd, short event, void *arg) { struct event *ev = arg; @@ -139,11 +139,13 @@ multiple_read_cb(int fd, short event, void *arg) } roff += len; - if (!usepersist) - event_add(ev, NULL); + if (!usepersist) { + if (event_add(ev, NULL) == -1) + exit(1); + } } -static void +void timeout_cb(int fd, short event, void *arg) { struct timeval tv; @@ -163,7 +165,7 @@ timeout_cb(int fd, short event, void *arg) test_ok = 1; } -static void +void signal_cb(int fd, short event, void *arg) { struct event *ev = arg; @@ -173,39 +175,11 @@ signal_cb(int fd, short event, void *arg) } struct both { - struct event ev, ev1; + struct event ev; int nread; }; -static void -eof_read_cb(int fd, short event, void *arg) -{ - struct both *both = arg; - char buf[256]; - int ret; - - ret = read(fd, buf, sizeof(buf)); - test_ok = both->nread == 0 && ret == 0; - - if (both->nread > 0) - event_add(&both->ev, NULL); - else - event_del(&both->ev1); - - both->nread -= ret; -} - -static void -eof_timeout_cb(int fd, short event, void *arg) -{ - struct both *both = arg; - - event_del(&both->ev); - - test_ok = 0; -} - -static void +void combined_read_cb(int fd, short event, void *arg) { struct both *both = arg; @@ -219,10 +193,11 @@ combined_read_cb(int fd, short event, void *arg) return; both->nread += len; - event_add(&both->ev, NULL); + if (event_add(&both->ev, NULL) == -1) + exit(1); } -static void +void combined_write_cb(int fd, short event, void *arg) { struct both *both = arg; @@ -242,12 +217,13 @@ combined_write_cb(int fd, short event, void *arg) } both->nread -= len; - event_add(&both->ev, NULL); + if (event_add(&both->ev, NULL) == -1) + exit(1); } /* Test infrastructure */ -static int +int setup_test(char *name) { @@ -258,12 +234,18 @@ setup_test(char *name) exit(1); } + if (fcntl(pair[0], F_SETFL, O_NONBLOCK) == -1) + warn("fcntl(O_NONBLOCK)"); + + if (fcntl(pair[1], F_SETFL, O_NONBLOCK) == -1) + warn("fcntl(O_NONBLOCK)"); + test_ok = 0; called = 0; return (0); } -static int +int cleanup_test(void) { close(pair[0]); @@ -279,20 +261,10 @@ cleanup_test(void) return (0); } -int -main(int argc, char **argv) +void +test1(void) { - struct event ev, ev2; - struct timeval tv; - struct itimerval itv; - struct both r1, r2, w1, w2; - int i, fd, n, tmp; - char template[] = "/tmp/eventXXXX"; - - setvbuf(stdout, NULL, _IONBF, 0); - - /* Initalize the event library */ - event_init(); + struct event ev; /* Very simple read test */ setup_test("Simple read: "); @@ -301,19 +273,34 @@ main(int argc, char **argv) shutdown(pair[0], SHUT_WR); event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev); - event_add(&ev, NULL); + if (event_add(&ev, NULL) == -1) + exit(1); event_dispatch(); cleanup_test(); +} + +void +test2(void) +{ + struct event ev; /* Very simple write test */ setup_test("Simple write: "); event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev); - event_add(&ev, NULL); + if (event_add(&ev, NULL) == -1) + exit(1); event_dispatch(); cleanup_test(); +} + +void +test3(void) +{ + struct event ev, ev2; + int i; /* Multiple read and write test */ setup_test("Multiple read/write: "); @@ -325,15 +312,24 @@ main(int argc, char **argv) usepersist = 0; event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev); - event_add(&ev, NULL); + if (event_add(&ev, NULL) == -1) + exit(1); event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2); - event_add(&ev2, NULL); + if (event_add(&ev2, NULL) == -1) + exit(1); event_dispatch(); if (roff == woff) test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0; cleanup_test(); +} + +void +test4(void) +{ + struct event ev, ev2; + int i; /* Multiple read and write test with persist */ setup_test("Persist read/write: "); @@ -345,15 +341,23 @@ main(int argc, char **argv) usepersist = 1; event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev); - event_add(&ev, NULL); + if (event_add(&ev, NULL) == -1) + exit(1); event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2); - event_add(&ev2, NULL); + if (event_add(&ev2, NULL) == -1) + exit(1); event_dispatch(); if (roff == woff) test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0; cleanup_test(); +} + +void +test5(void) +{ + struct both r1, r2, w1, w2; setup_test("Combined read/write: "); memset(&r1, 0, sizeof(r1)); @@ -368,10 +372,14 @@ main(int argc, char **argv) event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1); event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2); event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2); - event_add(&r1.ev, NULL); - event_add(&w1.ev, NULL); - event_add(&r2.ev, NULL); - event_add(&w2.ev, NULL); + if (event_add(&r1.ev, NULL) == -1) + exit(1); + if (event_add(&w1.ev, NULL)) + exit(1); + if (event_add(&r2.ev, NULL)) + exit(1); + if (event_add(&w2.ev, NULL)) + exit(1); event_dispatch(); @@ -379,7 +387,13 @@ main(int argc, char **argv) test_ok = 1; cleanup_test(); - +} + +void +test6(void) +{ + struct timeval tv; + struct event ev; setup_test("Simple timeout: "); @@ -392,6 +406,13 @@ main(int argc, char **argv) event_dispatch(); cleanup_test(); +} + +void +test7(void) +{ + struct event ev; + struct itimerval itv; setup_test("Simple signal: "); signal_set(&ev, SIGALRM, signal_cb, &ev); @@ -407,38 +428,117 @@ main(int argc, char **argv) signal_del(&ev); cleanup_test(); +} - setup_test("EOF Behavior: "); +void +test8(void) +{ + struct timeval tv, tv_start, tv_end; + struct event ev; - if ((fd = mkstemp(template)) == -1) - return (1); + setup_test("Loop exit: "); - unlink(template); + tv.tv_usec = 0; + tv.tv_sec = 60*60*24; + evtimer_set(&ev, timeout_cb, NULL); + evtimer_add(&ev, &tv); - n = strlen(TEST1) + 1; - r1.nread = n; + tv.tv_usec = 0; + tv.tv_sec = 1; + event_loopexit(&tv); + + gettimeofday(&tv_start, NULL); + event_dispatch(); + gettimeofday(&tv_end, NULL); + timersub(&tv_end, &tv_start, &tv_end); + + evtimer_del(&ev); - while (n > 0) { - if ((tmp = write(fd, TEST1, n)) == -1) - return (1); + if (tv.tv_sec < 2) + test_ok = 1; + + cleanup_test(); +} - n -= tmp; +void +readcb(struct bufferevent *bev, void *arg) +{ + if (EVBUFFER_LENGTH(bev->input) == 8333) { + bufferevent_disable(bev, EV_READ); + test_ok++; } +} + +void +writecb(struct bufferevent *bev, void *arg) +{ + if (EVBUFFER_LENGTH(bev->output) == 0) + test_ok++; +} + +void +errorcb(struct bufferevent *bev, short what, void *arg) +{ + test_ok = -2; +} - if (lseek(fd, 0, SEEK_SET) == -1) - return (1); +void +test9(void) +{ + struct bufferevent *bev1, *bev2; + char buffer[8333]; + int i; - event_set(&r1.ev, fd, EV_READ, eof_read_cb, &r1); - event_add(&r1.ev, NULL); + setup_test("Bufferevent: "); - tv.tv_usec = 0; - tv.tv_sec = SECONDS; - evtimer_set(&r1.ev1, eof_timeout_cb, &r1); - evtimer_add(&r1.ev1, &tv); + bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL); + bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL); + + bufferevent_disable(bev1, EV_READ); + bufferevent_enable(bev2, EV_READ); + + for (i = 0; i < sizeof(buffer); i++) + buffer[0] = i; + + bufferevent_write(bev1, buffer, sizeof(buffer)); event_dispatch(); + bufferevent_free(bev1); + bufferevent_free(bev2); + + if (test_ok != 2) + test_ok = 0; + cleanup_test(); +} + +int +main (int argc, char **argv) +{ + setvbuf(stdout, NULL, _IONBF, 0); + + /* Initalize the event library */ + event_init(); + + test1(); + + test2(); + + test3(); + + test4(); + + test5(); + + test6(); + + test7(); + + test8(); + + test9(); return (0); } + |