Skip to content

Commit

Permalink
fix async test listener
Browse files Browse the repository at this point in the history
Avoid a race condition with connecting before listening, and connect to
explicit localhost IP, since the default for `listen` is IPv4, while
getaddrinfo for "localhost" may return ip"::1", resulting in connection
failure.
  • Loading branch information
vtjnash committed Mar 22, 2021
1 parent 45e2c2f commit 1220949
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions test/async.jl
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
using JSON
using Test
using Distributed: RemoteChannel

@isdefined(a) || include("json-samples.jl")

finished_async_tests = RemoteChannel()

using Sockets

@async begin
s = listen(7777)
s = accept(s)

Base.start_reading(s)

@test JSON.parse(s) != nothing # a
@test JSON.parse(s) != nothing # b
validate_c(s) # c
@test JSON.parse(s) != nothing # d
validate_svg_tviewer_menu(s) # svg_tviewer_menu
@test JSON.parse(s) != nothing # gmaps
@test JSON.parse(s) != nothing # colors1
@test JSON.parse(s) != nothing # colors2
@test JSON.parse(s) != nothing # colors3
@test JSON.parse(s) != nothing # twitter
@test JSON.parse(s) != nothing # facebook
validate_flickr(s) # flickr
@test JSON.parse(s) != nothing # youtube
@test JSON.parse(s) != nothing # iphone
@test JSON.parse(s) != nothing # customer
@test JSON.parse(s) != nothing # product
@test JSON.parse(s) != nothing # interop
validate_unicode(s) # unicode
@test JSON.parse(s) != nothing # issue5
@test JSON.parse(s) != nothing # dollars
@test JSON.parse(s) != nothing # brackets

put!(finished_async_tests, nothing)
let serv = listen(7777)
@async let s; try
s = accept(serv)
close(serv)
@test JSON.parse(s) != nothing # a
@test JSON.parse(s) != nothing # b
validate_c(s) # c
@test JSON.parse(s) != nothing # d
validate_svg_tviewer_menu(s) # svg_tviewer_menu
@test JSON.parse(s) != nothing # gmaps
@test JSON.parse(s) != nothing # colors1
@test JSON.parse(s) != nothing # colors2
@test JSON.parse(s) != nothing # colors3
@test JSON.parse(s) != nothing # twitter
@test JSON.parse(s) != nothing # facebook
validate_flickr(s) # flickr
@test JSON.parse(s) != nothing # youtube
@test JSON.parse(s) != nothing # iphone
@test JSON.parse(s) != nothing # customer
@test JSON.parse(s) != nothing # product
@test JSON.parse(s) != nothing # interop
validate_unicode(s) # unicode
@test JSON.parse(s) != nothing # issue5
@test JSON.parse(s) != nothing # dollars
@test JSON.parse(s) != nothing # brackets

put!(finished_async_tests, nothing)
catch ex
@error "async test failure" _exception=ex
finally
@isdefined(s) && close(s)
close(serv)
end; end
end

w = connect("localhost", 7777)
w = connect(Sockets.localhost, 7777)

@test JSON.parse(a) != nothing
write(w, a)
Expand Down

0 comments on commit 1220949

Please sign in to comment.