frugal_timeout 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'frugal_timeout'
3
- s.version = '0.0.10'
4
- s.date = '2014-01-02'
3
+ s.version = '0.0.11'
4
+ s.date = '2014-01-03'
5
5
  s.summary = 'Timeout.timeout replacement'
6
6
  s.description = 'Timeout.timeout replacement that uses only 1 thread'
7
7
  s.authors = ['Dmitry Maksyoma']
@@ -77,7 +77,10 @@ module FrugalTimeout
77
77
 
78
78
  def enforceTimeout
79
79
  @@mutex.synchronize {
80
- @thread.raise @klass, 'execution expired' unless @defused
80
+ return if @defused
81
+
82
+ @thread.raise @klass, 'execution expired'
83
+ true
81
84
  }
82
85
  end
83
86
  end
@@ -97,6 +100,10 @@ module FrugalTimeout
97
100
  end
98
101
  private :defuse_thread!
99
102
 
103
+ def onEnforce &b
104
+ @onEnforce = b
105
+ end
106
+
100
107
  def onNewNearestRequest &b
101
108
  @onNewNearestRequest = b
102
109
  end
@@ -104,14 +111,17 @@ module FrugalTimeout
104
111
  # Purge and enforce expired timeouts. Only enforce once for each thread,
105
112
  # even if multiple timeouts for that thread expire at once.
106
113
  def purgeExpired
107
- expiredRequests, filter, now = nil, {}, MonotonicTime.now
114
+ filter, now = {}, MonotonicTime.now
108
115
  @requests.synchronize {
116
+ @onEnforce.call if @onEnforce
117
+
109
118
  @requests.reject_and_get! { |r| r.at <= now }.each { |r|
110
119
  next if filter[r.thread]
111
120
 
112
- r.enforceTimeout
113
- defuse_thread! r.thread
114
- filter[r.thread] = true
121
+ if r.enforceTimeout
122
+ defuse_thread! r.thread
123
+ filter[r.thread] = true
124
+ end
115
125
  }
116
126
 
117
127
  # It's necessary to call onNewNearestRequest inside synchronize as other
@@ -292,6 +302,10 @@ module FrugalTimeout
292
302
  end'
293
303
  end
294
304
 
305
+ def self.on_enforce &b #:nodoc:
306
+ @requestQueue.onEnforce &b
307
+ end
308
+
295
309
  def self.on_ensure &b #:nodoc:
296
310
  @onEnsure = b
297
311
  end
@@ -122,6 +122,17 @@ describe FrugalTimeout do
122
122
  }.to raise_error Timeout::Error
123
123
  end
124
124
 
125
+ it 'raises exception if inner timeout is defused before it is enforced' do
126
+ expect {
127
+ timeout(0.05, IOError) {
128
+ FrugalTimeout.on_enforce { sleep 0.02 }
129
+ timeout(0.01) { }
130
+ FrugalTimeout.on_enforce
131
+ sleep 0.06
132
+ }
133
+ }.to raise_error IOError
134
+ end
135
+
125
136
  context "doesn't raise second exception in the same thread" do
126
137
  before :all do
127
138
  FrugalTimeout.on_ensure { sleep 0.02 }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frugal_timeout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-02 00:00:00.000000000 Z
12
+ date: 2014-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec