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