-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_epoll.c
107 lines (86 loc) · 2.55 KB
/
test_epoll.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdlib.h>
#include <sys/socket.h>
#include <assert.h>
#include <unistd.h>
#include <sys/epoll.h>
#include "test_helper.h"
#include "tests.h"
int test_epoll (void) {
int e = epoll_create(0);
assert(e > 2);
int s = socket(AF_INET, SOCK_STREAM, 0);
assert(s > e);
assert(bind(s, NULL, 0) == 0);
assert(listen(s, 1) == 0);
struct epoll_event ev = {
.events = EPOLLIN | EPOLLOUT,
.data = {
.u64 = 0x1337,
},
};
assert(epoll_ctl(e, EPOLL_CTL_ADD, s, &ev) == 0);
ev = (struct epoll_event) {
.events = EPOLLOUT,
.data = {
.u64 = 1111,
},
};
assert(epoll_ctl(e, EPOLL_CTL_ADD, 1, &ev) == 0);
struct epoll_event results[256] = {0};
/* wait for server ready */
assert(epoll_wait(e, results, 256, 0) == 1);
assert(results[0].events == EPOLLIN);
assert(results[0].data.u64 == 0x1337);
/* get client connection */
int c = accept(s, NULL, NULL);
assert(c > s);
ev = (struct epoll_event) {
.events = EPOLLIN | EPOLLOUT,
.data = {
.u64 = 1234,
},
};
assert(epoll_ctl(e, EPOLL_CTL_ADD, c, &ev) == 0);
/* wait for client ready */
assert(epoll_wait(e, results, 256, 0) == 1);
assert(results[0].events == (EPOLLIN | EPOLLOUT));
assert(results[0].data.u64 == 1234);
/* wait for client again */
assert(epoll_wait(e, results, 256, 0) == 1);
assert(results[0].events == (EPOLLIN | EPOLLOUT));
assert(results[0].data.u64 == 1234);
/* remove client */
close(c);
assert(epoll_ctl(e, EPOLL_CTL_DEL, c, NULL) == -1);
/* wait for server ready again */
assert(epoll_wait(e, results, 256, 0) == 1);
assert(results[0].events == EPOLLIN);
assert(results[0].data.u64 == 0x1337);
/* delete server */
assert(epoll_ctl(e, EPOLL_CTL_DEL, s, NULL) == 0);
assert(epoll_ctl(e, EPOLL_CTL_DEL, 1, NULL) == 0);
/* wait on empty list */
assert(epoll_wait(e, results, 256, 0) == 0);
close(s);
close(e);
return TEST_SUCCESS;
}
int test_passthrough (void) {
int e = epoll_create(0);
assert(e > 2);
struct epoll_event ev = {
.events = EPOLLIN,
.data = {
.u64 = 0x1337,
},
};
assert(epoll_ctl(e, EPOLL_CTL_ADD, 0, &ev) == 0);
struct epoll_event results[256] = {0};
assert(epoll_wait(e, results, 256, 1000) == 0);
return TEST_SUCCESS;
}
test_fn tests [] = {
test_epoll,
test_passthrough,
NULL,
};