libevent patch required for SMTP test
[ioevent.git] / test / io-test-eof.cpp
blobb8e6d4aa3b587a6c14217781627eb35ac31ab8ab
1 /*
2 * Embedded build:
3 * c++ -I$(LIBEV_SRC) -o io-test-eof -DEV_STANDALONE=1 io-test-eof.cpp $(LIBEV_SRC)/ev.c $(LIBEV_SRC)/event.c
6 * Wed 2006-12-27 - Modified by Leandro Lucarella <llucax+eventxx@gmail.com>
8 * Adapted to test the C++ inteface.
10 * Wed 2007-12-19 - Modified by Chris Brody <chris.brody@gmail.com>
12 * Adapted to test the ioevent C++ inteface.
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <cstdlib>
23 #include <cstdio>
24 #include <cstring>
25 #include <cerrno>
27 #include <ioevent>
29 int test_okay = 1;
30 int called = 0;
32 ioevent * ev;
34 void
35 read_cb(int fd, short event, void *arg)
37 char buf[256];
38 int len;
40 len = read(fd, buf, sizeof(buf));
42 printf("%s: read %d%s\n", __func__,
43 len, len ? "" : " - means EOF");
45 if (len == 0) {
46 ev->stop();
48 if (called == 1)
49 test_okay = 0;
52 called++;
55 int
56 main (int argc, char **argv)
58 const char * test = "test string";
59 int pair[2];
61 IOLOOP loop = ioloop_new();
63 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
64 return (1);
66 write(pair[0], test, strlen(test)+1);
67 shutdown(pair[0], SHUT_WR);
69 /* Init one event */
70 ev = new ioevent(pair[1], EV_READ, read_cb, NULL);
72 ev->start();
74 ioloop_dispatch(loop);
76 delete ev;
78 return (test_okay);