Skip to content

Commit

Permalink
improve async tests by checking (waiting) for task completion success…
Browse files Browse the repository at this point in the history
… and using listenany so that multiple test suites can run in parallel with less likelyhood of collisions
  • Loading branch information
vtjnash committed Jan 16, 2015
1 parent 2e2e4f6 commit 7a50a64
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
40 changes: 21 additions & 19 deletions test/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,28 +140,30 @@ workloads = hist(@parallel((a,b)->[a,b], for i=1:7; myid(); end), nprocs())[2]
@test @parallel(+, for i=1:2; i; end) == 3

# Testing timedwait on multiple RemoteRefs
rr1 = RemoteRef()
rr2 = RemoteRef()
rr3 = RemoteRef()
@sync begin
rr1 = RemoteRef()
rr2 = RemoteRef()
rr3 = RemoteRef()

@async begin sleep(0.5); put!(rr1, :ok) end
@async begin sleep(1.0); put!(rr2, :ok) end
@async begin sleep(2.0); put!(rr3, :ok) end
@async begin sleep(0.5); put!(rr1, :ok) end
@async begin sleep(1.0); put!(rr2, :ok) end
@async begin sleep(2.0); put!(rr3, :ok) end

tic()
timedwait(1.0) do
all(map(isready, [rr1, rr2, rr3]))
end
et=toq()

# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU
try
@test (et >= 1.0) && (et <= 1.5)
@test !isready(rr3)
catch
warn("timedwait tests delayed. et=$et, isready(rr3)=$(isready(rr3))")
tic()
timedwait(1.0) do
all(map(isready, [rr1, rr2, rr3]))
end
et=toq()
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU
try
@test (et >= 1.0) && (et <= 1.5)
@test !isready(rr3)
catch
warn("timedwait tests delayed. et=$et, isready(rr3)=$(isready(rr3))")
end
@test isready(rr1)
end
@test isready(rr1)


# TODO: The below block should be always enabled but the error is printed by the event loop

Expand Down
42 changes: 29 additions & 13 deletions test/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,26 @@
@test repr(ip"2001:db8:0:0:1:0:0:1") == "ip\"2001:db8::1:0:0:1\""
@test repr(ip"2001:0:0:1:0:0:0:1") == "ip\"2001:0:0:1::1\""

port = RemoteRef()
c = Base.Condition()
port = rand(2000:4000)
@async begin
s = listen(port)
defaultport = rand(2000:4000)
tsk = @async begin
p, s = listenany(defaultport)
put!(port, p)
Base.notify(c)
sock = accept(s)
write(sock,"Hello World\n")
close(s)
close(sock)
end
wait(c)
@test readall(connect(port)) == "Hello World\n"
@test readall(connect(fetch(port))) == "Hello World\n"
wait(tsk)

socketname = @windows ? "\\\\.\\pipe\\uv-test" : "testsocket"
socketname = (@windows ? "\\\\.\\pipe\\uv-test" : "testsocket") * "-" * randstring(6)
@unix_only isfile(socketname) && Base.FS.unlink(socketname)
for T in (ASCIIString, UTF8String, UTF16String) # test for issue #9435
@async begin
tsk = @async begin
s = listen(T(socketname))
Base.notify(c)
sock = accept(s)
Expand All @@ -57,21 +60,31 @@ for T in (ASCIIString, UTF8String, UTF16String) # test for issue #9435
end
wait(c)
@test readall(connect(socketname)) == "Hello World\n"
wait(tsk)
end

@test_throws Base.UVError getaddrinfo(".invalid")
@test_throws Base.UVError connect("localhost", 21452)

server = listen(port)
@async @test_throws ErrorException accept(server)
sleep(0.1)
p, server = listenany(defaultport)
r = RemoteRef()
tsk = @async begin
put!(r, :start)
@test_throws Base.UVError accept(server)
end
@test fetch(r) === :start
close(server)
wait(tsk)

server = listen(port)
port, server = listenany(defaultport)
@async connect("localhost",port)
s1 = accept(server)
@test_throws ErrorException accept(server,s1)
@test_throws Base.UVError listen(port)
port2, server2 = listenany(port)
@test port != port2
close(server)
close(server2)

@test_throws Base.UVError connect(".invalid",80)

Expand All @@ -82,7 +95,7 @@ begin
bind(b,ip"127.0.0.1",port+1)

c = Condition()
@async begin
tsk = @async begin
@test bytestring(recv(a)) == "Hello World"
# Issue 6505
@async begin
Expand All @@ -93,14 +106,16 @@ begin
end
send(b,ip"127.0.0.1",port,"Hello World")
wait(c)
wait(tsk)

@async begin
tsk = @async begin
@test begin
(addr,data) = recvfrom(a)
addr == ip"127.0.0.1" && bytestring(data) == "Hello World"
end
end
send(b, ip"127.0.0.1",port,"Hello World")
wait(tsk)

@test_throws MethodError bind(UDPSocket(),port)

Expand All @@ -113,11 +128,12 @@ begin
bind(a, ip"::1", uint16(port))
bind(b, ip"::1", uint16(port+1))

@async begin
tsk = @async begin
@test begin
(addr, data) = recvfrom(a)
addr == ip"::1" && bytestring(data) == "Hello World"
end
end
send(b, ip"::1", port, "Hello World")
wait(tsk)
end
13 changes: 8 additions & 5 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,16 @@ rm(file)

# Stream Redirection
@unix_only begin
r = RemoteRef()
@async begin
server = listen(2326)
port, server = listenany(2326)
put!(r,port)
client = accept(server)
@test readall(client |> `cat`) == "hello world\n"
close(server)
end
@async begin
sock = connect(2326)
sock = connect(fetch(r))
run(`echo hello world` |> sock)
close(sock)
end
Expand Down Expand Up @@ -123,15 +125,16 @@ yield()

# Test marking of AsyncStream

r = RemoteRef()
@async begin
server = listen(2327)
port, server = listenany(2327)
put!(r, port)
client = accept(server)
write(client, "Hello, world!\n")
write(client, "Goodbye, world...\n")
close(server)
end
sleep(0.1)
sock = connect(2327)
sock = connect(fetch(r))
mark(sock)
@test ismarked(sock)
@test readline(sock) == "Hello, world!\n"
Expand Down

1 comment on commit 7a50a64

@tkelman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this help/fix #9572? Also been seeing OSX Travis time out on the parallel test.

Please sign in to comment.