3 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
38 #include <sys/types.h>
40 #ifdef HAVE_SYS_TIME_H
43 #include <sys/queue.h>
45 #include <sys/socket.h>
61 #include "event-internal.h"
66 #include "regress.gen.h"
72 static char wbuf
[4096];
73 static char rbuf
[4096];
76 static int usepersist
;
77 static struct timeval tset
;
78 static struct timeval tcalled
;
79 static struct event_base
*global_base
;
81 #define TEST1 "this is a test"
89 #define write(fd,buf,len) send((fd),(buf),(len),0)
90 #define read(fd,buf,len) recv((fd),(buf),(len),0)
94 simple_read_cb(int fd
, short event
, void *arg
)
102 len
= read(fd
, buf
, sizeof(buf
));
106 if (event_add(arg
, NULL
) == -1)
109 } else if (called
== 1)
116 simple_write_cb(int fd
, short event
, void *arg
)
123 len
= write(fd
, TEST1
, strlen(TEST1
) + 1);
131 multiple_write_cb(int fd
, short event
, void *arg
)
133 struct event
*ev
= arg
;
137 if (woff
+ len
>= sizeof(wbuf
))
138 len
= sizeof(wbuf
) - woff
;
140 len
= write(fd
, wbuf
+ woff
, len
);
142 fprintf(stderr
, "%s: write\n", __func__
);
150 if (woff
>= sizeof(wbuf
)) {
151 shutdown(fd
, SHUT_WR
);
158 if (event_add(ev
, NULL
) == -1)
164 multiple_read_cb(int fd
, short event
, void *arg
)
166 struct event
*ev
= arg
;
169 len
= read(fd
, rbuf
+ roff
, sizeof(rbuf
) - roff
);
171 fprintf(stderr
, "%s: read\n", __func__
);
180 if (event_add(ev
, NULL
) == -1)
186 timeout_cb(int fd
, short event
, void *arg
)
191 evutil_gettimeofday(&tcalled
, NULL
);
192 if (evutil_timercmp(&tcalled
, &tset
, >))
193 evutil_timersub(&tcalled
, &tset
, &tv
);
195 evutil_timersub(&tset
, &tcalled
, &tv
);
197 diff
= tv
.tv_sec
*1000 + tv
.tv_usec
/1000 - SECONDS
* 1000;
207 signal_cb_sa(int sig
)
213 signal_cb(int fd
, short event
, void *arg
)
215 struct event
*ev
= arg
;
228 combined_read_cb(int fd
, short event
, void *arg
)
230 struct both
*both
= arg
;
234 len
= read(fd
, buf
, sizeof(buf
));
236 fprintf(stderr
, "%s: read\n", __func__
);
241 if (event_add(&both
->ev
, NULL
) == -1)
246 combined_write_cb(int fd
, short event
, void *arg
)
248 struct both
*both
= arg
;
253 if (len
> both
->nread
)
256 len
= write(fd
, buf
, len
);
258 fprintf(stderr
, "%s: write\n", __func__
);
260 shutdown(fd
, SHUT_WR
);
265 if (event_add(&both
->ev
, NULL
) == -1)
269 /* Test infrastructure */
272 setup_test(const char *name
)
275 fprintf(stdout
, "%s", name
);
277 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
278 fprintf(stderr
, "%s: socketpair\n", __func__
);
283 if (fcntl(pair
[0], F_SETFL
, O_NONBLOCK
) == -1)
284 fprintf(stderr
, "fcntl(O_NONBLOCK)");
286 if (fcntl(pair
[1], F_SETFL
, O_NONBLOCK
) == -1)
287 fprintf(stderr
, "fcntl(O_NONBLOCK)");
302 CloseHandle((HANDLE
)pair
[0]);
303 CloseHandle((HANDLE
)pair
[1]);
306 fprintf(stdout
, "OK\n");
308 fprintf(stdout
, "FAILED\n");
316 test_registerfds(void)
320 struct event read_evs
[512];
321 struct event write_evs
[512];
323 struct event_base
*base
= event_base_new();
325 fprintf(stdout
, "Testing register fds: ");
327 for (i
= 0; i
< 512; ++i
) {
328 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
329 /* run up to the limit of file descriptors */
332 event_set(&read_evs
[i
], pair
[0],
333 EV_READ
|EV_PERSIST
, simple_read_cb
, NULL
);
334 event_base_set(base
, &read_evs
[i
]);
335 event_add(&read_evs
[i
], NULL
);
336 event_set(&write_evs
[i
], pair
[1],
337 EV_WRITE
|EV_PERSIST
, simple_write_cb
, NULL
);
338 event_base_set(base
, &write_evs
[i
]);
339 event_add(&write_evs
[i
], NULL
);
342 event_base_loop(base
, EVLOOP_ONCE
);
345 /* now delete everything */
346 for (j
= 0; j
< i
; ++j
) {
347 event_del(&read_evs
[j
]);
348 event_del(&write_evs
[j
]);
350 close(read_evs
[j
].ev_fd
);
351 close(write_evs
[j
].ev_fd
);
353 CloseHandle((HANDLE
)read_evs
[j
].ev_fd
);
354 CloseHandle((HANDLE
)write_evs
[j
].ev_fd
);
358 event_base_loop(base
, EVLOOP_ONCE
);
361 event_base_free(base
);
363 fprintf(stdout
, "OK\n");
367 test_simpleread(void)
371 /* Very simple read test */
372 setup_test("Simple read: ");
374 write(pair
[0], TEST1
, strlen(TEST1
)+1);
375 shutdown(pair
[0], SHUT_WR
);
377 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
378 if (event_add(&ev
, NULL
) == -1)
386 test_simplewrite(void)
390 /* Very simple write test */
391 setup_test("Simple write: ");
393 event_set(&ev
, pair
[0], EV_WRITE
, simple_write_cb
, &ev
);
394 if (event_add(&ev
, NULL
) == -1)
404 struct event ev
, ev2
;
407 /* Multiple read and write test */
408 setup_test("Multiple read/write: ");
409 memset(rbuf
, 0, sizeof(rbuf
));
410 for (i
= 0; i
< sizeof(wbuf
); i
++)
416 event_set(&ev
, pair
[0], EV_WRITE
, multiple_write_cb
, &ev
);
417 if (event_add(&ev
, NULL
) == -1)
419 event_set(&ev2
, pair
[1], EV_READ
, multiple_read_cb
, &ev2
);
420 if (event_add(&ev2
, NULL
) == -1)
425 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
431 test_persistent(void)
433 struct event ev
, ev2
;
436 /* Multiple read and write test with persist */
437 setup_test("Persist read/write: ");
438 memset(rbuf
, 0, sizeof(rbuf
));
439 for (i
= 0; i
< sizeof(wbuf
); i
++)
445 event_set(&ev
, pair
[0], EV_WRITE
|EV_PERSIST
, multiple_write_cb
, &ev
);
446 if (event_add(&ev
, NULL
) == -1)
448 event_set(&ev2
, pair
[1], EV_READ
|EV_PERSIST
, multiple_read_cb
, &ev2
);
449 if (event_add(&ev2
, NULL
) == -1)
454 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
462 struct both r1
, r2
, w1
, w2
;
464 setup_test("Combined read/write: ");
465 memset(&r1
, 0, sizeof(r1
));
466 memset(&r2
, 0, sizeof(r2
));
467 memset(&w1
, 0, sizeof(w1
));
468 memset(&w2
, 0, sizeof(w2
));
473 event_set(&r1
.ev
, pair
[0], EV_READ
, combined_read_cb
, &r1
);
474 event_set(&w1
.ev
, pair
[0], EV_WRITE
, combined_write_cb
, &w1
);
475 event_set(&r2
.ev
, pair
[1], EV_READ
, combined_read_cb
, &r2
);
476 event_set(&w2
.ev
, pair
[1], EV_WRITE
, combined_write_cb
, &w2
);
477 if (event_add(&r1
.ev
, NULL
) == -1)
479 if (event_add(&w1
.ev
, NULL
))
481 if (event_add(&r2
.ev
, NULL
))
483 if (event_add(&w2
.ev
, NULL
))
488 if (r1
.nread
== 8192 && r2
.nread
== 4096)
495 test_simpletimeout(void)
500 setup_test("Simple timeout: ");
504 evtimer_set(&ev
, timeout_cb
, NULL
);
505 evtimer_add(&ev
, &tv
);
507 evutil_gettimeofday(&tset
, NULL
);
514 extern struct event_base
*current_base
;
517 child_signal_cb(int fd
, short event
, void *arg
)
532 int status
, got_sigchld
= 0;
533 struct event ev
, sig_ev
;
536 setup_test("After fork: ");
538 write(pair
[0], TEST1
, strlen(TEST1
)+1);
540 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
541 if (event_add(&ev
, NULL
) == -1)
544 signal_set(&sig_ev
, SIGCHLD
, child_signal_cb
, &got_sigchld
);
545 signal_add(&sig_ev
, NULL
);
547 if ((pid
= fork()) == 0) {
549 if (event_reinit(current_base
) == -1) {
550 fprintf(stderr
, "FAILED (reinit)\n");
560 /* we do not send an EOF; simple_read_cb requires an EOF
561 * to set test_ok. we just verify that the callback was
563 exit(test_ok
!= 0 || called
!= 2 ? -2 : 76);
566 /* wait for the child to read the data */
569 write(pair
[0], TEST1
, strlen(TEST1
)+1);
571 if (waitpid(pid
, &status
, 0) == -1) {
572 fprintf(stderr
, "FAILED (fork)\n");
576 if (WEXITSTATUS(status
) != 76) {
577 fprintf(stderr
, "FAILED (exit): %d\n", WEXITSTATUS(status
));
581 /* test that the current event loop still works */
582 write(pair
[0], TEST1
, strlen(TEST1
)+1);
583 shutdown(pair
[0], SHUT_WR
);
588 fprintf(stdout
, "FAILED (sigchld)\n");
598 test_simplesignal(void)
601 struct itimerval itv
;
603 setup_test("Simple signal: ");
604 signal_set(&ev
, SIGALRM
, signal_cb
, &ev
);
605 signal_add(&ev
, NULL
);
606 /* find bugs in which operations are re-ordered */
608 signal_add(&ev
, NULL
);
610 memset(&itv
, 0, sizeof(itv
));
611 itv
.it_value
.tv_sec
= 1;
612 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
613 goto skip_simplesignal
;
617 if (signal_del(&ev
) == -1)
624 test_multiplesignal(void)
626 struct event ev_one
, ev_two
;
627 struct itimerval itv
;
629 setup_test("Multiple signal: ");
631 signal_set(&ev_one
, SIGALRM
, signal_cb
, &ev_one
);
632 signal_add(&ev_one
, NULL
);
634 signal_set(&ev_two
, SIGALRM
, signal_cb
, &ev_two
);
635 signal_add(&ev_two
, NULL
);
637 memset(&itv
, 0, sizeof(itv
));
638 itv
.it_value
.tv_sec
= 1;
639 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
640 goto skip_simplesignal
;
645 if (signal_del(&ev_one
) == -1)
647 if (signal_del(&ev_two
) == -1)
654 test_immediatesignal(void)
659 printf("Immediate signal: ");
660 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
661 signal_add(&ev
, NULL
);
663 event_loop(EVLOOP_NONBLOCK
);
669 test_signal_dealloc(void)
671 /* make sure that signal_event is event_del'ed and pipe closed */
673 struct event_base
*base
= event_init();
674 printf("Signal dealloc: ");
675 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
676 signal_add(&ev
, NULL
);
678 event_base_free(base
);
679 /* If we got here without asserting, we're fine. */
685 test_signal_pipeloss(void)
687 /* make sure that the base1 pipe is closed correctly. */
688 struct event_base
*base1
, *base2
;
691 printf("Signal pipeloss: ");
692 base1
= event_init();
693 pipe1
= base1
->sig
.ev_signal_pair
[0];
694 base2
= event_init();
695 event_base_free(base2
);
696 event_base_free(base1
);
697 if (close(pipe1
) != -1 || errno
!=EBADF
) {
698 /* fd must be closed, so second close gives -1, EBADF */
699 printf("signal pipe not closed. ");
708 * make two bases to catch signals, use both of them. this only works
709 * for event mechanisms that use our signal pipe trick. kqueue handles
710 * signals internally, and all interested kqueues get all the signals.
713 test_signal_switchbase(void)
715 struct event ev1
, ev2
;
716 struct event_base
*base1
, *base2
;
719 printf("Signal switchbase: ");
720 base1
= event_init();
721 base2
= event_init();
722 is_kqueue
= !strcmp(event_get_method(),"kqueue");
723 signal_set(&ev1
, SIGUSR1
, signal_cb
, &ev1
);
724 signal_set(&ev2
, SIGUSR1
, signal_cb
, &ev2
);
725 if (event_base_set(base1
, &ev1
) ||
726 event_base_set(base2
, &ev2
) ||
727 event_add(&ev1
, NULL
) ||
728 event_add(&ev2
, NULL
)) {
729 fprintf(stderr
, "%s: cannot set base, add\n", __func__
);
734 /* can handle signal before loop is called */
736 event_base_loop(base2
, EVLOOP_NONBLOCK
);
742 event_base_loop(base1
, EVLOOP_NONBLOCK
);
743 if (test_ok
&& !is_kqueue
) {
746 /* set base1 to handle signals */
747 event_base_loop(base1
, EVLOOP_NONBLOCK
);
749 event_base_loop(base1
, EVLOOP_NONBLOCK
);
750 event_base_loop(base2
, EVLOOP_NONBLOCK
);
753 event_base_free(base1
);
754 event_base_free(base2
);
759 * assert that a signal event removed from the event queue really is
760 * removed - with no possibility of it's parent handler being fired.
763 test_signal_assert(void)
766 struct event_base
*base
= event_init();
768 printf("Signal handler assert: ");
769 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
770 signal_set(&ev
, SIGCONT
, signal_cb
, &ev
);
771 signal_add(&ev
, NULL
);
773 * if signal_del() fails to reset the handler, it's current handler
774 * will still point to evsignal_handler().
779 /* only way to verify we were in evsignal_handler() */
780 if (base
->sig
.evsignal_caught
)
785 event_base_free(base
);
791 * assert that we restore our previous signal handler properly.
794 test_signal_restore(void)
797 struct event_base
*base
= event_init();
798 #ifdef HAVE_SIGACTION
803 printf("Signal handler restore: ");
804 #ifdef HAVE_SIGACTION
805 sa
.sa_handler
= signal_cb_sa
;
807 sigemptyset(&sa
.sa_mask
);
808 if (sigaction(SIGUSR1
, &sa
, NULL
) == -1)
811 if (signal(SIGUSR1
, signal_cb_sa
) == SIG_ERR
)
814 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
815 signal_add(&ev
, NULL
);
819 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
823 event_base_free(base
);
829 signal_cb_swp(int sig
, short event
, void *arg
)
835 event_loopexit(NULL
);
838 timeout_cb_swp(int fd
, short event
, void *arg
)
841 struct timeval tv
= {5, 0};
844 evtimer_add((struct event
*)arg
, &tv
);
849 event_loopexit(NULL
);
853 test_signal_while_processing(void)
855 struct event_base
*base
= event_init();
856 struct event ev
, ev_timer
;
857 struct timeval tv
= {0, 0};
859 setup_test("Receiving a signal while processing other signal: ");
863 signal_set(&ev
, SIGUSR1
, signal_cb_swp
, NULL
);
864 signal_add(&ev
, NULL
);
865 evtimer_set(&ev_timer
, timeout_cb_swp
, &ev_timer
);
866 evtimer_add(&ev_timer
, &tv
);
869 event_base_free(base
);
876 test_free_active_base(void)
878 struct event_base
*base1
;
880 setup_test("Free active base: ");
881 base1
= event_init();
882 event_set(&ev1
, pair
[1], EV_READ
, simple_read_cb
, &ev1
);
883 event_base_set(base1
, &ev1
);
884 event_add(&ev1
, NULL
);
885 /* event_del(&ev1); */
886 event_base_free(base1
);
892 test_event_base_new(void)
894 struct event_base
*base
;
896 setup_test("Event base new: ");
898 write(pair
[0], TEST1
, strlen(TEST1
)+1);
899 shutdown(pair
[0], SHUT_WR
);
901 base
= event_base_new();
902 event_set(&ev1
, pair
[1], EV_READ
, simple_read_cb
, &ev1
);
903 event_base_set(base
, &ev1
);
904 event_add(&ev1
, NULL
);
906 event_base_dispatch(base
);
908 event_base_free(base
);
916 struct timeval tv
, tv_start
, tv_end
;
919 setup_test("Loop exit: ");
922 tv
.tv_sec
= 60*60*24;
923 evtimer_set(&ev
, timeout_cb
, NULL
);
924 evtimer_add(&ev
, &tv
);
930 evutil_gettimeofday(&tv_start
, NULL
);
932 evutil_gettimeofday(&tv_end
, NULL
);
933 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
944 test_loopexit_multiple(void)
947 struct event_base
*base
;
949 setup_test("Loop Multiple exit: ");
951 base
= event_base_new();
955 event_base_loopexit(base
, &tv
);
959 event_base_loopexit(base
, &tv
);
961 event_base_dispatch(base
);
963 event_base_free(base
);
971 break_cb(int fd
, short events
, void *arg
)
978 fail_cb(int fd
, short events
, void *arg
)
986 struct event ev1
, ev2
;
989 setup_test("Loop break: ");
993 evtimer_set(&ev1
, break_cb
, NULL
);
994 evtimer_add(&ev1
, &tv
);
995 evtimer_set(&ev2
, fail_cb
, NULL
);
996 evtimer_add(&ev2
, &tv
);
1007 test_evbuffer(void) {
1009 struct evbuffer
*evb
= evbuffer_new();
1010 setup_test("Testing Evbuffer: ");
1012 evbuffer_add_printf(evb
, "%s/%d", "hello", 1);
1014 if (EVBUFFER_LENGTH(evb
) == 7 &&
1015 strcmp((char*)EVBUFFER_DATA(evb
), "hello/1") == 0)
1024 test_evbuffer_find(void)
1027 const char* test1
= "1234567890\r\n";
1028 const char* test2
= "1234567890\r";
1029 #define EVBUFFER_INITIAL_LENGTH 256
1030 char test3
[EVBUFFER_INITIAL_LENGTH
];
1032 struct evbuffer
* buf
= evbuffer_new();
1034 /* make sure evbuffer_find doesn't match past the end of the buffer */
1035 fprintf(stdout
, "Testing evbuffer_find 1: ");
1036 evbuffer_add(buf
, (u_char
*)test1
, strlen(test1
));
1037 evbuffer_drain(buf
, strlen(test1
));
1038 evbuffer_add(buf
, (u_char
*)test2
, strlen(test2
));
1039 p
= evbuffer_find(buf
, (u_char
*)"\r\n", 2);
1041 fprintf(stdout
, "OK\n");
1043 fprintf(stdout
, "FAILED\n");
1048 * drain the buffer and do another find; in r309 this would
1049 * read past the allocated buffer causing a valgrind error.
1051 fprintf(stdout
, "Testing evbuffer_find 2: ");
1052 evbuffer_drain(buf
, strlen(test2
));
1053 for (i
= 0; i
< EVBUFFER_INITIAL_LENGTH
; ++i
)
1055 test3
[EVBUFFER_INITIAL_LENGTH
- 1] = 'x';
1056 evbuffer_add(buf
, (u_char
*)test3
, EVBUFFER_INITIAL_LENGTH
);
1057 p
= evbuffer_find(buf
, (u_char
*)"xy", 2);
1061 fprintf(stdout
, "FAILED\n");
1065 /* simple test for match at end of allocated buffer */
1066 fprintf(stdout
, "Testing evbuffer_find 3: ");
1067 p
= evbuffer_find(buf
, (u_char
*)"ax", 2);
1068 if (p
!= NULL
&& strncmp((char*)p
, "ax", 2) == 0) {
1071 fprintf(stdout
, "FAILED\n");
1079 * simple bufferevent test
1083 readcb(struct bufferevent
*bev
, void *arg
)
1085 if (EVBUFFER_LENGTH(bev
->input
) == 8333) {
1086 bufferevent_disable(bev
, EV_READ
);
1092 writecb(struct bufferevent
*bev
, void *arg
)
1094 if (EVBUFFER_LENGTH(bev
->output
) == 0)
1099 errorcb(struct bufferevent
*bev
, short what
, void *arg
)
1105 test_bufferevent(void)
1107 struct bufferevent
*bev1
, *bev2
;
1111 setup_test("Bufferevent: ");
1113 bev1
= bufferevent_new(pair
[0], readcb
, writecb
, errorcb
, NULL
);
1114 bev2
= bufferevent_new(pair
[1], readcb
, writecb
, errorcb
, NULL
);
1116 bufferevent_disable(bev1
, EV_READ
);
1117 bufferevent_enable(bev2
, EV_READ
);
1119 for (i
= 0; i
< sizeof(buffer
); i
++)
1122 bufferevent_write(bev1
, buffer
, sizeof(buffer
));
1126 bufferevent_free(bev1
);
1127 bufferevent_free(bev2
);
1136 * test watermarks and bufferevent
1140 wm_readcb(struct bufferevent
*bev
, void *arg
)
1142 int len
= EVBUFFER_LENGTH(bev
->input
);
1145 assert(len
>= 10 && len
<= 20);
1147 evbuffer_drain(bev
->input
, len
);
1150 if (nread
== 65000) {
1151 bufferevent_disable(bev
, EV_READ
);
1157 wm_writecb(struct bufferevent
*bev
, void *arg
)
1159 if (EVBUFFER_LENGTH(bev
->output
) == 0)
1164 wm_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
1170 test_bufferevent_watermarks(void)
1172 struct bufferevent
*bev1
, *bev2
;
1176 setup_test("Bufferevent Watermarks: ");
1178 bev1
= bufferevent_new(pair
[0], NULL
, wm_writecb
, wm_errorcb
, NULL
);
1179 bev2
= bufferevent_new(pair
[1], wm_readcb
, NULL
, wm_errorcb
, NULL
);
1181 bufferevent_disable(bev1
, EV_READ
);
1182 bufferevent_enable(bev2
, EV_READ
);
1184 for (i
= 0; i
< sizeof(buffer
); i
++)
1187 bufferevent_write(bev1
, buffer
, sizeof(buffer
));
1189 /* limit the reading on the receiving bufferevent */
1190 bufferevent_setwatermark(bev2
, EV_READ
, 10, 20);
1194 bufferevent_free(bev1
);
1195 bufferevent_free(bev2
);
1203 struct test_pri_event
{
1209 test_priorities_cb(int fd
, short what
, void *arg
)
1211 struct test_pri_event
*pri
= arg
;
1214 if (pri
->count
== 3) {
1215 event_loopexit(NULL
);
1221 evutil_timerclear(&tv
);
1222 event_add(&pri
->ev
, &tv
);
1226 test_priorities(int npriorities
)
1229 struct test_pri_event one
, two
;
1232 evutil_snprintf(buf
, sizeof(buf
), "Testing Priorities %d: ", npriorities
);
1235 event_base_priority_init(global_base
, npriorities
);
1237 memset(&one
, 0, sizeof(one
));
1238 memset(&two
, 0, sizeof(two
));
1240 timeout_set(&one
.ev
, test_priorities_cb
, &one
);
1241 if (event_priority_set(&one
.ev
, 0) == -1) {
1242 fprintf(stderr
, "%s: failed to set priority", __func__
);
1246 timeout_set(&two
.ev
, test_priorities_cb
, &two
);
1247 if (event_priority_set(&two
.ev
, npriorities
- 1) == -1) {
1248 fprintf(stderr
, "%s: failed to set priority", __func__
);
1252 evutil_timerclear(&tv
);
1254 if (event_add(&one
.ev
, &tv
) == -1)
1256 if (event_add(&two
.ev
, &tv
) == -1)
1264 if (npriorities
== 1) {
1265 if (one
.count
== 3 && two
.count
== 3)
1267 } else if (npriorities
== 2) {
1268 /* Two is called once because event_loopexit is priority 1 */
1269 if (one
.count
== 3 && two
.count
== 1)
1272 if (one
.count
== 3 && two
.count
== 0)
1280 test_multiple_cb(int fd
, short event
, void *arg
)
1282 if (event
& EV_READ
)
1284 else if (event
& EV_WRITE
)
1289 test_multiple_events_for_same_fd(void)
1291 struct event e1
, e2
;
1293 setup_test("Multiple events for same fd: ");
1295 event_set(&e1
, pair
[0], EV_READ
, test_multiple_cb
, NULL
);
1296 event_add(&e1
, NULL
);
1297 event_set(&e2
, pair
[0], EV_WRITE
, test_multiple_cb
, NULL
);
1298 event_add(&e2
, NULL
);
1299 event_loop(EVLOOP_ONCE
);
1301 write(pair
[1], TEST1
, strlen(TEST1
)+1);
1302 event_loop(EVLOOP_ONCE
);
1311 int evtag_decode_int(uint32_t *pnumber
, struct evbuffer
*evbuf
);
1312 int evtag_encode_tag(struct evbuffer
*evbuf
, uint32_t number
);
1313 int evtag_decode_tag(uint32_t *pnumber
, struct evbuffer
*evbuf
);
1316 read_once_cb(int fd
, short event
, void *arg
)
1321 len
= read(fd
, buf
, sizeof(buf
));
1326 /* Assumes global pair[0] can be used for writing */
1327 write(pair
[0], TEST1
, strlen(TEST1
)+1);
1335 test_want_only_once(void)
1340 /* Very simple read test */
1341 setup_test("Want read only once: ");
1343 write(pair
[0], TEST1
, strlen(TEST1
)+1);
1345 /* Setup the loop termination */
1346 evutil_timerclear(&tv
);
1348 event_loopexit(&tv
);
1350 event_set(&ev
, pair
[1], EV_READ
, read_once_cb
, &ev
);
1351 if (event_add(&ev
, NULL
) == -1)
1358 #define TEST_MAX_INT 6
1361 evtag_int_test(void)
1363 struct evbuffer
*tmp
= evbuffer_new();
1364 uint32_t integers
[TEST_MAX_INT
] = {
1365 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1370 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1372 oldlen
= EVBUFFER_LENGTH(tmp
);
1373 encode_int(tmp
, integers
[i
]);
1374 newlen
= EVBUFFER_LENGTH(tmp
);
1375 fprintf(stdout
, "\t\tencoded 0x%08x with %d bytes\n",
1376 integers
[i
], newlen
- oldlen
);
1379 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1380 if (evtag_decode_int(&integer
, tmp
) == -1) {
1381 fprintf(stderr
, "decode %d failed", i
);
1384 if (integer
!= integers
[i
]) {
1385 fprintf(stderr
, "got %x, wanted %x",
1386 integer
, integers
[i
]);
1391 if (EVBUFFER_LENGTH(tmp
) != 0) {
1392 fprintf(stderr
, "trailing data");
1397 fprintf(stdout
, "\t%s: OK\n", __func__
);
1403 u_char buffer
[4096];
1404 struct evbuffer
*tmp
= evbuffer_new();
1409 for (j
= 0; j
< 100; j
++) {
1410 for (i
= 0; i
< sizeof(buffer
); i
++)
1412 evbuffer_drain(tmp
, -1);
1413 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1415 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1)
1419 /* The majority of decodes should fail */
1420 if (not_failed
>= 10) {
1421 fprintf(stderr
, "evtag_unmarshal should have failed");
1425 /* Now insert some corruption into the tag length field */
1426 evbuffer_drain(tmp
, -1);
1427 evutil_timerclear(&tv
);
1429 evtag_marshal_timeval(tmp
, 0, &tv
);
1430 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1432 EVBUFFER_DATA(tmp
)[1] = 0xff;
1433 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1) {
1434 fprintf(stderr
, "evtag_unmarshal_timeval should have failed");
1440 fprintf(stdout
, "\t%s: OK\n", __func__
);
1444 evtag_tag_encoding(void)
1446 struct evbuffer
*tmp
= evbuffer_new();
1447 uint32_t integers
[TEST_MAX_INT
] = {
1448 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1453 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1455 oldlen
= EVBUFFER_LENGTH(tmp
);
1456 evtag_encode_tag(tmp
, integers
[i
]);
1457 newlen
= EVBUFFER_LENGTH(tmp
);
1458 fprintf(stdout
, "\t\tencoded 0x%08x with %d bytes\n",
1459 integers
[i
], newlen
- oldlen
);
1462 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1463 if (evtag_decode_tag(&integer
, tmp
) == -1) {
1464 fprintf(stderr
, "decode %d failed", i
);
1467 if (integer
!= integers
[i
]) {
1468 fprintf(stderr
, "got %x, wanted %x",
1469 integer
, integers
[i
]);
1474 if (EVBUFFER_LENGTH(tmp
) != 0) {
1475 fprintf(stderr
, "trailing data");
1480 fprintf(stdout
, "\t%s: OK\n", __func__
);
1486 fprintf(stdout
, "Testing Tagging:\n");
1492 evtag_tag_encoding();
1494 fprintf(stdout
, "OK\n");
1501 struct msg
*msg
, *msg2
;
1502 struct kill
*attack
;
1504 struct evbuffer
*tmp
= evbuffer_new();
1505 struct timeval tv_start
, tv_end
;
1509 fprintf(stdout
, "Testing RPC: ");
1512 EVTAG_ASSIGN(msg
, from_name
, "niels");
1513 EVTAG_ASSIGN(msg
, to_name
, "phoenix");
1515 if (EVTAG_GET(msg
, attack
, &attack
) == -1) {
1516 fprintf(stderr
, "Failed to set kill message.\n");
1520 EVTAG_ASSIGN(attack
, weapon
, "feather");
1521 EVTAG_ASSIGN(attack
, action
, "tickle");
1523 evutil_gettimeofday(&tv_start
, NULL
);
1524 for (i
= 0; i
< 1000; ++i
) {
1525 run
= EVTAG_ADD(msg
, run
);
1527 fprintf(stderr
, "Failed to add run message.\n");
1530 EVTAG_ASSIGN(run
, how
, "very fast but with some data in it");
1531 EVTAG_ASSIGN(run
, fixed_bytes
,
1532 (unsigned char*)"012345678901234567890123");
1535 if (msg_complete(msg
) == -1) {
1536 fprintf(stderr
, "Failed to make complete message.\n");
1540 evtag_marshal_msg(tmp
, 0xdeaf, msg
);
1542 if (evtag_peek(tmp
, &tag
) == -1) {
1543 fprintf(stderr
, "Failed to peak tag.\n");
1547 if (tag
!= 0xdeaf) {
1548 fprintf(stderr
, "Got incorrect tag: %0x.\n", tag
);
1553 if (evtag_unmarshal_msg(tmp
, 0xdeaf, msg2
) == -1) {
1554 fprintf(stderr
, "Failed to unmarshal message.\n");
1558 evutil_gettimeofday(&tv_end
, NULL
);
1559 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
1560 fprintf(stderr
, "(%.1f us/add) ",
1561 (float)tv_end
.tv_sec
/(float)i
* 1000000.0 +
1562 tv_end
.tv_usec
/ (float)i
);
1564 if (!EVTAG_HAS(msg2
, from_name
) ||
1565 !EVTAG_HAS(msg2
, to_name
) ||
1566 !EVTAG_HAS(msg2
, attack
)) {
1567 fprintf(stderr
, "Missing data structures.\n");
1571 if (EVTAG_LEN(msg2
, run
) != i
) {
1572 fprintf(stderr
, "Wrong number of run messages.\n");
1581 fprintf(stdout
, "OK\n");
1586 test_evutil_strtoll(void)
1590 setup_test("evutil_stroll: ");
1593 if (evutil_strtoll("5000000000", NULL
, 10) != ((ev_int64_t
)5000000)*1000)
1595 if (evutil_strtoll("-5000000000", NULL
, 10) != ((ev_int64_t
)5000000)*-1000)
1598 if (evutil_strtoll(s
, &endptr
, 10) != (ev_int64_t
)99999)
1602 if (evutil_strtoll("foo", NULL
, 10) != 0)
1612 main (int argc
, char **argv
)
1615 WORD wVersionRequested
;
1619 wVersionRequested
= MAKEWORD( 2, 2 );
1621 err
= WSAStartup( wVersionRequested
, &wsaData
);
1625 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1628 setvbuf(stdout
, NULL
, _IONBF
, 0);
1630 /* Initalize the event library */
1631 global_base
= event_init();
1635 test_evutil_strtoll();
1637 /* use the global event base and need to be called first */
1643 test_evbuffer_find();
1646 test_bufferevent_watermarks();
1648 test_free_active_base();
1650 test_event_base_new();
1674 test_simpletimeout();
1676 test_simplesignal();
1677 test_multiplesignal();
1678 test_immediatesignal();
1683 test_loopexit_multiple();
1685 test_multiple_events_for_same_fd();
1687 test_want_only_once();
1694 test_signal_dealloc();
1695 test_signal_pipeloss();
1696 test_signal_switchbase();
1697 test_signal_restore();
1698 test_signal_assert();
1699 test_signal_while_processing();