Skip to content

Commit

Permalink
try_with_timeout: rethrow if wrapped function throws
Browse files Browse the repository at this point in the history
The previous behaviour was to return any exception as res.
  • Loading branch information
gustafsson committed Oct 18, 2022
1 parent 2377beb commit 618dab2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/Exceptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ function try_with_timeout(f, shouldtimeout, delay, iftimeout=() -> nothing)
notify(cond, f())
catch e
@debugv 1 "error executing f in try_with_timeout"
notify(cond, e)
isopen(timer) && notify(cond, e, error = true)
end
# start a timer
timer = Timer(delay; interval=delay / 10) do tm
if shouldtimeout()
@debugv 1 "❗️ Timeout: $delay"
notify(cond, TimeoutError(delay))
iftimeout()
try
if shouldtimeout()
@debugv 1 "❗️ Timeout: $delay"
close(tm)
iftimeout()
notify(cond, TimeoutError(delay), error = true)
end
catch e
@debugv 1 "callback error in try_with_timeout"
close(tm)
notify(cond, e, error = true)
end
end
res = wait(cond)
@debugv 1 "try_with_timeout finished with: $res"
if res isa TimeoutError
# timedout
throw(res)
end
# didn't timeout
close(timer)
if res isa Exception
throw(res)
try
res = wait(cond)
@debugv 1 "try_with_timeout finished with: $res"
res
catch e
@debugv 1 "try_with_timeout failed with: $e"
rethrow()
finally
close(timer)
end
return res
end

abstract type HTTPError <: Exception end
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ include(joinpath(dir, "resources/TestRequest.jl"))
"server.jl",
"async.jl",
"mwe.jl",
"try_with_timeout.jl",
]
file = joinpath(dir, f)
println("Running $file tests...")
Expand Down
41 changes: 41 additions & 0 deletions test/try_with_timeout.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@testset "try_with_timeout $warmup" for warmup in [true, false]
nevertimeout() = false
timeoutafterfirstdelay() = true
throwerrorexception() = throw(ErrorException("error as expected"))
throwargumenterror() = throw(ArgumentError("unexpected error"))

@testset "rethrow exceptions" begin
t = @elapsed begin
@test_throws ErrorException HTTP.try_with_timeout(nevertimeout, 1) do
throwerrorexception()
end
end
if !warmup
@test t < 1
end
end

@testset "rethrow exceptions from shouldtimeout callback" begin
t = @elapsed begin
@test_throws ErrorException HTTP.try_with_timeout(throwerrorexception, 1) do
sleep(5)
throwargumenterror()
end
end
if !warmup
@test 1 < t < 2
end
end

@testset "rethrow exceptions from iftimeout callback" begin
t = @elapsed begin
@test_throws ErrorException HTTP.try_with_timeout(timeoutafterfirstdelay, 1, throwerrorexception) do
sleep(5)
throwargumenterror()
end
end
if !warmup
@test 1 < t < 2
end
end
end

0 comments on commit 618dab2

Please sign in to comment.