Refactor a SocketStream unittest.
[chromium-blink-merge.git] / third_party / libevent / test / test-weof.c
blob7fd6c8b1f966db513620799e8a44ad7ba5810e50
1 /*
2 * Compile with:
3 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4 */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
10 #ifdef WIN32
11 #include <winsock2.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #ifdef HAVE_SYS_TIME_H
16 #include <sys/time.h>
17 #endif
18 #ifdef HAVE_SYS_SOCKET_H
19 #include <sys/socket.h>
20 #endif
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <signal.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <errno.h>
31 #include <event.h>
32 #include <evutil.h>
34 int pair[2];
35 int test_okay = 1;
36 int called = 0;
38 static void
39 write_cb(int fd, short event, void *arg)
41 const char *test = "test string";
42 int len;
44 len = send(fd, test, strlen(test) + 1, 0);
46 printf("%s: write %d%s\n", __func__,
47 len, len ? "" : " - means EOF");
49 if (len > 0) {
50 if (!called)
51 event_add(arg, NULL);
52 EVUTIL_CLOSESOCKET(pair[0]);
53 } else if (called == 1)
54 test_okay = 0;
56 called++;
59 int
60 main (int argc, char **argv)
62 struct event ev;
64 #ifndef WIN32
65 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
66 return (1);
67 #endif
69 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
70 return (1);
72 /* Initalize the event library */
73 event_init();
75 /* Initalize one event */
76 event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
78 event_add(&ev, NULL);
80 event_dispatch();
82 return (test_okay);