Skip to content

Commit

Permalink
types.jl: sleep with Date.Period types
Browse files Browse the repository at this point in the history
Supports sleep(Dates.Second(10))
		 sleep(Dates.Milliesecond(10))

Closes #19736
  • Loading branch information
Shade5 committed Jan 1, 2017
1 parent f147aaa commit 3dd6a79
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
5 changes: 5 additions & 0 deletions base/dates/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,8 @@ Base.isless(x::Date,y::Date) = isless(value(x),value(y))
Base.isless(x::DateTime,y::DateTime) = isless(value(x),value(y))
Base.isless(x::TimeType,y::TimeType) = isless(promote(x,y)...)
==(x::TimeType,y::TimeType) = ===(promote(x,y)...)

import Base: sleep,Timer,timedwait
sleep(time::Period) = sleep(toms(time) / 1000)
Timer(time::Period, repeat::Period=Second(0)) = Timer(toms(time) / 1000,toms(repeat) / 1000)
timedwait(testcb::Function, time::Period) = timedwait(testcb, toms(time) / 1000)
4 changes: 2 additions & 2 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ end
@async begin sleep(2.0); put!(rr3, :ok) end

tic()
timedwait(callback, 1.0)
timedwait(callback, Dates.Second(1))
et=toq()
# assuming that 0.5 seconds is a good enough buffer on a typical modern CPU
try
@assert (et >= 1.0) && (et <= 1.5)
@assert !isready(rr3)
catch
warn("timedwait tests delayed. et=$et, isready(rr3)=$(isready(rr3))")
warn("timedwait-Period tests delayed. et=$et, isready(rr3)=$(isready(rr3))")
end
@test isready(rr1)
end
61 changes: 31 additions & 30 deletions test/threads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,37 +347,38 @@ for T in (Int32, Int64, Float32, Float64)
@test varmax[] === T(maximum(1:nloops))
@test varmin[] === T(0)
end

let async = Base.AsyncCondition(), t
c = Condition()
task = schedule(Task(function()
notify(c)
wait(c)
t = Timer(0.06)
wait(t)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
for period in (0.06, Dates.Millisecond(60))
let async = Base.AsyncCondition(), t
c = Condition()
task = schedule(Task(function()
notify(c)
wait(c)
t = Timer(period)
wait(t)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
wait(c)
sleep(period)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
end))
wait(c)
sleep(0.06)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
ccall(:uv_async_send, Void, (Ptr{Void},), async)
end))
wait(c)
notify(c)
delay1 = @elapsed wait(async)
notify(c)
delay2 = @elapsed wait(async)
@test istaskdone(task)
@test delay1 > 0.05
@test delay2 > 0.05
@test isopen(async)
@test !isopen(t)
close(t)
close(async)
@test_throws EOFError wait(async)
@test !isopen(async)
@test_throws EOFError wait(t)
@test_throws EOFError wait(async)
notify(c)
delay1 = @elapsed wait(async)
notify(c)
delay2 = @elapsed wait(async)
@test istaskdone(task)
@test delay1 > 0.05
@test delay2 > 0.05
@test isopen(async)
@test !isopen(t)
close(t)
close(async)
@test_throws EOFError wait(async)
@test !isopen(async)
@test_throws EOFError wait(t)
@test_throws EOFError wait(async)
end
end

# Compare the two ways of checking if threading is enabled.
Expand Down

0 comments on commit 3dd6a79

Please sign in to comment.