Skip to content

Instantly share code, notes, and snippets.

@jjb
Created July 20, 2012 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjb/3149039 to your computer and use it in GitHub Desktop.
Save jjb/3149039 to your computer and use it in GitHub Desktop.
tests illustrating problems with Timeout.timeout
def subject(throws, catches)
$inner_succeeded=nil
$raised_in_inner=nil
$caught_in_inner=nil
$raised_in_outer=nil
$not_raised_in_outer=nil
begin
Timeout.timeout(0.1, throws){
begin
sleep 0.2
rescue catches
$caught_in_inner=true
else
$inner_succeeded=true
ensure
$raised_in_inner=true
end
}
rescue Exception
$raised_in_outer = true
else
$not_raised_in_outer = true
end
sleep 0.3 # an extra .1 for good measure
end
puts "when an exception to raise is not specified and the inner code does not catch Exception"
def test_1
subject(nil, StandardError)
# EXPECTED
assert !$inner_succeeded
assert !$caught_in_inner
assert $raised_in_outer && !$not_raised_in_outer
# WEIRD
assert $raised_in_inner
end
puts "when an exception to raise is not specified and the inner code does catch Exception"
def test_2
subject(nil, Exception)
# EXPECTED
assert !$inner_succeeded
# WEIRD
assert $raised_in_inner
assert $caught_in_inner
# BAD
assert !$raised_in_outer && $not_raised_in_outer
end
puts "when an exception to raise is StandardError and the inner code does not catch Exception"
class MyError < StandardError; end
def test_3
subject(MyError, StandardError)
# EXPECTED
assert !$inner_succeeded
# WEIRD
assert $raised_in_inner
assert $caught_in_inner
# BAD
assert !$raised_in_outer && $not_raised_in_outer
end
puts "when an exception to raise is StandardError and the inner code does catch Exception"
class MyError2 < StandardError; end
def test_4
subject(MyError2, Exception)
# EXPECTED
assert !$inner_succeeded
# WEIRD
assert $raised_in_inner
assert $caught_in_inner
# BAD
assert !$raised_in_outer && $not_raised_in_outer
end
puts "when an exception to raise is Exception and the inner code does not catch Exception"
class MyError3 < Exception; end
def test_5
subject(MyError3, StandardError)
# EXPECTED
assert !$inner_succeeded
assert !$caught_in_inner
assert $raised_in_outer && !$not_raised_in_outer
# WEIRD
assert $raised_in_inner
end
puts "when an exception to raise is Exception and the inner code does catch Exception"
class MyError4 < Exception; end
def test_6
subject(MyError4, Exception)
# EXPECTED
assert !$inner_succeeded
# WEIRD
assert $raised_in_inner
assert $caught_in_inner
# VERY BAD
assert !$raised_in_outer && $not_raised_in_outer
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment