Skip to content

Commit 3b10496

Browse files
committed
Better tests for Async mixin.
1 parent 7e89414 commit 3b10496

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

spec/concurrent/async_spec.rb

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ module Concurrent
44

55
describe Async do
66

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
913
include Concurrent::Async
14+
attr_accessor :accessor
1015
def echo(msg)
1116
msg
1217
end
@@ -19,9 +24,14 @@ def boom(ex = StandardError.new)
1924
def wait(seconds)
2025
sleep(seconds)
2126
end
22-
}.new
27+
def with_block
28+
yield
29+
end
30+
end
2331
end
2432

33+
subject { async_class.new }
34+
2535
context '#validate_argc' do
2636

2737
subject do
@@ -135,6 +145,21 @@ def many(*args, &block) nil; end
135145
val.should be_rejected
136146
end
137147

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+
138163
it 'is aliased as #future' do
139164
val = subject.future.wait(5)
140165
val.should be_a Concurrent::Future
@@ -154,10 +179,6 @@ def many(*args, &block) nil; end
154179
expect { subject.async.bogus }.to raise_error(NameError)
155180
expect { subject.async.method(:bogus) }.to raise_error(NameError)
156181
end
157-
158-
it 'uses the same mutex as #await' do
159-
subject.await.mutex.should eq subject.async.mutex
160-
end
161182
end
162183
end
163184

@@ -206,6 +227,18 @@ def many(*args, &block) nil; end
206227
val.should be_rejected
207228
end
208229

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+
209242
it 'is aliased as #defer' do
210243
val = subject.defer.echo(5)
211244
val.should be_a Concurrent::IVar
@@ -224,10 +257,26 @@ def many(*args, &block) nil; end
224257
expect { subject.await.bogus }.to raise_error(NameError)
225258
expect { subject.await.method(:bogus) }.to raise_error(NameError)
226259
end
260+
end
261+
end
227262

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]
231280
end
232281
end
233282
end

0 commit comments

Comments
 (0)