Skip to content

Commit 861ecf0

Browse files
committed
types.jl: sleep with Date.Period types
Supports sleep(Dates.Second(10)) sleep(Dates.Milliesecond(10)) Closes JuliaLang#19736
1 parent 1539061 commit 861ecf0

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

base/dates/types.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,8 @@ Base.isless(x::Date,y::Date) = isless(value(x),value(y))
241241
Base.isless(x::DateTime,y::DateTime) = isless(value(x),value(y))
242242
Base.isless(x::TimeType,y::TimeType) = isless(promote(x,y)...)
243243
==(x::TimeType,y::TimeType) = ===(promote(x,y)...)
244+
245+
import Base: sleep,Timer,timedwait
246+
sleep(time::Period) = sleep(toms(time) / 1000)
247+
Timer(time::Period, repeat::Period=Second(0)) = Timer(toms(time) / 1000,toms(repeat) / 1000)
248+
timedwait(testcb::Function, time::Period) = timedwait(testcb, toms(time) / 1000)

test/channels.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,30 @@ end
103103
end
104104
@test isready(rr1)
105105
end
106+
107+
@sync begin
108+
rr1 = Channel(1)
109+
rr2 = Channel(1)
110+
rr3 = Channel(1)
111+
112+
callback2() = all(map(isready, [rr1, rr2, rr3]))
113+
# precompile functions which will be tested for execution time
114+
@test !callback2()
115+
@test timedwait(callback2, 0.0) === :timed_out
116+
117+
@async begin sleep(0.5); put!(rr1, :ok) end
118+
@async begin sleep(1.0); put!(rr2, :ok) end
119+
@async begin sleep(2.0); put!(rr3, :ok) end
120+
121+
tic()
122+
timedwait(callback2, Dates.Second(1))
123+
et=toq()
124+
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU
125+
try
126+
@assert (et >= 1.0) && (et <= 1.5)
127+
@assert !isready(rr3)
128+
catch
129+
warn("timedwait-Period tests delayed. et=$et, isready(rr3)=$(isready(rr3))")
130+
end
131+
@test isready(rr1)
132+
end

test/threads.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,38 @@ let async = Base.AsyncCondition(), t
380380
@test_throws EOFError wait(async)
381381
end
382382

383+
let async = Base.AsyncCondition(), t
384+
c = Condition()
385+
task = schedule(Task(function()
386+
notify(c)
387+
wait(c)
388+
t = Timer(Dates.Millisecond(60))
389+
wait(t)
390+
ccall(:uv_async_send, Void, (Ptr{Void},), async)
391+
ccall(:uv_async_send, Void, (Ptr{Void},), async)
392+
wait(c)
393+
sleep(Dates.Millisecond(60))
394+
ccall(:uv_async_send, Void, (Ptr{Void},), async)
395+
ccall(:uv_async_send, Void, (Ptr{Void},), async)
396+
end))
397+
wait(c)
398+
notify(c)
399+
delay1 = @elapsed wait(async)
400+
notify(c)
401+
delay2 = @elapsed wait(async)
402+
@test istaskdone(task)
403+
@test delay1 > 0.05
404+
@test delay2 > 0.05
405+
@test isopen(async)
406+
@test !isopen(t)
407+
close(t)
408+
close(async)
409+
@test_throws EOFError wait(async)
410+
@test !isopen(async)
411+
@test_throws EOFError wait(t)
412+
@test_throws EOFError wait(async)
413+
end
414+
383415
# Compare the two ways of checking if threading is enabled.
384416
# `jl_tls_states` should only be defined on non-threading build.
385417
if ccall(:jl_threading_enabled, Cint, ()) == 0

0 commit comments

Comments
 (0)