Permalink
Browse files

Always clear the timer, even if user code raises exceptions

  • Loading branch information...
1 parent 7112542 commit 10445b551f366bdf243bb71b8c1e058ac9f565dd @natemueller natemueller committed Jan 22, 2013
Showing with 18 additions and 5 deletions.
  1. +1 −1 Gemfile.lock
  2. +1 −1 itimer.gemspec
  3. +6 −3 lib/itimer.rb
  4. +10 −0 test/itimer.rb
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- itimer (1)
+ itimer (4)
GEM
remote: http://rubygems.org/
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'itimer'
- s.version = '3'
+ s.version = '4'
s.authors = ['Nate Mueller']
s.date = '2012-08-10'
View
@@ -9,9 +9,12 @@ def self.timeout(seconds, klass=Timeout)
raise klass
end
- set(:real, seconds)
- ret = yield
- set(:real, 0)
+ begin
+ set(:real, seconds)
+ ret = yield
+ ensure
+ set(:real, 0)
+ end
return ret
end
View
@@ -90,4 +90,14 @@ def test_compat
end
assert_in_delta( Time.now-start, 0.25, 0.1 )
end
+
+ def test_exception
+ start = Time.now
+ assert_raises( ArgumentError ) do
+ Itimer.timeout(1) { raise ArgumentError }
+ end
+ assert_in_delta( Time.now-start, 0, 0.1 )
+
+ assert_equal( 0, Itimer.get(:real) )
+ end
end

0 comments on commit 10445b5

Please sign in to comment.