Skip to content

Commit

Permalink
test: fix small race in AsyncCondition test (JuliaLang#39583)
Browse files Browse the repository at this point in the history
There was a small race window where the second t might not have finished
exiting, leading to interference in the Workqueue, resulting in
process_events from running again before the async callback had a chance
to run and dequeue the first event. (This test is probably much stronger
than necessary.)

N.B. This depends on the recent changes in
af353ec for being able to extract the
internal state needed for the sake of the test.

Fixes JuliaLang#39527
  • Loading branch information
vtjnash committed Feb 9, 2021
1 parent df7334a commit 53f44fe
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,36 +390,43 @@ end
t = Timer(0) do t
tc[] += 1
end
cb = first(t.cond.waitq)
Libc.systemsleep(0.005)
@test isopen(t)
Base.process_events()
@test !isopen(t)
@test tc[] == 0
yield()
@test tc[] == 1
@test istaskdone(cb)
end

let tc = Ref(0)
t = Timer(0) do t
tc[] += 1
end
cb = first(t.cond.waitq)
Libc.systemsleep(0.005)
@test isopen(t)
close(t)
@test !isopen(t)
sleep(0.1)
wait(cb)
@test tc[] == 0
@test t.handle === C_NULL
end

let tc = Ref(0)
async = Base.AsyncCondition() do async
tc[] += 1
end
cb = first(async.cond.waitq)
@test isopen(async)
ccall(:uv_async_send, Cvoid, (Ptr{Cvoid},), async)
ccall(:uv_async_send, Cvoid, (Ptr{Cvoid},), async)
@test isempty(Base.Workqueue)
Base.process_events() # schedule event
Sys.iswindows() && Base.process_events() # schedule event (windows?)
@test length(Base.Workqueue) == 1
ccall(:uv_async_send, Cvoid, (Ptr{Cvoid},), async)
@test tc[] == 0
yield() # consume event
Expand All @@ -440,13 +447,16 @@ end
yield() # consume event & then close
@test tc[] == 3
sleep(0.1) # no further events
wait(cb)
@test tc[] == 3
@test async.handle === C_NULL
end

let tc = Ref(0)
async = Base.AsyncCondition() do async
tc[] += 1
end
cb = first(async.cond.waitq)
@test isopen(async)
ccall(:uv_async_send, Cvoid, (Ptr{Cvoid},), async)
Base.process_events() # schedule event
Expand All @@ -457,8 +467,10 @@ end
@test tc[] == 0
yield() # consume event & then close
@test tc[] == 1
sleep(0.1)
sleep(0.1) # no further events
wait(cb)
@test tc[] == 1
@test async.handle === C_NULL
end
end

Expand Down

0 comments on commit 53f44fe

Please sign in to comment.