@@ -4,9 +4,14 @@ module Concurrent
4
4
5
5
describe Async do
6
6
7
- subject do
8
- Class . new {
7
+ before ( :each ) do
8
+ Concurrent ::Future . thread_pool = Concurrent ::NullThreadPool . new
9
+ end
10
+
11
+ let ( :async_class ) do
12
+ Class . new do
9
13
include Concurrent ::Async
14
+ attr_accessor :accessor
10
15
def echo ( msg )
11
16
msg
12
17
end
@@ -19,9 +24,14 @@ def boom(ex = StandardError.new)
19
24
def wait ( seconds )
20
25
sleep ( seconds )
21
26
end
22
- } . new
27
+ def with_block
28
+ yield
29
+ end
30
+ end
23
31
end
24
32
33
+ subject { async_class . new }
34
+
25
35
context '#validate_argc' do
26
36
27
37
subject do
@@ -135,6 +145,21 @@ def many(*args, &block) nil; end
135
145
val . should be_rejected
136
146
end
137
147
148
+ it 'supports attribute accessors' do
149
+ subject . async . accessor = :foo
150
+ sleep ( 0.1 )
151
+ val = subject . async . accessor
152
+ sleep ( 0.1 )
153
+ val . value . should eq :foo
154
+ subject . accessor . should eq :foo
155
+ end
156
+
157
+ it 'supports methods with blocks' do
158
+ val = subject . async . with_block { :foo }
159
+ sleep ( 0.1 )
160
+ val . value . should eq :foo
161
+ end
162
+
138
163
it 'is aliased as #future' do
139
164
val = subject . future . wait ( 5 )
140
165
val . should be_a Concurrent ::Future
@@ -154,10 +179,6 @@ def many(*args, &block) nil; end
154
179
expect { subject . async . bogus } . to raise_error ( NameError )
155
180
expect { subject . async . method ( :bogus ) } . to raise_error ( NameError )
156
181
end
157
-
158
- it 'uses the same mutex as #await' do
159
- subject . await . mutex . should eq subject . async . mutex
160
- end
161
182
end
162
183
end
163
184
@@ -206,6 +227,18 @@ def many(*args, &block) nil; end
206
227
val . should be_rejected
207
228
end
208
229
230
+ it 'supports attribute accessors' do
231
+ subject . await . accessor = :foo
232
+ val = subject . await . accessor
233
+ val . value . should eq :foo
234
+ subject . accessor . should eq :foo
235
+ end
236
+
237
+ it 'supports methods with blocks' do
238
+ val = subject . await . with_block { :foo }
239
+ val . value . should eq :foo
240
+ end
241
+
209
242
it 'is aliased as #defer' do
210
243
val = subject . defer . echo ( 5 )
211
244
val . should be_a Concurrent ::IVar
@@ -224,10 +257,26 @@ def many(*args, &block) nil; end
224
257
expect { subject . await . bogus } . to raise_error ( NameError )
225
258
expect { subject . await . method ( :bogus ) } . to raise_error ( NameError )
226
259
end
260
+ end
261
+ end
227
262
228
- it 'uses the same mutex as #async' do
229
- subject . await . mutex . should eq subject . async . mutex
230
- end
263
+ context 'locking' do
264
+
265
+ it 'uses the same mutex for both #async and #await' do
266
+ object = Class . new {
267
+ include Concurrent ::Async
268
+ attr_reader :bucket
269
+ def gather ( seconds , first , *rest )
270
+ sleep ( seconds )
271
+ ( @bucket ||= [ ] ) . concat ( [ first ] )
272
+ @bucket . concat ( rest )
273
+ end
274
+ } . new
275
+
276
+ object . async . gather ( 0.5 , :a , :b )
277
+ sleep ( 0.1 )
278
+ object . await . gather ( 0 , :c , :d )
279
+ object . bucket . should eq [ :a , :b , :c , :d ]
231
280
end
232
281
end
233
282
end
0 commit comments