]> arthur.barton.de Git - netatalk.git/blob - libevent/test/test-eof.c
Merge master
[netatalk.git] / libevent / test / test-eof.c
1 /*
2  * Compile with:
3  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4  */
5 #include "event2/event-config.h"
6
7 #ifdef WIN32
8 #include <winsock2.h>
9 #else
10 #include <unistd.h>
11 #endif
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #ifdef _EVENT_HAVE_SYS_TIME_H
15 #include <sys/time.h>
16 #endif
17 #ifdef _EVENT_HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
19 #endif
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <event.h>
27 #include <evutil.h>
28
29 #ifdef _EVENT___func__
30 #define __func__ _EVENT___func__
31 #endif
32
33 int test_okay = 1;
34 int called = 0;
35 struct timeval timeout = {60, 0};
36
37 static void
38 read_cb(evutil_socket_t fd, short event, void *arg)
39 {
40         char buf[256];
41         int len;
42
43         if (EV_TIMEOUT & event) {
44                 printf("%s: Timeout!\n", __func__);
45                 exit(1);
46         }
47
48         len = recv(fd, buf, sizeof(buf), 0);
49
50         printf("%s: read %d%s\n", __func__,
51             len, len ? "" : " - means EOF");
52
53         if (len) {
54                 if (!called)
55                         event_add(arg, &timeout);
56         } else if (called == 1)
57                 test_okay = 0;
58
59         called++;
60 }
61
62 #ifndef SHUT_WR
63 #define SHUT_WR 1
64 #endif
65
66 int
67 main(int argc, char **argv)
68 {
69         struct event ev;
70         const char *test = "test string";
71         evutil_socket_t pair[2];
72
73 #ifdef WIN32
74         WORD wVersionRequested;
75         WSADATA wsaData;
76         int     err;
77
78         wVersionRequested = MAKEWORD(2, 2);
79
80         err = WSAStartup(wVersionRequested, &wsaData);
81 #endif
82
83         if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
84                 return (1);
85
86
87         send(pair[0], test, (int)strlen(test)+1, 0);
88         shutdown(pair[0], SHUT_WR);
89
90         /* Initalize the event library */
91         event_init();
92
93         /* Initalize one event */
94         event_set(&ev, pair[1], EV_READ | EV_TIMEOUT, read_cb, &ev);
95
96         event_add(&ev, &timeout);
97
98         event_dispatch();
99
100         return (test_okay);
101 }
102