-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtest_epoll.lua
More file actions
466 lines (435 loc) · 12.8 KB
/
Copy pathtest_epoll.lua
File metadata and controls
466 lines (435 loc) · 12.8 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
local lt = require "ltest"
local epoll = require "bee.epoll"
local socket = require "bee.socket"
local time = require "bee.time"
local m = lt.test "epoll"
local function SimpleServer(protocol, ...)
local fd = assert(socket.create(protocol))
assert(fd:bind(...))
assert(fd:listen())
return fd
end
local function SimpleClient(protocol, ...)
local fd = assert(socket.create(protocol))
local ok, err = fd:connect(...)
assert(ok ~= nil, err)
return fd
end
function m.test_create()
lt.assertFailed("maxevents is less than or equal to zero.", epoll.create(-1))
lt.assertFailed("maxevents is less than or equal to zero.", epoll.create(0))
local epfd <close> = epoll.create(16)
lt.assertIsUserdata(epfd)
end
function m.test_close()
local epfd = epoll.create(16)
local fd <close> = SimpleServer("tcp", "127.0.0.1", 0)
lt.assertEquals(true, epfd:event_add(fd, 0))
lt.assertEquals(true, epfd:close())
lt.assertEquals(epfd:close(), nil)
lt.assertEquals(epfd:event_add(fd, 0), nil)
end
function m.test_event()
local epfd = epoll.create(16)
local fd <close> = SimpleServer("tcp", "127.0.0.1", 0)
lt.assertIsNil(epfd:event_mod(fd, 0))
lt.assertIsNil(epfd:event_del(fd))
lt.assertEquals(true, epfd:event_add(fd, 0))
lt.assertIsNil(epfd:event_add(fd, 0))
lt.assertEquals(true, epfd:event_mod(fd, 0))
lt.assertEquals(true, epfd:event_del(fd))
lt.assertIsNil(epfd:event_mod(fd, 0))
lt.assertIsNil(epfd:event_del(fd))
lt.assertEquals(true, epfd:event_add(fd, 0))
lt.assertIsNil(epfd:event_add(fd, 0))
lt.assertEquals(true, epfd:event_mod(fd, 0))
lt.assertEquals(true, epfd:event_del(fd))
epfd:close()
end
function m.test_enum()
lt.assertEquals(epoll.EPOLLIN, 1 << 0)
lt.assertEquals(epoll.EPOLLPRI, 1 << 1)
lt.assertEquals(epoll.EPOLLOUT, 1 << 2)
lt.assertEquals(epoll.EPOLLERR, 1 << 3)
lt.assertEquals(epoll.EPOLLHUP, 1 << 4)
lt.assertEquals(epoll.EPOLLRDNORM, 1 << 6)
lt.assertEquals(epoll.EPOLLRDBAND, 1 << 7)
lt.assertEquals(epoll.EPOLLWRNORM, 1 << 8)
lt.assertEquals(epoll.EPOLLWRBAND, 1 << 9)
lt.assertEquals(epoll.EPOLLMSG, 1 << 10)
lt.assertEquals(epoll.EPOLLRDHUP, 1 << 13)
lt.assertEquals(epoll.EPOLLONESHOT, 1 << 30)
end
function m.test_wait()
do
local epfd = epoll.create(16)
epfd:close()
lt.assertIsNil(epfd:wait())
end
do
local epfd <close> = epoll.create(16)
lt.assertIsFunction(epfd:wait(0))
for _ in epfd:wait(0) do
lt.failure "Shouldn't run to here."
end
end
local epfd <close> = epoll.create(16)
local fd <close> = SimpleServer("tcp", "127.0.0.1", 0)
epfd:event_add(fd, 0, fd)
for _ in epfd:wait(0) do
lt.failure "Shouldn't run to here."
end
end
local events = {
"EPOLLIN",
"EPOLLPRI",
"EPOLLOUT",
"EPOLLERR",
"EPOLLHUP",
"EPOLLRDNORM",
"EPOLLRDBAND",
"EPOLLWRNORM",
"EPOLLWRBAND",
"EPOLLMSG",
"EPOLLRDHUP",
"EPOLLONESHOT",
"EPOLLET",
}
local function event_tostring(e)
if e == 0 then
lt.failure "unknown flags: 0"
return "0"
end
local r = {}
for _, name in ipairs(events) do
local v = epoll[name]
if v and e & v ~= 0 then
r[#r+1] = name
e = e & ~v
if e == 0 then
return table.concat(r, " | ")
end
end
end
r[#r+1] = string.format("0x%x", e)
local res = table.concat(r, " | ")
lt.failure("unknown flags: %s", res)
return res
end
local function event_tointeger(e)
local r = 0
for name in e:gmatch "[A-Z]+" do
local v = epoll[name]
if v then
r = r | v
end
end
return r
end
local function event_bor(a, b)
if a then
return event_tostring(event_tointeger(a) | b)
end
return event_tostring(b)
end
local function assertEpollWait(epfd, values)
local actual = {}
local expected = {}
if type(values[1]) == "table" then
for _, v in ipairs(values) do
expected[v[1]] = v[2]
end
else
local v = values
if v[1] then
expected[v[1]] = v[2]
end
end
local start = time.monotonic()
while time.monotonic() - start < 100 do
for s, event in epfd:wait(1) do
actual[s] = event_bor(actual[s], event)
end
if lt.equals(actual, expected) then
return
end
end
lt.assertEquals(actual, expected)
end
function m.test_connect_event()
do
local sfd <close> = SimpleServer("tcp", "127.0.0.1", 0)
local cfd <close> = SimpleClient("tcp", sfd:info "socket")
local sep <const> = epoll.create(16)
local cep <const> = epoll.create(16)
sep:event_add(sfd, epoll.EPOLLIN | epoll.EPOLLOUT, sfd)
cep:event_add(cfd, epoll.EPOLLIN | epoll.EPOLLOUT, cfd)
assertEpollWait(sep, { sfd, "EPOLLIN" })
assertEpollWait(cep, { cfd, "EPOLLOUT" })
end
do
local sfd <close> = SimpleServer("tcp", "127.0.0.1", 0)
local cfd <close> = SimpleClient("tcp", sfd:info "socket")
local sep <const> = epoll.create(16)
local cep <const> = epoll.create(16)
sep:event_add(sfd, epoll.EPOLLIN | epoll.EPOLLOUT, "")
sep:event_mod(sfd, epoll.EPOLLIN | epoll.EPOLLOUT, "server")
cep:event_add(cfd, epoll.EPOLLIN | epoll.EPOLLOUT, "")
cep:event_mod(cfd, epoll.EPOLLIN | epoll.EPOLLOUT, "client")
assertEpollWait(sep, { "server", "EPOLLIN" })
assertEpollWait(cep, { "client", "EPOLLOUT" })
end
end
function m.test_send_recv_event()
local sfd <close> = SimpleServer("tcp", "127.0.0.1", 0)
local cfd <close> = SimpleClient("tcp", sfd:info "socket")
local sep <const> = epoll.create(16)
local cep <const> = epoll.create(16)
sep:event_add(sfd, epoll.EPOLLIN | epoll.EPOLLOUT, sfd)
cep:event_add(cfd, epoll.EPOLLIN | epoll.EPOLLOUT, cfd)
assertEpollWait(sep, { sfd, "EPOLLIN" })
assertEpollWait(cep, { cfd, "EPOLLOUT" })
local newfd = sfd:accept()
sep:event_add(newfd, epoll.EPOLLIN | epoll.EPOLLOUT, newfd)
assertEpollWait(sep, { newfd, "EPOLLOUT" })
local function test(data)
lt.assertEquals(newfd:send(data), #data)
assertEpollWait(sep, { newfd, "EPOLLOUT" })
assertEpollWait(cep, { cfd, "EPOLLIN | EPOLLOUT" })
lt.assertEquals(cfd:recv(), data)
lt.assertEquals({ cfd:recv() }, { false })
assertEpollWait(cep, { cfd, "EPOLLOUT" })
end
test "1234567890"
end
function m.test_shutdown_event()
local sfd <close> = SimpleServer("tcp", "127.0.0.1", 0)
local cfd <close> = SimpleClient("tcp", sfd:info "socket")
local sep <const> = epoll.create(16)
local cep <const> = epoll.create(16)
sep:event_add(sfd, epoll.EPOLLIN | epoll.EPOLLOUT | epoll.EPOLLRDHUP, sfd)
cep:event_add(cfd, epoll.EPOLLIN | epoll.EPOLLOUT | epoll.EPOLLRDHUP, cfd)
assertEpollWait(sep, { sfd, "EPOLLIN" })
assertEpollWait(cep, { cfd, "EPOLLOUT" })
local newfd <close> = sfd:accept()
sep:event_add(newfd, epoll.EPOLLIN | epoll.EPOLLOUT | epoll.EPOLLRDHUP, newfd)
newfd:shutdown "w"
assertEpollWait(sep, { newfd, "EPOLLOUT" })
lt.assertIsNil(newfd:send "")
assertEpollWait(cep, { cfd, "EPOLLIN | EPOLLOUT | EPOLLRDHUP" })
lt.assertEquals({ newfd:recv() }, { false })
newfd:shutdown "r"
assertEpollWait(cep, { cfd, "EPOLLIN | EPOLLOUT | EPOLLRDHUP" })
lt.assertIsNil(newfd:recv())
end
local EPOLLIN <const> = epoll.EPOLLIN
local EPOLLOUT <const> = epoll.EPOLLOUT
local EPOLLERR <const> = epoll.EPOLLERR
local EPOLLHUP <const> = epoll.EPOLLHUP
local function create_stream(epfd, fd)
local sock = { fd = fd }
local kMaxReadBufSize <const> = 4 * 1024
local rbuf = ""
local wbuf = ""
local closed = false
local halfclose_r = false
local halfclose_w = false
local event_r = false
local event_w = false
local event_mask = 0
local function closefd()
epfd:event_del(fd)
fd:close()
end
local function event_update()
local mask = 0
if event_r then
mask = mask | EPOLLIN
end
if event_w then
mask = mask | EPOLLOUT
end
if mask ~= event_mask then
epfd:event_mod(fd, mask)
event_mask = mask
end
end
local function force_close()
closefd()
assert(halfclose_r)
assert(halfclose_w)
end
local function on_read()
local data, err = fd:recv()
if data == nil then
if err then
lt.failure("recv error: %s", err)
end
halfclose_r = true
event_r = false
if halfclose_w then
force_close()
elseif #wbuf == 0 then
halfclose_w = true
event_w = false
force_close()
else
event_update()
end
elseif data == false then
else
rbuf = rbuf..data
if #rbuf > kMaxReadBufSize then
event_r = false
event_update()
end
sock:on_recv()
end
end
local function on_write()
local n, err = fd:send(wbuf)
if n == nil then
if err then
lt.failure("send error: %s", err)
end
halfclose_w = true
event_w = false
if halfclose_r then
force_close()
else
event_update()
end
elseif n == false then
else
wbuf = wbuf:sub(n + 1)
if #wbuf == 0 then
event_w = false
event_update()
end
end
end
local function on_event(e)
if e & (EPOLLERR | EPOLLHUP) ~= 0 then
e = e & (EPOLLIN | EPOLLOUT)
end
if e & EPOLLIN ~= 0 then
assert(not halfclose_r)
on_read()
end
if e & EPOLLOUT ~= 0 then
if not halfclose_w then
on_write()
end
end
end
epfd:event_add(fd, 0, on_event)
event_r = true
event_update()
function sock:send(data)
if closed then
return
end
if #data > 0 then
wbuf = wbuf..data
event_w = true
event_update()
end
end
function sock:recv(n)
if n == 0 then
return ""
end
local res
local full = #rbuf >= kMaxReadBufSize
if n == nil then
res = rbuf
rbuf = ""
elseif n <= #rbuf then
res = rbuf:sub(1, n)
rbuf = rbuf:sub(n + 1)
elseif n >= kMaxReadBufSize then
res = rbuf
rbuf = ""
else
return
end
if full then
event_r = true
event_update()
end
return res
end
function sock:close()
if closed or halfclose_r then
return
end
halfclose_r = true
event_r = false
if halfclose_w then
force_close()
else
event_update()
end
end
return sock
end
local function create_listen(epfd, fd)
local sock = { fd = fd }
local function closefd()
epfd:event_del(fd)
fd:close()
end
local function on_event(e)
if e & (EPOLLERR | EPOLLHUP) ~= 0 then
closefd()
return
end
if e & EPOLLIN ~= 0 then
sock:on_accept()
end
end
epfd:event_add(fd, EPOLLIN, on_event)
return sock
end
function m.test_pingpong()
local epfd = epoll.create(512)
local quit = false
local s = SimpleServer("tcp", "127.0.0.1", 0)
local server = create_listen(epfd, s)
function server:on_accept()
local newfd = assert(self.fd:accept())
local session = create_stream(epfd, newfd)
function session:on_recv()
local res = self:recv(6)
if res == nil then
return
end
local strid = res:match "^PING%-(%d)$"
lt.assertIsString(strid)
self:send("PONG-"..strid)
end
end
local c = SimpleClient("tcp", s:info "socket")
local client = create_stream(epfd, c)
client:send "PING-1"
function client:on_recv()
local res = self:recv(6)
if res == nil then
return
end
local strid = res:match "^PONG%-(%d)$"
lt.assertIsString(strid)
local id = tonumber(strid)
if id < 9 then
self:send("PING-"..(id + 1))
else
quit = true
end
end
while not quit do
for f, event in epfd:wait() do
f(event)
end
end
epfd:close()
end