Skip to content

Commit

Permalink
Merge branch 'amitm/testpoll' of github.com:amitmurthy/julia into jn/…
Browse files Browse the repository at this point in the history
…pollfd_fix
  • Loading branch information
vtjnash committed Jun 16, 2014
2 parents 4fb6fb9 + b02ddf0 commit 2eb615e
Showing 1 changed file with 69 additions and 33 deletions.
102 changes: 69 additions & 33 deletions test/pollfd.jl
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

0 comments on commit 2eb615e

Please sign in to comment.