forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollfd.jl
51 lines (37 loc) · 1.03 KB
/
pollfd.jl
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
@unix_only begin
require("testdefs.jl")
pipe_fds = Array(Cint,2)
@test 0 == ccall(:pipe, Cint, (Ptr{Cint},), pipe_fds)
function test_poll_fd(timeout_s)
rc = poll_fd(RawFD(pipe_fds[1]), timeout_s; readable=true)
produce(rc)
end
function test_timeout_fd(tval)
tic()
t = Task(()->test_poll_fd(tval))
tr = consume(t)
t_elapsed = toq()
@test tr.timedout
@test tval <= t_elapsed
end
function test_read(slval)
tval = slval * 1.1
tic()
t = Task(()->test_poll_fd(tval))
sleep(slval)
@test 1 == ccall(:write, Csize_t, (Cint, Ptr{Uint8},Csize_t), pipe_fds[2], bytestring("A"), 1)
tr = consume(t)
t_elapsed = toq()
@test isreadable(tr) || iswritable(tr)
dout = Array(Uint8, 1)
@test 1 == ccall(:read, Csize_t, (Cint, Ptr{Uint8},Csize_t), pipe_fds[1], dout, 1)
@test dout[1] == int8('A')
@test slval <= t_elapsed
end
test_timeout_fd(.1)
test_timeout_fd(1)
test_read(.1)
test_read(1)
ccall(:close, Cint, (Cint,), pipe_fds[1])
ccall(:close, Cint, (Cint,), pipe_fds[2])
end