public
Created

tests illustrating problems with Timeout.timeout

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.