From a400c94d7272d29f09b16c059bb9ca3b34bd398f Mon Sep 17 00:00:00 2001 From: kosaki Date: Wed, 5 Dec 2012 15:04:09 +0000 Subject: * lib/timeout.rb (Timeout#timeout): set async_interrupt_timeing(:on_blocking) by default. [Bug #7503] [ruby-core:50524] * test/test_timeout.rb (#test_timeout_blocking): test for the above. * test/test_timeout.rb (test_timeout_immediate): ditto * test/test_timeout.rb (test_timeout_immediate2): ditto. * NEWS: news for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/test_timeout.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'test/test_timeout.rb') diff --git a/test/test_timeout.rb b/test/test_timeout.rb index 57eca3e478..dcd6206737 100644 --- a/test/test_timeout.rb +++ b/test/test_timeout.rb @@ -18,7 +18,7 @@ class TestTimeout < Test::Unit::TestCase } assert_nothing_raised("[ruby-dev:38319]") do Timeout.timeout(1) { - nil while @flag + sleep 0.01 while @flag } end assert !@flag, "[ruby-dev:38319]" @@ -29,4 +29,65 @@ class TestTimeout < Test::Unit::TestCase def (n = Object.new).zero?; false; end assert_raise(TypeError, bug3168) {Timeout.timeout(n) { sleep 0.1 }} end + + def test_timeout_immediate + begin + t = Thread.new { + Timeout.timeout(0.1, immediate: true) { + # loop forever, but can be interrupted + loop {} + } + } + sleep 0.5 + t.raise RuntimeError + assert_raise(Timeout::Error) { + t.join + } + ensure + t.kill if t.alive? + begin + t.join + rescue Exception + end + end + end + + def test_timeout_immediate2 + begin + t = Thread.new { + Timeout.timeout(0.1) { + # loop forever, must not interrupted + loop {} + } + } + sleep 0.5 + t.raise RuntimeError + assert_raise(RuntimeError) { + t.join + } + ensure + t.kill if t.alive? + begin + t.join + rescue Exception + end + end + end + + def test_timeout_blocking + t0 = Time.now + begin + Timeout.timeout(0.1) { + while true do + t1 = Time.now + break if t1 - t0 > 1 + end + sleep 2 + } + rescue Timeout::Error + end + t1 = Time.now + assert (t1 - t0) >= 1 + assert (t1 - t0) < 2 + end end -- cgit v1.2.3