-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'amitm/testpoll' of github.com:amitmurthy/julia into jn/…
…pollfd_fix
- Loading branch information
Showing
1 changed file
with
69 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,87 @@ | ||
@unix_only begin | ||
require("testdefs.jl") | ||
|
||
pipe_fds = Array(Cint,2) | ||
@test 0 == ccall(:pipe, Cint, (Ptr{Cint},), pipe_fds) | ||
# This script does the following | ||
# Sets up n unix pipes | ||
# For the odd pipes, a byte is written to the write end at intervals specified in intvls | ||
# Nothing is written into the even numbered pipes | ||
# Odd numbered pipes are tested for reads | ||
# Even numbered pipes are tested for timeouts | ||
# Writable ends are always tested for writability before a write | ||
|
||
function test_poll_fd(timeout_s) | ||
rc = poll_fd(RawFD(pipe_fds[1]), timeout_s; readable=true) | ||
produce(rc) | ||
end | ||
n = 20 | ||
intvls = [0.1, 0.1, 1.0, 2.0] # NOTE: The first interval is just used to let the readers/writers sort of synchnronize. | ||
|
||
function test_timeout_fd(tval) | ||
tic() | ||
t = Task(()->test_poll_fd(tval)) | ||
tr = consume(t) | ||
t_elapsed = toq() | ||
pipe_fds = cell(n) | ||
for i in 1:n | ||
pipe_fds[i] = Array(Cint, 2) | ||
@test 0 == ccall(:pipe, Cint, (Ptr{Cint},), pipe_fds[i]) | ||
end | ||
|
||
@test tr.timedout | ||
|
||
@test tval <= t_elapsed | ||
function pfd_tst_reads(idx) | ||
for (i, intvl) in enumerate(intvls) | ||
tic() | ||
evt = poll_fd(RawFD(pipe_fds[idx][1]), intvl * 10.0; readable=true, writable=true) | ||
t_elapsed = toq() | ||
@test evt.readable && !(evt.writable) && !(evt.timedout) | ||
|
||
# ignore the first one, everyone is just getting setup and synchronized | ||
if i > 1 | ||
# println("i ", i, ", Expected ", intvl, ", actual ", t_elapsed, ", diff ", t_elapsed - intvl) | ||
# Assuming that a 200 millisecond buffer is good enough on a modern system | ||
@test t_elapsed <= (intvl + 0.2) | ||
end | ||
|
||
|
||
dout = Array(Uint8, 1) | ||
@test 1 == ccall(:read, Csize_t, (Cint, Ptr{Uint8},Csize_t), pipe_fds[idx][1], dout, 1) | ||
@test dout[1] == int8('A') | ||
end | ||
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) | ||
function pfd_tst_timeout(idx) | ||
for intvl in intvls | ||
tic() | ||
evt = poll_fd(RawFD(pipe_fds[idx][1]), intvl; readable=true, writable=true) | ||
@test !(evt.readable) && !(evt.writable) && evt.timedout | ||
t_elapsed = toq() | ||
|
||
@test (intvl <= t_elapsed) && (t_elapsed <= (intvl + 0.2)) | ||
end | ||
end | ||
|
||
tr = consume(t) | ||
t_elapsed = toq() | ||
|
||
@test isreadable(tr) || iswritable(tr) | ||
# Odd numbers trigger reads, even numbers timeout | ||
@sync begin | ||
for i in 1:n | ||
if isodd(i) | ||
@async pfd_tst_reads(i) | ||
else | ||
@async pfd_tst_timeout(i) | ||
end | ||
end | ||
|
||
for (i, intvl) in enumerate(intvls) | ||
sleep(intvl) | ||
# tickle only the odd ones, but test for writablity for everyone | ||
for idx in 1:n | ||
evt = poll_fd(RawFD(pipe_fds[idx][2]), 0.0; readable=true, writable=true) | ||
@test !(evt.readable) && evt.writable && !(evt.timedout) | ||
|
||
if isodd(idx) | ||
@test 1 == ccall(:write, Csize_t, (Cint, Ptr{Uint8},Csize_t), pipe_fds[idx][2], bytestring("A"), 1) | ||
end | ||
end | ||
end | ||
end | ||
|
||
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 | ||
for i in 1:n | ||
ccall(:close, Cint, (Cint,), pipe_fds[i][1]) | ||
ccall(:close, Cint, (Cint,), pipe_fds[i][2]) | ||
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 |