concurrent-ruby 0.1.0 → 0.1.1.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -21
- data/README.md +279 -224
- data/lib/concurrent.rb +27 -20
- data/lib/concurrent/agent.rb +106 -130
- data/lib/concurrent/cached_thread_pool.rb +130 -122
- data/lib/concurrent/defer.rb +67 -69
- data/lib/concurrent/drb_async_demux.rb +72 -0
- data/lib/concurrent/event.rb +60 -60
- data/lib/concurrent/event_machine_defer_proxy.rb +23 -23
- data/lib/concurrent/executor.rb +87 -0
- data/lib/concurrent/fixed_thread_pool.rb +89 -89
- data/lib/concurrent/functions.rb +120 -0
- data/lib/concurrent/future.rb +52 -42
- data/lib/concurrent/global_thread_pool.rb +3 -3
- data/lib/concurrent/goroutine.rb +29 -25
- data/lib/concurrent/obligation.rb +67 -121
- data/lib/concurrent/promise.rb +172 -194
- data/lib/concurrent/reactor.rb +162 -0
- data/lib/concurrent/smart_mutex.rb +66 -0
- data/lib/concurrent/tcp_sync_demux.rb +96 -0
- data/lib/concurrent/thread_pool.rb +65 -61
- data/lib/concurrent/utilities.rb +34 -0
- data/lib/concurrent/version.rb +3 -3
- data/lib/concurrent_ruby.rb +1 -1
- data/md/agent.md +123 -123
- data/md/defer.md +174 -174
- data/md/event.md +32 -32
- data/md/executor.md +176 -0
- data/md/future.md +83 -83
- data/md/goroutine.md +52 -52
- data/md/obligation.md +32 -32
- data/md/promise.md +225 -225
- data/md/thread_pool.md +197 -197
- data/spec/concurrent/agent_spec.rb +376 -405
- data/spec/concurrent/cached_thread_pool_spec.rb +112 -112
- data/spec/concurrent/defer_spec.rb +209 -199
- data/spec/concurrent/event_machine_defer_proxy_spec.rb +250 -246
- data/spec/concurrent/event_spec.rb +134 -134
- data/spec/concurrent/executor_spec.rb +146 -0
- data/spec/concurrent/fixed_thread_pool_spec.rb +84 -84
- data/spec/concurrent/functions_spec.rb +57 -0
- data/spec/concurrent/future_spec.rb +125 -115
- data/spec/concurrent/goroutine_spec.rb +67 -52
- data/spec/concurrent/obligation_shared.rb +121 -121
- data/spec/concurrent/promise_spec.rb +299 -310
- data/spec/concurrent/smart_mutex_spec.rb +234 -0
- data/spec/concurrent/thread_pool_shared.rb +209 -209
- data/spec/concurrent/utilities_spec.rb +74 -0
- data/spec/spec_helper.rb +21 -19
- metadata +38 -14
- checksums.yaml +0 -7
@@ -1,310 +1,299 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative 'obligation_shared'
|
3
|
-
|
4
|
-
module Concurrent
|
5
|
-
|
6
|
-
describe Promise do
|
7
|
-
|
8
|
-
let!(:fulfilled_value) { 10 }
|
9
|
-
let!(:rejected_reason) { StandardError.new('mojo jojo') }
|
10
|
-
|
11
|
-
let(:pending_subject) do
|
12
|
-
Promise.new{ sleep(1) }
|
13
|
-
end
|
14
|
-
|
15
|
-
let(:fulfilled_subject) do
|
16
|
-
Promise.new{ fulfilled_value }.tap(){ sleep(0.1) }
|
17
|
-
end
|
18
|
-
|
19
|
-
let(:rejected_subject) do
|
20
|
-
Promise.new{ raise rejected_reason }.
|
21
|
-
rescue{ nil }.tap(){ sleep(0.1) }
|
22
|
-
end
|
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
|
-
p1
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
p1
|
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
|
-
p1
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
p.
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
p.
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
p.
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
@expected
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
@expected
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
@expected
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
@expected
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
it 'aliases #
|
281
|
-
fulfilled_subject.should
|
282
|
-
end
|
283
|
-
|
284
|
-
it 'aliases #
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
@expected
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
end
|
301
|
-
|
302
|
-
it 'aliases Kernel#promise for Promise.new' do
|
303
|
-
promise().should be_a(Promise)
|
304
|
-
promise(){ nil }.should be_a(Promise)
|
305
|
-
promise(1, 2, 3).should be_a(Promise)
|
306
|
-
promise(1, 2, 3){ nil }.should be_a(Promise)
|
307
|
-
end
|
308
|
-
end
|
309
|
-
end
|
310
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'obligation_shared'
|
3
|
+
|
4
|
+
module Concurrent
|
5
|
+
|
6
|
+
describe Promise do
|
7
|
+
|
8
|
+
let!(:fulfilled_value) { 10 }
|
9
|
+
let!(:rejected_reason) { StandardError.new('mojo jojo') }
|
10
|
+
|
11
|
+
let(:pending_subject) do
|
12
|
+
Promise.new{ sleep(1) }
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:fulfilled_subject) do
|
16
|
+
Promise.new{ fulfilled_value }.tap(){ sleep(0.1) }
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:rejected_subject) do
|
20
|
+
Promise.new{ raise rejected_reason }.
|
21
|
+
rescue{ nil }.tap(){ sleep(0.1) }
|
22
|
+
end
|
23
|
+
|
24
|
+
it_should_behave_like Obligation
|
25
|
+
|
26
|
+
context 'behavior' do
|
27
|
+
|
28
|
+
it 'implements :promise behavior' do
|
29
|
+
lambda {
|
30
|
+
Promise.new{ nil }
|
31
|
+
}.should_not raise_error
|
32
|
+
|
33
|
+
Promise.new{ nil }.behaves_as?(:promise).should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'implements :future behavior' do
|
37
|
+
lambda {
|
38
|
+
Promise.new{ nil }
|
39
|
+
}.should_not raise_error
|
40
|
+
|
41
|
+
Promise.new{ nil }.behaves_as?(:future).should be_true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context '#then' do
|
46
|
+
|
47
|
+
it 'returns a new Promise when :pending' do
|
48
|
+
p1 = pending_subject
|
49
|
+
p2 = p1.then{}
|
50
|
+
p2.should be_a(Promise)
|
51
|
+
p1.should_not eq p2
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns a new Promise when :fulfilled' do
|
55
|
+
p1 = fulfilled_subject
|
56
|
+
p2 = p1.then{}
|
57
|
+
p2.should be_a(Promise)
|
58
|
+
p1.should_not eq p2
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns a new Promise when :rejected' do
|
62
|
+
p1 = rejected_subject
|
63
|
+
p2 = p1.then{}
|
64
|
+
p2.should be_a(Promise)
|
65
|
+
p1.should_not eq p2
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'immediately rejects new promises when self has been rejected' do
|
69
|
+
p = rejected_subject
|
70
|
+
p.then.should be_rejected
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'accepts a nil block' do
|
74
|
+
lambda {
|
75
|
+
pending_subject.then
|
76
|
+
}.should_not raise_error
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'can be called more than once' do
|
80
|
+
p = pending_subject
|
81
|
+
p1 = p.then{}
|
82
|
+
p2 = p.then{}
|
83
|
+
p1.object_id.should_not eq p2.object_id
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context '#rescue' do
|
88
|
+
|
89
|
+
it 'returns self when a block is given' do
|
90
|
+
p1 = pending_subject
|
91
|
+
p2 = p1.rescue{}
|
92
|
+
p1.object_id.should eq p2.object_id
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'returns self when no block is given' do
|
96
|
+
p1 = pending_subject
|
97
|
+
p2 = p1.rescue
|
98
|
+
p1.object_id.should eq p2.object_id
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'accepts an exception class as the first parameter' do
|
102
|
+
lambda {
|
103
|
+
pending_subject.rescue(StandardError){}
|
104
|
+
}.should_not raise_error
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'fulfillment' do
|
109
|
+
|
110
|
+
it 'passes all arguments to the first promise in the chain' do
|
111
|
+
@a = @b = @c = nil
|
112
|
+
p = Promise.new(1, 2, 3) do |a, b, c|
|
113
|
+
@a, @b, @c = a, b, c
|
114
|
+
end
|
115
|
+
sleep(0.1)
|
116
|
+
[@a, @b, @c].should eq [1, 2, 3]
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'passes the result of each block to all its children' do
|
120
|
+
@expected = nil
|
121
|
+
Promise.new(10){|a| a * 2 }.then{|result| @expected = result}
|
122
|
+
sleep(0.1)
|
123
|
+
@expected.should eq 20
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'sets the promise value to the result if its block' do
|
127
|
+
p = Promise.new(10){|a| a * 2 }.then{|result| result * 2}
|
128
|
+
sleep(0.1)
|
129
|
+
p.value.should eq 40
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'sets the promise state to :fulfilled if the block completes' do
|
133
|
+
p = Promise.new(10){|a| a * 2 }.then{|result| result * 2}
|
134
|
+
sleep(0.1)
|
135
|
+
p.should be_fulfilled
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'passes the last result through when a promise has no block' do
|
139
|
+
@expected = nil
|
140
|
+
Promise.new(10){|a| a * 2 }.then.then{|result| @expected = result}
|
141
|
+
sleep(0.1)
|
142
|
+
@expected.should eq 20
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'rejection' do
|
147
|
+
|
148
|
+
it 'sets the promise reason the error object on exception' do
|
149
|
+
p = Promise.new{ raise StandardError.new('Boom!') }
|
150
|
+
sleep(0.1)
|
151
|
+
p.reason.should be_a(Exception)
|
152
|
+
p.reason.should.to_s =~ /Boom!/
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'sets the promise state to :rejected on exception' do
|
156
|
+
p = Promise.new{ raise StandardError.new('Boom!') }
|
157
|
+
sleep(0.1)
|
158
|
+
p.should be_rejected
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'recursively rejects all children' do
|
162
|
+
p = Promise.new{ Thread.pass; raise StandardError.new('Boom!') }
|
163
|
+
promises = 10.times.collect{ p.then{ true } }
|
164
|
+
sleep(0.1)
|
165
|
+
|
166
|
+
10.times.each{|i| promises[i].should be_rejected }
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'skips processing rejected promises' do
|
170
|
+
p = Promise.new{ raise StandardError.new('Boom!') }
|
171
|
+
promises = 3.times.collect{ p.then{ true } }
|
172
|
+
sleep(0.1)
|
173
|
+
promises.each{|p| p.value.should_not be_true }
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'calls the first exception block with a matching class' do
|
177
|
+
@expected = nil
|
178
|
+
Promise.new{ raise StandardError }.
|
179
|
+
rescue(StandardError){|ex| @expected = 1 }.
|
180
|
+
rescue(StandardError){|ex| @expected = 2 }.
|
181
|
+
rescue(StandardError){|ex| @expected = 3 }
|
182
|
+
sleep(0.1)
|
183
|
+
@expected.should eq 1
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'matches all with a rescue with no class given' do
|
187
|
+
@expected = nil
|
188
|
+
Promise.new{ raise NoMethodError }.
|
189
|
+
rescue(LoadError){|ex| @expected = 1 }.
|
190
|
+
rescue{|ex| @expected = 2 }.
|
191
|
+
rescue(StandardError){|ex| @expected = 3 }
|
192
|
+
sleep(0.1)
|
193
|
+
@expected.should eq 2
|
194
|
+
end
|
195
|
+
|
196
|
+
it 'searches associated rescue handlers in order' do
|
197
|
+
@expected = nil
|
198
|
+
Promise.new{ raise ArgumentError }.
|
199
|
+
rescue(ArgumentError){|ex| @expected = 1 }.
|
200
|
+
rescue(LoadError){|ex| @expected = 2 }.
|
201
|
+
rescue(Exception){|ex| @expected = 3 }
|
202
|
+
sleep(0.1)
|
203
|
+
@expected.should eq 1
|
204
|
+
|
205
|
+
@expected = nil
|
206
|
+
Promise.new{ raise LoadError }.
|
207
|
+
rescue(ArgumentError){|ex| @expected = 1 }.
|
208
|
+
rescue(LoadError){|ex| @expected = 2 }.
|
209
|
+
rescue(Exception){|ex| @expected = 3 }
|
210
|
+
sleep(0.1)
|
211
|
+
@expected.should eq 2
|
212
|
+
|
213
|
+
@expected = nil
|
214
|
+
Promise.new{ raise StandardError }.
|
215
|
+
rescue(ArgumentError){|ex| @expected = 1 }.
|
216
|
+
rescue(LoadError){|ex| @expected = 2 }.
|
217
|
+
rescue(Exception){|ex| @expected = 3 }
|
218
|
+
sleep(0.1)
|
219
|
+
@expected.should eq 3
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'passes the exception object to the matched block' do
|
223
|
+
@expected = nil
|
224
|
+
Promise.new{ raise StandardError }.
|
225
|
+
rescue(ArgumentError){|ex| @expected = ex }.
|
226
|
+
rescue(LoadError){|ex| @expected = ex }.
|
227
|
+
rescue(Exception){|ex| @expected = ex }
|
228
|
+
sleep(0.1)
|
229
|
+
@expected.should be_a(StandardError)
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'ignores rescuers without a block' do
|
233
|
+
@expected = nil
|
234
|
+
Promise.new{ raise StandardError }.
|
235
|
+
rescue(StandardError).
|
236
|
+
rescue(StandardError){|ex| @expected = ex }.
|
237
|
+
rescue(Exception){|ex| @expected = ex }
|
238
|
+
sleep(0.1)
|
239
|
+
@expected.should be_a(StandardError)
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'supresses the exception if no rescue matches' do
|
243
|
+
lambda {
|
244
|
+
Promise.new{ raise StandardError }.
|
245
|
+
rescue(ArgumentError){|ex| @expected = ex }.
|
246
|
+
rescue(StandardError){|ex| @expected = ex }.
|
247
|
+
rescue(Exception){|ex| @expected = ex }
|
248
|
+
sleep(0.1)
|
249
|
+
}.should_not raise_error
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'supresses exceptions thrown from rescue handlers' do
|
253
|
+
lambda {
|
254
|
+
Promise.new{ raise ArgumentError }.
|
255
|
+
rescue(Exception){ raise StandardError }
|
256
|
+
sleep(0.1)
|
257
|
+
}.should_not raise_error
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'calls matching rescue handlers on all children' do
|
261
|
+
@expected = []
|
262
|
+
Promise.new{ Thread.pass; raise StandardError }.
|
263
|
+
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }.
|
264
|
+
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }.
|
265
|
+
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }.
|
266
|
+
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }.
|
267
|
+
then{ sleep(0.1) }.rescue{ @expected << 'Boom!' }
|
268
|
+
sleep(0.1)
|
269
|
+
|
270
|
+
@expected.length.should eq 5
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
context 'aliases' do
|
275
|
+
|
276
|
+
it 'aliases #realized? for #fulfilled?' do
|
277
|
+
fulfilled_subject.should be_realized
|
278
|
+
end
|
279
|
+
|
280
|
+
it 'aliases #deref for #value' do
|
281
|
+
fulfilled_subject.deref.should eq fulfilled_value
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'aliases #catch for #rescue' do
|
285
|
+
@expected = nil
|
286
|
+
Promise.new{ raise StandardError }.catch{ @expected = true }
|
287
|
+
sleep(0.1)
|
288
|
+
@expected.should be_true
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'aliases #on_error for #rescue' do
|
292
|
+
@expected = nil
|
293
|
+
Promise.new{ raise StandardError }.on_error{ @expected = true }
|
294
|
+
sleep(0.1)
|
295
|
+
@expected.should be_true
|
296
|
+
end
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|