Skip to content

Commit 1220949

Browse files
committed
fix async test listener
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.
1 parent 45e2c2f commit 1220949

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

test/async.jl

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
1+
using JSON
2+
using Test
3+
using Distributed: RemoteChannel
4+
5+
@isdefined(a) || include("json-samples.jl")
6+
17
finished_async_tests = RemoteChannel()
28

39
using Sockets
410

5-
@async begin
6-
s = listen(7777)
7-
s = accept(s)
8-
9-
Base.start_reading(s)
10-
11-
@test JSON.parse(s) != nothing # a
12-
@test JSON.parse(s) != nothing # b
13-
validate_c(s) # c
14-
@test JSON.parse(s) != nothing # d
15-
validate_svg_tviewer_menu(s) # svg_tviewer_menu
16-
@test JSON.parse(s) != nothing # gmaps
17-
@test JSON.parse(s) != nothing # colors1
18-
@test JSON.parse(s) != nothing # colors2
19-
@test JSON.parse(s) != nothing # colors3
20-
@test JSON.parse(s) != nothing # twitter
21-
@test JSON.parse(s) != nothing # facebook
22-
validate_flickr(s) # flickr
23-
@test JSON.parse(s) != nothing # youtube
24-
@test JSON.parse(s) != nothing # iphone
25-
@test JSON.parse(s) != nothing # customer
26-
@test JSON.parse(s) != nothing # product
27-
@test JSON.parse(s) != nothing # interop
28-
validate_unicode(s) # unicode
29-
@test JSON.parse(s) != nothing # issue5
30-
@test JSON.parse(s) != nothing # dollars
31-
@test JSON.parse(s) != nothing # brackets
32-
33-
put!(finished_async_tests, nothing)
11+
let serv = listen(7777)
12+
@async let s; try
13+
s = accept(serv)
14+
close(serv)
15+
@test JSON.parse(s) != nothing # a
16+
@test JSON.parse(s) != nothing # b
17+
validate_c(s) # c
18+
@test JSON.parse(s) != nothing # d
19+
validate_svg_tviewer_menu(s) # svg_tviewer_menu
20+
@test JSON.parse(s) != nothing # gmaps
21+
@test JSON.parse(s) != nothing # colors1
22+
@test JSON.parse(s) != nothing # colors2
23+
@test JSON.parse(s) != nothing # colors3
24+
@test JSON.parse(s) != nothing # twitter
25+
@test JSON.parse(s) != nothing # facebook
26+
validate_flickr(s) # flickr
27+
@test JSON.parse(s) != nothing # youtube
28+
@test JSON.parse(s) != nothing # iphone
29+
@test JSON.parse(s) != nothing # customer
30+
@test JSON.parse(s) != nothing # product
31+
@test JSON.parse(s) != nothing # interop
32+
validate_unicode(s) # unicode
33+
@test JSON.parse(s) != nothing # issue5
34+
@test JSON.parse(s) != nothing # dollars
35+
@test JSON.parse(s) != nothing # brackets
36+
37+
put!(finished_async_tests, nothing)
38+
catch ex
39+
@error "async test failure" _exception=ex
40+
finally
41+
@isdefined(s) && close(s)
42+
close(serv)
43+
end; end
3444
end
3545

36-
w = connect("localhost", 7777)
46+
w = connect(Sockets.localhost, 7777)
3747

3848
@test JSON.parse(a) != nothing
3949
write(w, a)

0 commit comments

Comments
 (0)