itimer 6 → 7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -5
  3. data/lib/itimer.rb +25 -9
  4. data/test/itimer.rb +67 -4
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9303f43abb6d89f16d982ecd699badd4c2fcda54
4
- data.tar.gz: 5a68f96c80393c9fe74f0240a051bcaea3a69c9b
3
+ metadata.gz: 03c7871d15f037b4eca089e3a450f11c9507f36a
4
+ data.tar.gz: e59146e12d5468383501dd68333441ee80db96c3
5
5
  SHA512:
6
- metadata.gz: d03a296115e138ac991c9b23ba4c24fc8bc947253fbd23b337c97e8adddc240634e21fd2973fce8e25c288082cbdb134969c16c2fe2816a2d3ce43f0e0ddc95d
7
- data.tar.gz: d7f583044bc06894247b146f7bdcb36e1ef2934ee85cf714463fd9ab467952670664a77abc23626b7907a55363519569a38e5624100fe8cfa3d0b63868aa58b4
6
+ metadata.gz: 1e0b85378dab98fde8370ed571ba7f42147837ab53937070bd4bf2693179db0a9aac9c2d09004383abb1ef735261e2c1edbfdf43793232316d85a3755d3f36a6
7
+ data.tar.gz: d0073c174fab5aa25cf8879f31ffaa1a03dd4e1289ed75e6a209f7aa7b73bd560639fb02a5a9ede0049a31622d5697f8cd3c9f9d0d85ce24f4ae323d29588933
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Itimer [![Build Status](https://secure.travis-ci.org/nearbuy/ruby-itimer.png?branch=master)]
1
+ # Itimer [![Build Status](https://travis-ci.org/nearbuy/ruby-itimer.png)](https://travis-ci.org/nearbuy/ruby-itimer)
2
2
 
3
3
  Drop-in replacement for stdlib Timeout that uses POSIX interval timers instead of threads. Not as portable but much lighter weight. Also includes low-level wrappers for setitimer and getitimer.
4
4
 
@@ -32,15 +32,15 @@ Signal.trap('VTALRM') { raise Itimer::Timeout }
32
32
  begin
33
33
  # set :real, :virtual and :prof
34
34
  Itimer.set(:virtual, 1)
35
- do\_expensive\_computation()
35
+ do_expensive_computation()
36
36
  rescue Itimer::Timeout
37
37
  puts 'Execution time expired'
38
38
  end
39
39
 
40
40
  Signal.trap('ALRM') { print '.' }
41
41
 
42
- Itimer.set\_interval(:real, 0.5)
43
- do\_expensive\_computation()
42
+ Itimer.set_interval(:real, 0.5)
43
+ do_expensive_computation()
44
44
  puts 'Done'
45
45
  ```
46
46
 
@@ -55,10 +55,11 @@ getitimer(2), setitimer(2)
55
55
  ## Authors
56
56
 
57
57
  * Nate Mueller ([natemueller](https://github.com/natemueller))
58
+ * Peter Sanford ([psanford](https://github.com/psanford))
58
59
 
59
60
  ## License
60
61
 
61
- Copyright (c) 2011 Nearbuy Systems
62
+ Copyright (c) 2013 Nearbuy Systems
62
63
 
63
64
  Permission is hereby granted, free of charge, to any person obtaining
64
65
  a copy of this software and associated documentation files (the
@@ -5,8 +5,13 @@ module Itimer
5
5
  end
6
6
 
7
7
  def self.timeout(seconds, klass=Timeout)
8
- Signal.trap 'ALRM' do
9
- raise klass
8
+ @counter ||= 0
9
+ @counter += 1
10
+
11
+ catch_name = "itimer_#{@counter}"
12
+
13
+ prev_handler = Signal.trap 'ALRM' do
14
+ throw catch_name
10
15
  end
11
16
 
12
17
  prev = get(:real)
@@ -15,17 +20,28 @@ module Itimer
15
20
  seconds = [prev, seconds].min
16
21
  end
17
22
 
18
- begin
23
+ timed_out = true
24
+
25
+ catch catch_name do
19
26
  set(:real, seconds)
20
- ret = yield
21
- ensure
22
- if prev > 0
23
- set(:real, [prev - (Time.now-start), 0.01].max)
24
- else
25
- set(:real, 0)
27
+ begin
28
+ ret = yield
29
+ timed_out = false
30
+ ensure
31
+ if prev > 0
32
+ set(:real, [prev - (Time.now-start), 0.01].max)
33
+ else
34
+ set(:real, 0)
35
+ end
36
+
37
+ Signal.trap('ALRM', prev_handler)
26
38
  end
27
39
  end
28
40
 
41
+ if timed_out
42
+ raise klass
43
+ end
44
+
29
45
  return ret
30
46
  end
31
47
  end
@@ -3,19 +3,35 @@ require 'test/unit'
3
3
  require 'itimer/compat'
4
4
 
5
5
  class ItimerTest < Test::Unit::TestCase
6
+ def teardown
7
+ assert_equal( 0, Itimer.get(:real) )
8
+ assert_equal( 0, Itimer.get(:virtual) )
9
+ assert_equal( 0, Itimer.get(:prof) )
10
+ end
11
+
6
12
  def test_itimer
7
13
  Signal.trap 'ALRM' do
8
14
  raise Exception.new('ALRM')
9
15
  end
10
16
 
11
- Signal.trap 'VTALRM' do
12
- raise Exception.new('VTALRM')
13
- end
14
-
15
17
  Signal.trap 'PROF' do
16
18
  raise Exception.new('PROF')
17
19
  end
18
20
 
21
+ can_trap_vtalrm = true
22
+ begin
23
+ # this fails on ruby >= 2.0.0
24
+ Signal.trap('VTALRM', 'default')
25
+ rescue ArgumentError
26
+ can_trap_vtalrm = false
27
+ end
28
+
29
+ if can_trap_vtalrm
30
+ Signal.trap 'VTALRM' do
31
+ raise Exception.new('VTALRM')
32
+ end
33
+ end
34
+
19
35
  assert_equal( 0, Itimer.get(:real) )
20
36
  assert_equal( 0, Itimer.get(:virtual) )
21
37
  assert_equal( 0, Itimer.get(:prof) )
@@ -67,6 +83,7 @@ class ItimerTest < Test::Unit::TestCase
67
83
  assert_equal( 'ALRM', wait_for_sig.call )
68
84
  assert_in_delta( Time.now-start, 1.2, 0.1 )
69
85
  Itimer.set_interval(:real, 0)
86
+ Itimer.set(:real, 0)
70
87
  end
71
88
 
72
89
  def test_timeout
@@ -101,7 +118,9 @@ class ItimerTest < Test::Unit::TestCase
101
118
  end
102
119
  end
103
120
  assert_in_delta( Time.now-start, 0.1, 0.1 )
121
+ end
104
122
 
123
+ def test_nested_timeouts_inner_rescue
105
124
  start = Time.now
106
125
  timeouts = false
107
126
  assert_raise( Itimer::Timeout ) do
@@ -118,6 +137,9 @@ class ItimerTest < Test::Unit::TestCase
118
137
  end
119
138
  assert( timeouts )
120
139
  assert_in_delta( Time.now-start, 0.25, 0.1 )
140
+ end
141
+
142
+ def test_nested_timeouts_larger_inner
121
143
 
122
144
  start = Time.now
123
145
  timeouts = false
@@ -154,4 +176,45 @@ class ItimerTest < Test::Unit::TestCase
154
176
 
155
177
  assert_equal( 0, Itimer.get(:real) )
156
178
  end
179
+
180
+ def test_exceptions_caught_at_right_level
181
+ inner_exception = false
182
+ outer_exception = false
183
+ begin
184
+ Itimer.timeout(0.25) do
185
+ begin
186
+ sleep 0.5
187
+ rescue Itimer::Timeout
188
+ inner_exception = true
189
+ end
190
+ end
191
+ rescue Itimer::Timeout
192
+ outer_exception = true
193
+ end
194
+
195
+ assert( outer_exception )
196
+ assert( !inner_exception )
197
+ end
198
+
199
+ def test_throwing_exception_in_nested_itimer
200
+ inner_exception = false
201
+ outer_exception = false
202
+ begin
203
+ Itimer.timeout(0.25) do
204
+ begin
205
+ Itimer.timeout(0.1) do
206
+ throw ArgumentError
207
+ end
208
+ rescue ArgumentError
209
+ inner_exception = true
210
+ end
211
+ sleep 0.5
212
+ end
213
+ rescue Itimer::Timeout
214
+ outer_exception = true
215
+ end
216
+
217
+ assert( inner_exception )
218
+ assert( outer_exception )
219
+ end
157
220
  end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itimer
3
3
  version: !ruby/object:Gem::Version
4
- version: '6'
4
+ version: '7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Mueller
8
+ - Peter Sanford
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-10-28 00:00:00.000000000 Z
12
+ date: 2013-11-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake-compiler