Skip to content

Commit e97c9bb

Browse files
committed
Added tests: epoll
1 parent 0493185 commit e97c9bb

5 files changed

Lines changed: 443 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
import epoll;
3+
4+
class Main
5+
{
6+
static public main(argv)
7+
{/*{{{*/
8+
9+
// - $Epoll static to_string -
10+
if ($Epoll != "Epoll")
11+
{
12+
new Exception("Epoll to_string: expected Epoll, got %s" % $Epoll).throw();
13+
}
14+
15+
// - CLOEXEC create flag -
16+
if (type Epoll.CLOEXEC != type Integer)
17+
{
18+
new Exception("CLOEXEC: expected Integer type").throw();
19+
}
20+
21+
// - event type constants (8) -
22+
event_consts = [
23+
Epoll.EPOLLIN,
24+
Epoll.EPOLLOUT,
25+
Epoll.EPOLLRDHUP,
26+
Epoll.EPOLLPRI,
27+
Epoll.EPOLLERR,
28+
Epoll.EPOLLHUP,
29+
Epoll.EPOLLET,
30+
Epoll.EPOLLONESHOT,
31+
];
32+
33+
if (event_consts.length() != 8)
34+
{
35+
new Exception("event constants: expected 8, got %d" % event_consts.length()).throw();
36+
}
37+
38+
idx = 0;
39+
while (idx < event_consts.length())
40+
{
41+
if (type event_consts[idx] != type Integer)
42+
{
43+
new Exception("event constant %d: expected Integer type" % idx).throw();
44+
}
45+
idx += 1;
46+
}
47+
48+
// - EPOLLIN and EPOLLOUT should be positive -
49+
if (Epoll.EPOLLIN <= 0)
50+
{
51+
new Exception("EPOLLIN: expected positive, got %d" % Epoll.EPOLLIN).throw();
52+
}
53+
if (Epoll.EPOLLOUT <= 0)
54+
{
55+
new Exception("EPOLLOUT: expected positive, got %d" % Epoll.EPOLLOUT).throw();
56+
}
57+
}/*}}}*/
58+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
2+
import sys;
3+
import epoll;
4+
5+
class Main
6+
{
7+
static public main(argv)
8+
{/*{{{*/
9+
10+
// - constructor with 0 flags -
11+
ep = new Epoll(0);
12+
if ($ep != "Epoll")
13+
{
14+
new Exception("Epoll(0) to_string: expected Epoll, got %s" % $ep).throw();
15+
}
16+
17+
// - constructor with CLOEXEC -
18+
ep = new Epoll(Epoll.CLOEXEC);
19+
if ($ep != "Epoll")
20+
{
21+
new Exception("Epoll(CLOEXEC) to_string: expected Epoll, got %s" % $ep).throw();
22+
}
23+
24+
// - create pipe for testable fds -
25+
pp = Sys.pipe();
26+
rd_fd = pp[0].get_fd();
27+
wr_fd = pp[1].get_fd();
28+
29+
// - has before add returns 0 -
30+
if (ep.has(rd_fd) != 0)
31+
{
32+
new Exception("has before add: expected 0, got %d" % ep.has(rd_fd)).throw();
33+
}
34+
35+
// - add returns self -
36+
ret = ep.add(rd_fd,Epoll.EPOLLIN);
37+
if ($ret != "Epoll")
38+
{
39+
new Exception("add return: expected Epoll, got %s" % $ret).throw();
40+
}
41+
42+
// - has after add returns 1 -
43+
if (ep.has(rd_fd) != 1)
44+
{
45+
new Exception("has after add: expected 1, got %d" % ep.has(rd_fd)).throw();
46+
}
47+
48+
// - add second fd -
49+
ep.add(wr_fd,Epoll.EPOLLOUT);
50+
if (ep.has(wr_fd) != 1)
51+
{
52+
new Exception("has wr_fd after add: expected 1, got %d" % ep.has(wr_fd)).throw();
53+
}
54+
55+
// - del returns self -
56+
ret = ep.del(rd_fd);
57+
if ($ret != "Epoll")
58+
{
59+
new Exception("del return: expected Epoll, got %s" % $ret).throw();
60+
}
61+
62+
// - has after del returns 0 -
63+
if (ep.has(rd_fd) != 0)
64+
{
65+
new Exception("has after del: expected 0, got %d" % ep.has(rd_fd)).throw();
66+
}
67+
68+
// - other fd still tracked -
69+
if (ep.has(wr_fd) != 1)
70+
{
71+
new Exception("has wr_fd after del rd_fd: expected 1, got %d" % ep.has(wr_fd)).throw();
72+
}
73+
74+
// - re-add rd_fd for reinit test -
75+
ep.add(rd_fd,Epoll.EPOLLIN);
76+
77+
// - reinit returns self -
78+
ret = ep.reinit();
79+
if ($ret != "Epoll")
80+
{
81+
new Exception("reinit return: expected Epoll, got %s" % $ret).throw();
82+
}
83+
84+
// - fds still tracked after reinit -
85+
if (ep.has(rd_fd) != 1)
86+
{
87+
new Exception("has rd_fd after reinit: expected 1, got %d" % ep.has(rd_fd)).throw();
88+
}
89+
if (ep.has(wr_fd) != 1)
90+
{
91+
new Exception("has wr_fd after reinit: expected 1, got %d" % ep.has(wr_fd)).throw();
92+
}
93+
94+
// - add with modified events (re-add same fd = modify) -
95+
ep.add(rd_fd,Epoll.EPOLLIN | Epoll.EPOLLPRI);
96+
97+
// - cleanup -
98+
pp[0].close();
99+
pp[1].close();
100+
}/*}}}*/
101+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
import sys;
3+
import epoll;
4+
5+
class Main
6+
{
7+
static public main(argv)
8+
{/*{{{*/
9+
10+
pp = Sys.pipe();
11+
rd_fd = pp[0].get_fd();
12+
wr_fd = pp[1].get_fd();
13+
14+
// --- timeout case: empty pipe, 0ms timeout ---
15+
ep = new Epoll(Epoll.CLOEXEC);
16+
ep.add(rd_fd,Epoll.EPOLLIN);
17+
18+
result = ep.wait(1,0);
19+
if (Blank != result)
20+
{
21+
new Exception("wait timeout: expected Blank, got %s" % $result).throw();
22+
}
23+
24+
// --- writable fd: pipe write end is immediately writable ---
25+
ep2 = new Epoll(Epoll.CLOEXEC);
26+
ep2.add(wr_fd,Epoll.EPOLLOUT);
27+
28+
result = ep2.wait(1,100);
29+
if (Blank == result)
30+
{
31+
new Exception("wait writable: expected events, got Blank").throw();
32+
}
33+
34+
// - result is array with at least 2 elements [fd, events] -
35+
if (result.length() < 2)
36+
{
37+
new Exception("wait writable: expected >= 2 elements, got %d" % result.length()).throw();
38+
}
39+
40+
// - fd matches -
41+
if (result[0] != wr_fd)
42+
{
43+
new Exception("wait writable fd: expected %d, got %d" % [wr_fd,result[0]]).throw();
44+
}
45+
46+
// - EPOLLOUT should be set -
47+
if (Epoll.EPOLLOUT & result[1] == 0)
48+
{
49+
new Exception("wait writable: EPOLLOUT not set in events").throw();
50+
}
51+
52+
// --- readable fd: write data then check EPOLLIN ---
53+
pp[1].write("test data\n");
54+
pp[1].flush();
55+
56+
result = ep.wait(1,100);
57+
if (Blank == result)
58+
{
59+
new Exception("wait readable: expected events, got Blank").throw();
60+
}
61+
62+
if (result[0] != rd_fd)
63+
{
64+
new Exception("wait readable fd: expected %d, got %d" % [rd_fd,result[0]]).throw();
65+
}
66+
67+
if (Epoll.EPOLLIN & result[1] == 0)
68+
{
69+
new Exception("wait readable: EPOLLIN not set").throw();
70+
}
71+
72+
// --- multi-fd: both fds ready ---
73+
ep3 = new Epoll(Epoll.CLOEXEC);
74+
ep3.add(rd_fd,Epoll.EPOLLIN);
75+
ep3.add(wr_fd,Epoll.EPOLLOUT);
76+
77+
result = ep3.wait(10,100);
78+
if (Blank == result)
79+
{
80+
new Exception("wait multi: expected events, got Blank").throw();
81+
}
82+
83+
// - should have 4 elements (2 fds x 2 per fd) -
84+
if (result.length() != 4)
85+
{
86+
new Exception("wait multi: expected 4 elements, got %d" % result.length()).throw();
87+
}
88+
89+
// - cleanup -
90+
pp[0].close();
91+
pp[1].close();
92+
}/*}}}*/
93+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
import sys;
3+
import epoll;
4+
5+
class Main
6+
{
7+
static public main(argv)
8+
{/*{{{*/
9+
10+
pp1 = Sys.pipe();
11+
pp2 = Sys.pipe();
12+
rd1 = pp1[0].get_fd();
13+
wr1 = pp1[1].get_fd();
14+
rd2 = pp2[0].get_fd();
15+
wr2 = pp2[1].get_fd();
16+
17+
ep = new Epoll(Epoll.CLOEXEC);
18+
19+
// - update with empty array is no-op -
20+
ret = ep.update([]);
21+
if ($ret != "Epoll")
22+
{
23+
new Exception("update empty return: expected Epoll, got %s" % $ret).throw();
24+
}
25+
26+
// - update adds new fds -
27+
ep.update([wr1,Epoll.EPOLLOUT,wr2,Epoll.EPOLLOUT]);
28+
if (ep.has(wr1) != 1)
29+
{
30+
new Exception("update add wr1: expected has=1, got %d" % ep.has(wr1)).throw();
31+
}
32+
if (ep.has(wr2) != 1)
33+
{
34+
new Exception("update add wr2: expected has=1, got %d" % ep.has(wr2)).throw();
35+
}
36+
37+
// - verify both are writable via wait -
38+
result = ep.wait(10,100);
39+
if (Blank == result)
40+
{
41+
new Exception("update wait: expected events, got Blank").throw();
42+
}
43+
if (result.length() != 4)
44+
{
45+
new Exception("update wait: expected 4 elements, got %d" % result.length()).throw();
46+
}
47+
48+
// - update replacing set: keep wr1, drop wr2, add rd1 -
49+
pp1[1].write("data\n");
50+
pp1[1].flush();
51+
ep.update([wr1,Epoll.EPOLLOUT,rd1,Epoll.EPOLLIN]);
52+
53+
// - wr2 should be gone -
54+
if (ep.has(wr2) != 0)
55+
{
56+
new Exception("update replace wr2: expected has=0, got %d" % ep.has(wr2)).throw();
57+
}
58+
// - rd1 should be added -
59+
if (ep.has(rd1) != 1)
60+
{
61+
new Exception("update replace rd1: expected has=1, got %d" % ep.has(rd1)).throw();
62+
}
63+
// - wr1 should still be there -
64+
if (ep.has(wr1) != 1)
65+
{
66+
new Exception("update replace wr1: expected has=1, got %d" % ep.has(wr1)).throw();
67+
}
68+
69+
// - update to empty set removes everything -
70+
ep.update([]);
71+
if (ep.has(wr1) != 0)
72+
{
73+
new Exception("update empty wr1: expected has=0, got %d" % ep.has(wr1)).throw();
74+
}
75+
if (ep.has(rd1) != 0)
76+
{
77+
new Exception("update empty rd1: expected has=0, got %d" % ep.has(rd1)).throw();
78+
}
79+
80+
// - cleanup -
81+
pp1[0].close();
82+
pp1[1].close();
83+
pp2[0].close();
84+
pp2[1].close();
85+
}/*}}}*/
86+
}

0 commit comments

Comments
 (0)