From 4be146b4f0eefd94664310c69c141f4c22e00331 Mon Sep 17 00:00:00 2001 From: Asmodeus Date: Mon, 2 Jan 2017 08:46:57 +0530 Subject: [PATCH] types.jl: sleep with Date.Period types Supports sleep(Dates.Second(10)) sleep(Dates.Milliesecond(10)) Closes #19736 --- base/dates/types.jl | 5 ++++ test/channels.jl | 2 +- test/threads.jl | 61 +++++++++++++++++++++++---------------------- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/base/dates/types.jl b/base/dates/types.jl index 02adbbcd73423..458e405c6bc0e 100644 --- a/base/dates/types.jl +++ b/base/dates/types.jl @@ -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) diff --git a/test/channels.jl b/test/channels.jl index 471b5ce313a85..27897215b401a 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -92,7 +92,7 @@ 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 diff --git a/test/threads.jl b/test/threads.jl index bebec566cacdb..f95d5a6b2d0c4 100644 --- a/test/threads.jl +++ b/test/threads.jl @@ -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.