OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu> | 2 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu> |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 #include <sys/types.h> | 37 #include <sys/types.h> |
38 #include <sys/stat.h> | 38 #include <sys/stat.h> |
39 #ifdef HAVE_SYS_TIME_H | 39 #ifdef HAVE_SYS_TIME_H |
40 #include <sys/time.h> | 40 #include <sys/time.h> |
41 #endif | 41 #endif |
42 #include <sys/queue.h> | 42 #include <sys/queue.h> |
43 #ifndef WIN32 | 43 #ifndef WIN32 |
44 #include <sys/socket.h> | 44 #include <sys/socket.h> |
45 #include <sys/wait.h> | 45 #include <sys/wait.h> |
46 #include <sys/signal.h> | 46 #include <signal.h> |
47 #include <unistd.h> | 47 #include <unistd.h> |
48 #include <netdb.h> | 48 #include <netdb.h> |
49 #endif | 49 #endif |
50 #include <assert.h> | 50 #include <assert.h> |
51 #include <fcntl.h> | 51 #include <fcntl.h> |
52 #include <signal.h> | 52 #include <signal.h> |
53 #include <stdlib.h> | 53 #include <stdlib.h> |
54 #include <stdio.h> | 54 #include <stdio.h> |
55 #include <string.h> | 55 #include <string.h> |
56 #include <errno.h> | 56 #include <errno.h> |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 #define write(fd,buf,len) send((fd),(buf),(len),0) | 88 #define write(fd,buf,len) send((fd),(buf),(len),0) |
89 #define read(fd,buf,len) recv((fd),(buf),(len),0) | 89 #define read(fd,buf,len) recv((fd),(buf),(len),0) |
90 #endif | 90 #endif |
91 | 91 |
92 static void | 92 static void |
93 simple_read_cb(int fd, short event, void *arg) | 93 simple_read_cb(int fd, short event, void *arg) |
94 { | 94 { |
95 char buf[256]; | 95 char buf[256]; |
96 int len; | 96 int len; |
97 | 97 |
| 98 if (arg == NULL) |
| 99 return; |
| 100 |
98 len = read(fd, buf, sizeof(buf)); | 101 len = read(fd, buf, sizeof(buf)); |
99 | 102 |
100 if (len) { | 103 if (len) { |
101 if (!called) { | 104 if (!called) { |
102 if (event_add(arg, NULL) == -1) | 105 if (event_add(arg, NULL) == -1) |
103 exit(1); | 106 exit(1); |
104 } | 107 } |
105 } else if (called == 1) | 108 } else if (called == 1) |
106 test_ok = 1; | 109 test_ok = 1; |
107 | 110 |
108 called++; | 111 called++; |
109 } | 112 } |
110 | 113 |
111 static void | 114 static void |
112 simple_write_cb(int fd, short event, void *arg) | 115 simple_write_cb(int fd, short event, void *arg) |
113 { | 116 { |
114 int len; | 117 int len; |
115 | 118 |
| 119 if (arg == NULL) |
| 120 return; |
| 121 |
116 len = write(fd, TEST1, strlen(TEST1) + 1); | 122 len = write(fd, TEST1, strlen(TEST1) + 1); |
117 if (len == -1) | 123 if (len == -1) |
118 test_ok = 0; | 124 test_ok = 0; |
119 else | 125 else |
120 test_ok = 1; | 126 test_ok = 1; |
121 } | 127 } |
122 | 128 |
123 static void | 129 static void |
124 multiple_write_cb(int fd, short event, void *arg) | 130 multiple_write_cb(int fd, short event, void *arg) |
125 { | 131 { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 evutil_timersub(&tset, &tcalled, &tv); | 194 evutil_timersub(&tset, &tcalled, &tv); |
189 | 195 |
190 diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000; | 196 diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000; |
191 if (diff < 0) | 197 if (diff < 0) |
192 diff = -diff; | 198 diff = -diff; |
193 | 199 |
194 if (diff < 100) | 200 if (diff < 100) |
195 test_ok = 1; | 201 test_ok = 1; |
196 } | 202 } |
197 | 203 |
| 204 #ifndef WIN32 |
198 static void | 205 static void |
199 signal_cb_sa(int sig) | 206 signal_cb_sa(int sig) |
200 { | 207 { |
201 test_ok = 2; | 208 test_ok = 2; |
202 } | 209 } |
203 | 210 |
204 static void | 211 static void |
205 signal_cb(int fd, short event, void *arg) | 212 signal_cb(int fd, short event, void *arg) |
206 { | 213 { |
207 struct event *ev = arg; | 214 struct event *ev = arg; |
208 | 215 |
209 signal_del(ev); | 216 signal_del(ev); |
210 test_ok = 1; | 217 test_ok = 1; |
211 } | 218 } |
| 219 #endif |
212 | 220 |
213 struct both { | 221 struct both { |
214 struct event ev; | 222 struct event ev; |
215 int nread; | 223 int nread; |
216 }; | 224 }; |
217 | 225 |
218 static void | 226 static void |
219 combined_read_cb(int fd, short event, void *arg) | 227 combined_read_cb(int fd, short event, void *arg) |
220 { | 228 { |
221 struct both *both = arg; | 229 struct both *both = arg; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 fprintf(stdout, "OK\n"); | 305 fprintf(stdout, "OK\n"); |
298 else { | 306 else { |
299 fprintf(stdout, "FAILED\n"); | 307 fprintf(stdout, "FAILED\n"); |
300 exit(1); | 308 exit(1); |
301 } | 309 } |
302 test_ok = 0; | 310 test_ok = 0; |
303 return (0); | 311 return (0); |
304 } | 312 } |
305 | 313 |
306 static void | 314 static void |
| 315 test_registerfds(void) |
| 316 { |
| 317 int i, j; |
| 318 int pair[2]; |
| 319 struct event read_evs[512]; |
| 320 struct event write_evs[512]; |
| 321 |
| 322 struct event_base *base = event_base_new(); |
| 323 |
| 324 fprintf(stdout, "Testing register fds: "); |
| 325 |
| 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 */ |
| 329 break; |
| 330 } |
| 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); |
| 339 |
| 340 /* just loop once */ |
| 341 event_base_loop(base, EVLOOP_ONCE); |
| 342 } |
| 343 |
| 344 /* now delete everything */ |
| 345 for (j = 0; j < i; ++j) { |
| 346 event_del(&read_evs[j]); |
| 347 event_del(&write_evs[j]); |
| 348 #ifndef WIN32 |
| 349 close(read_evs[j].ev_fd); |
| 350 close(write_evs[j].ev_fd); |
| 351 #else |
| 352 CloseHandle((HANDLE)read_evs[j].ev_fd); |
| 353 CloseHandle((HANDLE)write_evs[j].ev_fd); |
| 354 #endif |
| 355 |
| 356 /* just loop once */ |
| 357 event_base_loop(base, EVLOOP_ONCE); |
| 358 } |
| 359 |
| 360 event_base_free(base); |
| 361 |
| 362 fprintf(stdout, "OK\n"); |
| 363 } |
| 364 |
| 365 static void |
307 test_simpleread(void) | 366 test_simpleread(void) |
308 { | 367 { |
309 struct event ev; | 368 struct event ev; |
310 | 369 |
311 /* Very simple read test */ | 370 /* Very simple read test */ |
312 setup_test("Simple read: "); | 371 setup_test("Simple read: "); |
313 | 372 |
314 write(pair[0], TEST1, strlen(TEST1)+1); | 373 write(pair[0], TEST1, strlen(TEST1)+1); |
315 shutdown(pair[0], SHUT_WR); | 374 shutdown(pair[0], SHUT_WR); |
316 | 375 |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 | 816 |
758 raise(SIGUSR1); | 817 raise(SIGUSR1); |
759 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */ | 818 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */ |
760 if (test_ok != 2) | 819 if (test_ok != 2) |
761 test_ok = 0; | 820 test_ok = 0; |
762 out: | 821 out: |
763 event_base_free(base); | 822 event_base_free(base); |
764 cleanup_test(); | 823 cleanup_test(); |
765 return; | 824 return; |
766 } | 825 } |
| 826 |
| 827 static void |
| 828 signal_cb_swp(int sig, short event, void *arg) |
| 829 { |
| 830 called++; |
| 831 if (called < 5) |
| 832 raise(sig); |
| 833 else |
| 834 event_loopexit(NULL); |
| 835 } |
| 836 static void |
| 837 timeout_cb_swp(int fd, short event, void *arg) |
| 838 { |
| 839 if (called == -1) { |
| 840 struct timeval tv = {5, 0}; |
| 841 |
| 842 called = 0; |
| 843 evtimer_add((struct event *)arg, &tv); |
| 844 raise(SIGUSR1); |
| 845 return; |
| 846 } |
| 847 test_ok = 0; |
| 848 event_loopexit(NULL); |
| 849 } |
| 850 |
| 851 static void |
| 852 test_signal_while_processing(void) |
| 853 { |
| 854 struct event_base *base = event_init(); |
| 855 struct event ev, ev_timer; |
| 856 struct timeval tv = {0, 0}; |
| 857 |
| 858 setup_test("Receiving a signal while processing other signal: "); |
| 859 |
| 860 called = -1; |
| 861 test_ok = 1; |
| 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); |
| 866 event_dispatch(); |
| 867 |
| 868 event_base_free(base); |
| 869 cleanup_test(); |
| 870 return; |
| 871 } |
767 #endif | 872 #endif |
768 | 873 |
769 static void | 874 static void |
770 test_free_active_base(void) | 875 test_free_active_base(void) |
771 { | 876 { |
772 struct event_base *base1; | 877 struct event_base *base1; |
773 struct event ev1; | 878 struct event ev1; |
774 setup_test("Free active base: "); | 879 setup_test("Free active base: "); |
775 base1 = event_init(); | 880 base1 = event_init(); |
776 event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1); | 881 event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1); |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1508 #ifdef WIN32 | 1613 #ifdef WIN32 |
1509 WORD wVersionRequested; | 1614 WORD wVersionRequested; |
1510 WSADATA wsaData; | 1615 WSADATA wsaData; |
1511 int err; | 1616 int err; |
1512 | 1617 |
1513 wVersionRequested = MAKEWORD( 2, 2 ); | 1618 wVersionRequested = MAKEWORD( 2, 2 ); |
1514 | 1619 |
1515 err = WSAStartup( wVersionRequested, &wsaData ); | 1620 err = WSAStartup( wVersionRequested, &wsaData ); |
1516 #endif | 1621 #endif |
1517 | 1622 |
| 1623 #ifndef WIN32 |
| 1624 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) |
| 1625 return (1); |
| 1626 #endif |
1518 setvbuf(stdout, NULL, _IONBF, 0); | 1627 setvbuf(stdout, NULL, _IONBF, 0); |
1519 | 1628 |
1520 /* Initalize the event library */ | 1629 /* Initalize the event library */ |
1521 global_base = event_init(); | 1630 global_base = event_init(); |
1522 | 1631 |
| 1632 test_registerfds(); |
| 1633 |
1523 test_evutil_strtoll(); | 1634 test_evutil_strtoll(); |
1524 | 1635 |
1525 /* use the global event base and need to be called first */ | 1636 /* use the global event base and need to be called first */ |
1526 test_priorities(1); | 1637 test_priorities(1); |
1527 test_priorities(2); | 1638 test_priorities(2); |
1528 test_priorities(3); | 1639 test_priorities(3); |
1529 | 1640 |
1530 test_evbuffer(); | 1641 test_evbuffer(); |
1531 test_evbuffer_find(); | 1642 test_evbuffer_find(); |
1532 | 1643 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1577 evtag_test(); | 1688 evtag_test(); |
1578 | 1689 |
1579 #ifndef WIN32 | 1690 #ifndef WIN32 |
1580 rpc_test(); | 1691 rpc_test(); |
1581 | 1692 |
1582 test_signal_dealloc(); | 1693 test_signal_dealloc(); |
1583 test_signal_pipeloss(); | 1694 test_signal_pipeloss(); |
1584 test_signal_switchbase(); | 1695 test_signal_switchbase(); |
1585 test_signal_restore(); | 1696 test_signal_restore(); |
1586 test_signal_assert(); | 1697 test_signal_assert(); |
| 1698 test_signal_while_processing(); |
1587 #endif | 1699 #endif |
1588 | 1700 |
1589 return (0); | 1701 return (0); |
1590 } | 1702 } |
1591 | 1703 |
OLD | NEW |