2 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_TIME_H
42 #include <sys/queue.h>
44 #include <sys/socket.h>
60 #include "event-internal.h"
65 #include "regress.gen.h"
71 static char wbuf
[4096];
72 static char rbuf
[4096];
75 static int usepersist
;
76 static struct timeval tset
;
77 static struct timeval tcalled
;
78 static struct event_base
*global_base
;
80 #define TEST1 "this is a test"
88 #define write(fd,buf,len) send((fd),(buf),(len),0)
89 #define read(fd,buf,len) recv((fd),(buf),(len),0)
93 simple_read_cb(int fd
, short event
, void *arg
)
101 len
= read(fd
, buf
, sizeof(buf
));
105 if (event_add(arg
, NULL
) == -1)
108 } else if (called
== 1)
115 simple_write_cb(int fd
, short event
, void *arg
)
122 len
= write(fd
, TEST1
, strlen(TEST1
) + 1);
130 multiple_write_cb(int fd
, short event
, void *arg
)
132 struct event
*ev
= arg
;
136 if (woff
+ len
>= sizeof(wbuf
))
137 len
= sizeof(wbuf
) - woff
;
139 len
= write(fd
, wbuf
+ woff
, len
);
141 fprintf(stderr
, "%s: write\n", __func__
);
149 if (woff
>= sizeof(wbuf
)) {
150 shutdown(fd
, SHUT_WR
);
157 if (event_add(ev
, NULL
) == -1)
163 multiple_read_cb(int fd
, short event
, void *arg
)
165 struct event
*ev
= arg
;
168 len
= read(fd
, rbuf
+ roff
, sizeof(rbuf
) - roff
);
170 fprintf(stderr
, "%s: read\n", __func__
);
179 if (event_add(ev
, NULL
) == -1)
185 timeout_cb(int fd
, short event
, void *arg
)
190 evutil_gettimeofday(&tcalled
, NULL
);
191 if (evutil_timercmp(&tcalled
, &tset
, >))
192 evutil_timersub(&tcalled
, &tset
, &tv
);
194 evutil_timersub(&tset
, &tcalled
, &tv
);
196 diff
= tv
.tv_sec
*1000 + tv
.tv_usec
/1000 - SECONDS
* 1000;
206 signal_cb_sa(int sig
)
212 signal_cb(int fd
, short event
, void *arg
)
214 struct event
*ev
= arg
;
227 combined_read_cb(int fd
, short event
, void *arg
)
229 struct both
*both
= arg
;
233 len
= read(fd
, buf
, sizeof(buf
));
235 fprintf(stderr
, "%s: read\n", __func__
);
240 if (event_add(&both
->ev
, NULL
) == -1)
245 combined_write_cb(int fd
, short event
, void *arg
)
247 struct both
*both
= arg
;
252 if (len
> both
->nread
)
255 len
= write(fd
, buf
, len
);
257 fprintf(stderr
, "%s: write\n", __func__
);
259 shutdown(fd
, SHUT_WR
);
264 if (event_add(&both
->ev
, NULL
) == -1)
268 /* Test infrastructure */
271 setup_test(const char *name
)
274 fprintf(stdout
, "%s", name
);
276 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
277 fprintf(stderr
, "%s: socketpair\n", __func__
);
282 if (fcntl(pair
[0], F_SETFL
, O_NONBLOCK
) == -1)
283 fprintf(stderr
, "fcntl(O_NONBLOCK)");
285 if (fcntl(pair
[1], F_SETFL
, O_NONBLOCK
) == -1)
286 fprintf(stderr
, "fcntl(O_NONBLOCK)");
301 CloseHandle((HANDLE
)pair
[0]);
302 CloseHandle((HANDLE
)pair
[1]);
305 fprintf(stdout
, "OK\n");
307 fprintf(stdout
, "FAILED\n");
315 test_registerfds(void)
319 struct event read_evs
[512];
320 struct event write_evs
[512];
322 struct event_base
*base
= event_base_new();
324 fprintf(stdout
, "Testing register fds: ");
326 for (i
= 0; i
< 512; ++i
) {
327 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
328 /* run up to the limit of file descriptors */
331 event_set(&read_evs
[i
], pair
[0],
332 EV_READ
|EV_PERSIST
, simple_read_cb
, NULL
);
333 event_base_set(base
, &read_evs
[i
]);
334 event_add(&read_evs
[i
], NULL
);
335 event_set(&write_evs
[i
], pair
[1],
336 EV_WRITE
|EV_PERSIST
, simple_write_cb
, NULL
);
337 event_base_set(base
, &write_evs
[i
]);
338 event_add(&write_evs
[i
], NULL
);
341 event_base_loop(base
, EVLOOP_ONCE
);
344 /* now delete everything */
345 for (j
= 0; j
< i
; ++j
) {
346 event_del(&read_evs
[j
]);
347 event_del(&write_evs
[j
]);
349 close(read_evs
[j
].ev_fd
);
350 close(write_evs
[j
].ev_fd
);
352 CloseHandle((HANDLE
)read_evs
[j
].ev_fd
);
353 CloseHandle((HANDLE
)write_evs
[j
].ev_fd
);
357 event_base_loop(base
, EVLOOP_ONCE
);
360 event_base_free(base
);
362 fprintf(stdout
, "OK\n");
366 test_simpleread(void)
370 /* Very simple read test */
371 setup_test("Simple read: ");
373 write(pair
[0], TEST1
, strlen(TEST1
)+1);
374 shutdown(pair
[0], SHUT_WR
);
376 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
377 if (event_add(&ev
, NULL
) == -1)
385 test_simplewrite(void)
389 /* Very simple write test */
390 setup_test("Simple write: ");
392 event_set(&ev
, pair
[0], EV_WRITE
, simple_write_cb
, &ev
);
393 if (event_add(&ev
, NULL
) == -1)
403 struct event ev
, ev2
;
406 /* Multiple read and write test */
407 setup_test("Multiple read/write: ");
408 memset(rbuf
, 0, sizeof(rbuf
));
409 for (i
= 0; i
< sizeof(wbuf
); i
++)
415 event_set(&ev
, pair
[0], EV_WRITE
, multiple_write_cb
, &ev
);
416 if (event_add(&ev
, NULL
) == -1)
418 event_set(&ev2
, pair
[1], EV_READ
, multiple_read_cb
, &ev2
);
419 if (event_add(&ev2
, NULL
) == -1)
424 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
430 test_persistent(void)
432 struct event ev
, ev2
;
435 /* Multiple read and write test with persist */
436 setup_test("Persist read/write: ");
437 memset(rbuf
, 0, sizeof(rbuf
));
438 for (i
= 0; i
< sizeof(wbuf
); i
++)
444 event_set(&ev
, pair
[0], EV_WRITE
|EV_PERSIST
, multiple_write_cb
, &ev
);
445 if (event_add(&ev
, NULL
) == -1)
447 event_set(&ev2
, pair
[1], EV_READ
|EV_PERSIST
, multiple_read_cb
, &ev2
);
448 if (event_add(&ev2
, NULL
) == -1)
453 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
461 struct both r1
, r2
, w1
, w2
;
463 setup_test("Combined read/write: ");
464 memset(&r1
, 0, sizeof(r1
));
465 memset(&r2
, 0, sizeof(r2
));
466 memset(&w1
, 0, sizeof(w1
));
467 memset(&w2
, 0, sizeof(w2
));
472 event_set(&r1
.ev
, pair
[0], EV_READ
, combined_read_cb
, &r1
);
473 event_set(&w1
.ev
, pair
[0], EV_WRITE
, combined_write_cb
, &w1
);
474 event_set(&r2
.ev
, pair
[1], EV_READ
, combined_read_cb
, &r2
);
475 event_set(&w2
.ev
, pair
[1], EV_WRITE
, combined_write_cb
, &w2
);
476 if (event_add(&r1
.ev
, NULL
) == -1)
478 if (event_add(&w1
.ev
, NULL
))
480 if (event_add(&r2
.ev
, NULL
))
482 if (event_add(&w2
.ev
, NULL
))
487 if (r1
.nread
== 8192 && r2
.nread
== 4096)
494 test_simpletimeout(void)
499 setup_test("Simple timeout: ");
503 evtimer_set(&ev
, timeout_cb
, NULL
);
504 evtimer_add(&ev
, &tv
);
506 evutil_gettimeofday(&tset
, NULL
);
513 extern struct event_base
*current_base
;
516 child_signal_cb(int fd
, short event
, void *arg
)
531 int status
, got_sigchld
= 0;
532 struct event ev
, sig_ev
;
535 setup_test("After fork: ");
537 write(pair
[0], TEST1
, strlen(TEST1
)+1);
539 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
540 if (event_add(&ev
, NULL
) == -1)
543 signal_set(&sig_ev
, SIGCHLD
, child_signal_cb
, &got_sigchld
);
544 signal_add(&sig_ev
, NULL
);
546 if ((pid
= fork()) == 0) {
548 if (event_reinit(current_base
) == -1) {
549 fprintf(stderr
, "FAILED (reinit)\n");
559 /* we do not send an EOF; simple_read_cb requires an EOF
560 * to set test_ok. we just verify that the callback was
562 exit(test_ok
!= 0 || called
!= 2 ? -2 : 76);
565 /* wait for the child to read the data */
568 write(pair
[0], TEST1
, strlen(TEST1
)+1);
570 if (waitpid(pid
, &status
, 0) == -1) {
571 fprintf(stderr
, "FAILED (fork)\n");
575 if (WEXITSTATUS(status
) != 76) {
576 fprintf(stderr
, "FAILED (exit): %d\n", WEXITSTATUS(status
));
580 /* test that the current event loop still works */
581 write(pair
[0], TEST1
, strlen(TEST1
)+1);
582 shutdown(pair
[0], SHUT_WR
);
587 fprintf(stdout
, "FAILED (sigchld)\n");
597 test_simplesignal(void)
600 struct itimerval itv
;
602 setup_test("Simple signal: ");
603 signal_set(&ev
, SIGALRM
, signal_cb
, &ev
);
604 signal_add(&ev
, NULL
);
605 /* find bugs in which operations are re-ordered */
607 signal_add(&ev
, NULL
);
609 memset(&itv
, 0, sizeof(itv
));
610 itv
.it_value
.tv_sec
= 1;
611 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
612 goto skip_simplesignal
;
616 if (signal_del(&ev
) == -1)
623 test_multiplesignal(void)
625 struct event ev_one
, ev_two
;
626 struct itimerval itv
;
628 setup_test("Multiple signal: ");
630 signal_set(&ev_one
, SIGALRM
, signal_cb
, &ev_one
);
631 signal_add(&ev_one
, NULL
);
633 signal_set(&ev_two
, SIGALRM
, signal_cb
, &ev_two
);
634 signal_add(&ev_two
, NULL
);
636 memset(&itv
, 0, sizeof(itv
));
637 itv
.it_value
.tv_sec
= 1;
638 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
639 goto skip_simplesignal
;
644 if (signal_del(&ev_one
) == -1)
646 if (signal_del(&ev_two
) == -1)
653 test_immediatesignal(void)
658 printf("Immediate signal: ");
659 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
660 signal_add(&ev
, NULL
);
662 event_loop(EVLOOP_NONBLOCK
);
668 test_signal_dealloc(void)
670 /* make sure that signal_event is event_del'ed and pipe closed */
672 struct event_base
*base
= event_init();
673 printf("Signal dealloc: ");
674 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
675 signal_add(&ev
, NULL
);
677 event_base_free(base
);
678 /* If we got here without asserting, we're fine. */
684 test_signal_pipeloss(void)
686 /* make sure that the base1 pipe is closed correctly. */
687 struct event_base
*base1
, *base2
;
690 printf("Signal pipeloss: ");
691 base1
= event_init();
692 pipe1
= base1
->sig
.ev_signal_pair
[0];
693 base2
= event_init();
694 event_base_free(base2
);
695 event_base_free(base1
);
696 if (close(pipe1
) != -1 || errno
!=EBADF
) {
697 /* fd must be closed, so second close gives -1, EBADF */
698 printf("signal pipe not closed. ");
707 * make two bases to catch signals, use both of them. this only works
708 * for event mechanisms that use our signal pipe trick. kqueue handles
709 * signals internally, and all interested kqueues get all the signals.
712 test_signal_switchbase(void)
714 struct event ev1
, ev2
;
715 struct event_base
*base1
, *base2
;
718 printf("Signal switchbase: ");
719 base1
= event_init();
720 base2
= event_init();
721 is_kqueue
= !strcmp(event_get_method(),"kqueue");
722 signal_set(&ev1
, SIGUSR1
, signal_cb
, &ev1
);
723 signal_set(&ev2
, SIGUSR1
, signal_cb
, &ev2
);
724 if (event_base_set(base1
, &ev1
) ||
725 event_base_set(base2
, &ev2
) ||
726 event_add(&ev1
, NULL
) ||
727 event_add(&ev2
, NULL
)) {
728 fprintf(stderr
, "%s: cannot set base, add\n", __func__
);
733 /* can handle signal before loop is called */
735 event_base_loop(base2
, EVLOOP_NONBLOCK
);
741 event_base_loop(base1
, EVLOOP_NONBLOCK
);
742 if (test_ok
&& !is_kqueue
) {
745 /* set base1 to handle signals */
746 event_base_loop(base1
, EVLOOP_NONBLOCK
);
748 event_base_loop(base1
, EVLOOP_NONBLOCK
);
749 event_base_loop(base2
, EVLOOP_NONBLOCK
);
752 event_base_free(base1
);
753 event_base_free(base2
);
758 * assert that a signal event removed from the event queue really is
759 * removed - with no possibility of it's parent handler being fired.
762 test_signal_assert(void)
765 struct event_base
*base
= event_init();
767 printf("Signal handler assert: ");
768 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
769 signal_set(&ev
, SIGCONT
, signal_cb
, &ev
);
770 signal_add(&ev
, NULL
);
772 * if signal_del() fails to reset the handler, it's current handler
773 * will still point to evsignal_handler().
778 /* only way to verify we were in evsignal_handler() */
779 if (base
->sig
.evsignal_caught
)
784 event_base_free(base
);
790 * assert that we restore our previous signal handler properly.
793 test_signal_restore(void)
796 struct event_base
*base
= event_init();
797 #ifdef HAVE_SIGACTION
802 printf("Signal handler restore: ");
803 #ifdef HAVE_SIGACTION
804 sa
.sa_handler
= signal_cb_sa
;
806 sigemptyset(&sa
.sa_mask
);
807 if (sigaction(SIGUSR1
, &sa
, NULL
) == -1)
810 if (signal(SIGUSR1
, signal_cb_sa
) == SIG_ERR
)
813 signal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
814 signal_add(&ev
, NULL
);
818 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
822 event_base_free(base
);
828 signal_cb_swp(int sig
, short event
, void *arg
)
834 event_loopexit(NULL
);
837 timeout_cb_swp(int fd
, short event
, void *arg
)
840 struct timeval tv
= {5, 0};
843 evtimer_add((struct event
*)arg
, &tv
);
848 event_loopexit(NULL
);
852 test_signal_while_processing(void)
854 struct event_base
*base
= event_init();
855 struct event ev
, ev_timer
;
856 struct timeval tv
= {0, 0};
858 setup_test("Receiving a signal while processing other signal: ");
862 signal_set(&ev
, SIGUSR1
, signal_cb_swp
, NULL
);
863 signal_add(&ev
, NULL
);
864 evtimer_set(&ev_timer
, timeout_cb_swp
, &ev_timer
);
865 evtimer_add(&ev_timer
, &tv
);
868 event_base_free(base
);
875 test_free_active_base(void)
877 struct event_base
*base1
;
879 setup_test("Free active base: ");
880 base1
= event_init();
881 event_set(&ev1
, pair
[1], EV_READ
, simple_read_cb
, &ev1
);
882 event_base_set(base1
, &ev1
);
883 event_add(&ev1
, NULL
);
884 /* event_del(&ev1); */
885 event_base_free(base1
);
891 test_event_base_new(void)
893 struct event_base
*base
;
895 setup_test("Event base new: ");
897 write(pair
[0], TEST1
, strlen(TEST1
)+1);
898 shutdown(pair
[0], SHUT_WR
);
900 base
= event_base_new();
901 event_set(&ev1
, pair
[1], EV_READ
, simple_read_cb
, &ev1
);
902 event_base_set(base
, &ev1
);
903 event_add(&ev1
, NULL
);
905 event_base_dispatch(base
);
907 event_base_free(base
);
915 struct timeval tv
, tv_start
, tv_end
;
918 setup_test("Loop exit: ");
921 tv
.tv_sec
= 60*60*24;
922 evtimer_set(&ev
, timeout_cb
, NULL
);
923 evtimer_add(&ev
, &tv
);
929 evutil_gettimeofday(&tv_start
, NULL
);
931 evutil_gettimeofday(&tv_end
, NULL
);
932 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
943 test_loopexit_multiple(void)
946 struct event_base
*base
;
948 setup_test("Loop Multiple exit: ");
950 base
= event_base_new();
954 event_base_loopexit(base
, &tv
);
958 event_base_loopexit(base
, &tv
);
960 event_base_dispatch(base
);
962 event_base_free(base
);
970 break_cb(int fd
, short events
, void *arg
)
977 fail_cb(int fd
, short events
, void *arg
)
985 struct event ev1
, ev2
;
988 setup_test("Loop break: ");
992 evtimer_set(&ev1
, break_cb
, NULL
);
993 evtimer_add(&ev1
, &tv
);
994 evtimer_set(&ev2
, fail_cb
, NULL
);
995 evtimer_add(&ev2
, &tv
);
1006 test_evbuffer(void) {
1008 struct evbuffer
*evb
= evbuffer_new();
1009 setup_test("Testing Evbuffer: ");
1011 evbuffer_add_printf(evb
, "%s/%d", "hello", 1);
1013 if (EVBUFFER_LENGTH(evb
) == 7 &&
1014 strcmp((char*)EVBUFFER_DATA(evb
), "hello/1") == 0)
1023 test_evbuffer_find(void)
1026 const char* test1
= "1234567890\r\n";
1027 const char* test2
= "1234567890\r";
1028 #define EVBUFFER_INITIAL_LENGTH 256
1029 char test3
[EVBUFFER_INITIAL_LENGTH
];
1031 struct evbuffer
* buf
= evbuffer_new();
1033 /* make sure evbuffer_find doesn't match past the end of the buffer */
1034 fprintf(stdout
, "Testing evbuffer_find 1: ");
1035 evbuffer_add(buf
, (u_char
*)test1
, strlen(test1
));
1036 evbuffer_drain(buf
, strlen(test1
));
1037 evbuffer_add(buf
, (u_char
*)test2
, strlen(test2
));
1038 p
= evbuffer_find(buf
, (u_char
*)"\r\n", 2);
1040 fprintf(stdout
, "OK\n");
1042 fprintf(stdout
, "FAILED\n");
1047 * drain the buffer and do another find; in r309 this would
1048 * read past the allocated buffer causing a valgrind error.
1050 fprintf(stdout
, "Testing evbuffer_find 2: ");
1051 evbuffer_drain(buf
, strlen(test2
));
1052 for (i
= 0; i
< EVBUFFER_INITIAL_LENGTH
; ++i
)
1054 test3
[EVBUFFER_INITIAL_LENGTH
- 1] = 'x';
1055 evbuffer_add(buf
, (u_char
*)test3
, EVBUFFER_INITIAL_LENGTH
);
1056 p
= evbuffer_find(buf
, (u_char
*)"xy", 2);
1060 fprintf(stdout
, "FAILED\n");
1064 /* simple test for match at end of allocated buffer */
1065 fprintf(stdout
, "Testing evbuffer_find 3: ");
1066 p
= evbuffer_find(buf
, (u_char
*)"ax", 2);
1067 if (p
!= NULL
&& strncmp((char*)p
, "ax", 2) == 0) {
1070 fprintf(stdout
, "FAILED\n");
1078 * simple bufferevent test
1082 readcb(struct bufferevent
*bev
, void *arg
)
1084 if (EVBUFFER_LENGTH(bev
->input
) == 8333) {
1085 bufferevent_disable(bev
, EV_READ
);
1091 writecb(struct bufferevent
*bev
, void *arg
)
1093 if (EVBUFFER_LENGTH(bev
->output
) == 0)
1098 errorcb(struct bufferevent
*bev
, short what
, void *arg
)
1104 test_bufferevent(void)
1106 struct bufferevent
*bev1
, *bev2
;
1110 setup_test("Bufferevent: ");
1112 bev1
= bufferevent_new(pair
[0], readcb
, writecb
, errorcb
, NULL
);
1113 bev2
= bufferevent_new(pair
[1], readcb
, writecb
, errorcb
, NULL
);
1115 bufferevent_disable(bev1
, EV_READ
);
1116 bufferevent_enable(bev2
, EV_READ
);
1118 for (i
= 0; i
< sizeof(buffer
); i
++)
1121 bufferevent_write(bev1
, buffer
, sizeof(buffer
));
1125 bufferevent_free(bev1
);
1126 bufferevent_free(bev2
);
1135 * test watermarks and bufferevent
1139 wm_readcb(struct bufferevent
*bev
, void *arg
)
1141 int len
= EVBUFFER_LENGTH(bev
->input
);
1144 assert(len
>= 10 && len
<= 20);
1146 evbuffer_drain(bev
->input
, len
);
1149 if (nread
== 65000) {
1150 bufferevent_disable(bev
, EV_READ
);
1156 wm_writecb(struct bufferevent
*bev
, void *arg
)
1158 if (EVBUFFER_LENGTH(bev
->output
) == 0)
1163 wm_errorcb(struct bufferevent
*bev
, short what
, void *arg
)
1169 test_bufferevent_watermarks(void)
1171 struct bufferevent
*bev1
, *bev2
;
1175 setup_test("Bufferevent Watermarks: ");
1177 bev1
= bufferevent_new(pair
[0], NULL
, wm_writecb
, wm_errorcb
, NULL
);
1178 bev2
= bufferevent_new(pair
[1], wm_readcb
, NULL
, wm_errorcb
, NULL
);
1180 bufferevent_disable(bev1
, EV_READ
);
1181 bufferevent_enable(bev2
, EV_READ
);
1183 for (i
= 0; i
< sizeof(buffer
); i
++)
1186 bufferevent_write(bev1
, buffer
, sizeof(buffer
));
1188 /* limit the reading on the receiving bufferevent */
1189 bufferevent_setwatermark(bev2
, EV_READ
, 10, 20);
1193 bufferevent_free(bev1
);
1194 bufferevent_free(bev2
);
1202 struct test_pri_event
{
1208 test_priorities_cb(int fd
, short what
, void *arg
)
1210 struct test_pri_event
*pri
= arg
;
1213 if (pri
->count
== 3) {
1214 event_loopexit(NULL
);
1220 evutil_timerclear(&tv
);
1221 event_add(&pri
->ev
, &tv
);
1225 test_priorities(int npriorities
)
1228 struct test_pri_event one
, two
;
1231 evutil_snprintf(buf
, sizeof(buf
), "Testing Priorities %d: ", npriorities
);
1234 event_base_priority_init(global_base
, npriorities
);
1236 memset(&one
, 0, sizeof(one
));
1237 memset(&two
, 0, sizeof(two
));
1239 timeout_set(&one
.ev
, test_priorities_cb
, &one
);
1240 if (event_priority_set(&one
.ev
, 0) == -1) {
1241 fprintf(stderr
, "%s: failed to set priority", __func__
);
1245 timeout_set(&two
.ev
, test_priorities_cb
, &two
);
1246 if (event_priority_set(&two
.ev
, npriorities
- 1) == -1) {
1247 fprintf(stderr
, "%s: failed to set priority", __func__
);
1251 evutil_timerclear(&tv
);
1253 if (event_add(&one
.ev
, &tv
) == -1)
1255 if (event_add(&two
.ev
, &tv
) == -1)
1263 if (npriorities
== 1) {
1264 if (one
.count
== 3 && two
.count
== 3)
1266 } else if (npriorities
== 2) {
1267 /* Two is called once because event_loopexit is priority 1 */
1268 if (one
.count
== 3 && two
.count
== 1)
1271 if (one
.count
== 3 && two
.count
== 0)
1279 test_multiple_cb(int fd
, short event
, void *arg
)
1281 if (event
& EV_READ
)
1283 else if (event
& EV_WRITE
)
1288 test_multiple_events_for_same_fd(void)
1290 struct event e1
, e2
;
1292 setup_test("Multiple events for same fd: ");
1294 event_set(&e1
, pair
[0], EV_READ
, test_multiple_cb
, NULL
);
1295 event_add(&e1
, NULL
);
1296 event_set(&e2
, pair
[0], EV_WRITE
, test_multiple_cb
, NULL
);
1297 event_add(&e2
, NULL
);
1298 event_loop(EVLOOP_ONCE
);
1300 write(pair
[1], TEST1
, strlen(TEST1
)+1);
1301 event_loop(EVLOOP_ONCE
);
1310 int evtag_decode_int(uint32_t *pnumber
, struct evbuffer
*evbuf
);
1311 int evtag_encode_tag(struct evbuffer
*evbuf
, uint32_t number
);
1312 int evtag_decode_tag(uint32_t *pnumber
, struct evbuffer
*evbuf
);
1315 read_once_cb(int fd
, short event
, void *arg
)
1320 len
= read(fd
, buf
, sizeof(buf
));
1325 /* Assumes global pair[0] can be used for writing */
1326 write(pair
[0], TEST1
, strlen(TEST1
)+1);
1334 test_want_only_once(void)
1339 /* Very simple read test */
1340 setup_test("Want read only once: ");
1342 write(pair
[0], TEST1
, strlen(TEST1
)+1);
1344 /* Setup the loop termination */
1345 evutil_timerclear(&tv
);
1347 event_loopexit(&tv
);
1349 event_set(&ev
, pair
[1], EV_READ
, read_once_cb
, &ev
);
1350 if (event_add(&ev
, NULL
) == -1)
1357 #define TEST_MAX_INT 6
1360 evtag_int_test(void)
1362 struct evbuffer
*tmp
= evbuffer_new();
1363 uint32_t integers
[TEST_MAX_INT
] = {
1364 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1369 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1371 oldlen
= EVBUFFER_LENGTH(tmp
);
1372 encode_int(tmp
, integers
[i
]);
1373 newlen
= EVBUFFER_LENGTH(tmp
);
1374 fprintf(stdout
, "\t\tencoded 0x%08x with %d bytes\n",
1375 integers
[i
], newlen
- oldlen
);
1378 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1379 if (evtag_decode_int(&integer
, tmp
) == -1) {
1380 fprintf(stderr
, "decode %d failed", i
);
1383 if (integer
!= integers
[i
]) {
1384 fprintf(stderr
, "got %x, wanted %x",
1385 integer
, integers
[i
]);
1390 if (EVBUFFER_LENGTH(tmp
) != 0) {
1391 fprintf(stderr
, "trailing data");
1396 fprintf(stdout
, "\t%s: OK\n", __func__
);
1402 u_char buffer
[4096];
1403 struct evbuffer
*tmp
= evbuffer_new();
1408 for (j
= 0; j
< 100; j
++) {
1409 for (i
= 0; i
< sizeof(buffer
); i
++)
1411 evbuffer_drain(tmp
, -1);
1412 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1414 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1)
1418 /* The majority of decodes should fail */
1419 if (not_failed
>= 10) {
1420 fprintf(stderr
, "evtag_unmarshal should have failed");
1424 /* Now insert some corruption into the tag length field */
1425 evbuffer_drain(tmp
, -1);
1426 evutil_timerclear(&tv
);
1428 evtag_marshal_timeval(tmp
, 0, &tv
);
1429 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1431 EVBUFFER_DATA(tmp
)[1] = 0xff;
1432 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1) {
1433 fprintf(stderr
, "evtag_unmarshal_timeval should have failed");
1439 fprintf(stdout
, "\t%s: OK\n", __func__
);
1443 evtag_tag_encoding(void)
1445 struct evbuffer
*tmp
= evbuffer_new();
1446 uint32_t integers
[TEST_MAX_INT
] = {
1447 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1452 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1454 oldlen
= EVBUFFER_LENGTH(tmp
);
1455 evtag_encode_tag(tmp
, integers
[i
]);
1456 newlen
= EVBUFFER_LENGTH(tmp
);
1457 fprintf(stdout
, "\t\tencoded 0x%08x with %d bytes\n",
1458 integers
[i
], newlen
- oldlen
);
1461 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1462 if (evtag_decode_tag(&integer
, tmp
) == -1) {
1463 fprintf(stderr
, "decode %d failed", i
);
1466 if (integer
!= integers
[i
]) {
1467 fprintf(stderr
, "got %x, wanted %x",
1468 integer
, integers
[i
]);
1473 if (EVBUFFER_LENGTH(tmp
) != 0) {
1474 fprintf(stderr
, "trailing data");
1479 fprintf(stdout
, "\t%s: OK\n", __func__
);
1485 fprintf(stdout
, "Testing Tagging:\n");
1491 evtag_tag_encoding();
1493 fprintf(stdout
, "OK\n");
1500 struct msg
*msg
, *msg2
;
1501 struct kill
*attack
;
1503 struct evbuffer
*tmp
= evbuffer_new();
1504 struct timeval tv_start
, tv_end
;
1508 fprintf(stdout
, "Testing RPC: ");
1511 EVTAG_ASSIGN(msg
, from_name
, "niels");
1512 EVTAG_ASSIGN(msg
, to_name
, "phoenix");
1514 if (EVTAG_GET(msg
, attack
, &attack
) == -1) {
1515 fprintf(stderr
, "Failed to set kill message.\n");
1519 EVTAG_ASSIGN(attack
, weapon
, "feather");
1520 EVTAG_ASSIGN(attack
, action
, "tickle");
1522 evutil_gettimeofday(&tv_start
, NULL
);
1523 for (i
= 0; i
< 1000; ++i
) {
1524 run
= EVTAG_ADD(msg
, run
);
1526 fprintf(stderr
, "Failed to add run message.\n");
1529 EVTAG_ASSIGN(run
, how
, "very fast but with some data in it");
1530 EVTAG_ASSIGN(run
, fixed_bytes
,
1531 (unsigned char*)"012345678901234567890123");
1534 if (msg_complete(msg
) == -1) {
1535 fprintf(stderr
, "Failed to make complete message.\n");
1539 evtag_marshal_msg(tmp
, 0xdeaf, msg
);
1541 if (evtag_peek(tmp
, &tag
) == -1) {
1542 fprintf(stderr
, "Failed to peak tag.\n");
1546 if (tag
!= 0xdeaf) {
1547 fprintf(stderr
, "Got incorrect tag: %0x.\n", tag
);
1552 if (evtag_unmarshal_msg(tmp
, 0xdeaf, msg2
) == -1) {
1553 fprintf(stderr
, "Failed to unmarshal message.\n");
1557 evutil_gettimeofday(&tv_end
, NULL
);
1558 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
1559 fprintf(stderr
, "(%.1f us/add) ",
1560 (float)tv_end
.tv_sec
/(float)i
* 1000000.0 +
1561 tv_end
.tv_usec
/ (float)i
);
1563 if (!EVTAG_HAS(msg2
, from_name
) ||
1564 !EVTAG_HAS(msg2
, to_name
) ||
1565 !EVTAG_HAS(msg2
, attack
)) {
1566 fprintf(stderr
, "Missing data structures.\n");
1570 if (EVTAG_LEN(msg2
, run
) != i
) {
1571 fprintf(stderr
, "Wrong number of run messages.\n");
1580 fprintf(stdout
, "OK\n");
1585 test_evutil_strtoll(void)
1589 setup_test("evutil_stroll: ");
1592 if (evutil_strtoll("5000000000", NULL
, 10) != ((ev_int64_t
)5000000)*1000)
1594 if (evutil_strtoll("-5000000000", NULL
, 10) != ((ev_int64_t
)5000000)*-1000)
1597 if (evutil_strtoll(s
, &endptr
, 10) != (ev_int64_t
)99999)
1601 if (evutil_strtoll("foo", NULL
, 10) != 0)
1611 main (int argc
, char **argv
)
1614 WORD wVersionRequested
;
1618 wVersionRequested
= MAKEWORD( 2, 2 );
1620 err
= WSAStartup( wVersionRequested
, &wsaData
);
1624 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
1627 setvbuf(stdout
, NULL
, _IONBF
, 0);
1629 /* Initalize the event library */
1630 global_base
= event_init();
1634 test_evutil_strtoll();
1636 /* use the global event base and need to be called first */
1642 test_evbuffer_find();
1645 test_bufferevent_watermarks();
1647 test_free_active_base();
1649 test_event_base_new();
1673 test_simpletimeout();
1675 test_simplesignal();
1676 test_multiplesignal();
1677 test_immediatesignal();
1682 test_loopexit_multiple();
1684 test_multiple_events_for_same_fd();
1686 test_want_only_once();
1693 test_signal_dealloc();
1694 test_signal_pipeloss();
1695 test_signal_switchbase();
1696 test_signal_restore();
1697 test_signal_assert();
1698 test_signal_while_processing();