8000 More tests for waiter. (#199) · socketry/async@456e2cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 456e2cc

Browse files
authored
More tests for waiter. (#199)
1 parent 70d2338 commit 456e2cc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/async/waiter.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ def initialize(parent: nil, finished: Async::Condition.new)
1313
@parent = parent
1414
end
1515

16+
# Execute a child task and add it to the waiter.
17+
# @asynchronous Executes the given block concurrently.
1618
def async(parent: (@parent or Task.current), &block)
1719
parent.async do |task|
1820
yield(task)
@@ -22,16 +24,28 @@ def async(parent: (@parent or Task.current), &block)
2224
end
2325
end
2426

27+
# Wait for the first `count` tasks to complete.
28+
# @parameter count [Integer | Nil] The number of tasks to wait for.
29+
# @returns [Array(Async::Task)] If an integer is given, the tasks which have completed.
30+
# @returns [Async::Task] Otherwise, the first task to complete.
2531
def first(count = nil)
26-
while @done.size < count
32+
minimum = count || 1
33+
34+
while @done.size < minimum
2735
@finished.wait
2836
end
2937

3038
return @done.shift(*count)
3139
end
3240

41+
# Wait for the first `count` tasks to complete.
42+
# @parameter count [Integer | Nil] The number of tasks to wait for.
3343
def wait(count = nil)
34-
first(count).map(&:wait)
44+
if count
45+
first(count).map(&:wait)
46+
else
47+
first.wait
48+
end
3549
end
3650
end
3751
end

test/async/waiter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
let(:waiter) {subject.new}
99

10+
it "can wait for the first task to complete" do
11+
waiter.async do
12+
:result
13+
end
14+
15+
expect(waiter.wait).to be == :result
16+
end
17+
1018
it "can wait for a subset of tasks" do
1119
3.times do
1220
waiter.async do

0 commit comments

Comments
 (0)
0