-
-
Notifications
You must be signed in to change notification settings - Fork 97
Add Async::Waiter #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add Async::Waiter #174
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'barrier' | ||
|
||
module Async | ||
class Waiter < Barrier | ||
def initialize(parent: nil) | ||
super | ||
@finished = Async::Condition.new | ||
@all_tasks = [] | ||
@done = [] | ||
end | ||
|
||
def async(*arguments, parent: (@parent or Task.current), **options) | ||
t = parent.async(*arguments, **options) do |task| | ||
yield(task) | ||
ensure | ||
@done << task | ||
@finished.signal | ||
end | ||
|
||
@tasks << t | ||
@all_tasks << t | ||
t | ||
end | ||
|
||
def wait_for(n = size) | ||
raise ArgumentError, "'n' cannot be greater than size. Given: #{n}, size: #{size}" if n > size | ||
|
||
while @done.size < n | ||
@finished.wait | ||
end | ||
|
||
done = @done.first(n) | ||
[done, @all_tasks - done] | ||
ensure | ||
@tasks.filter!{ |t| [email protected]?(t) } | ||
end | ||
|
||
def wait(n = size) | ||
wait_for(n).first.map(&:wait) | ||
end | ||
end | ||
end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'async/clock' | ||
require 'async/rspec' | ||
require 'async/semaphore' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about zeitwerk? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Autoload is broken on Ruby with the fiber scheduler so it's a no go for now, but maybe later? |
||
|
||
require_relative 'chainable_async_examples' | ||
|
||
RSpec.shared_examples 'barrier' do | ||
describe '#async' do | ||
let(:repeats) {40} | ||
let(:delay) {0.1} | ||
|
||
it 'should wait for all jobs to complete' do | ||
finished = 0 | ||
|
||
repeats.times.map do |i| | ||
subject.async do |task| | ||
task.sleep(delay) | ||
finished += 1 | ||
|
||
# This task is a child task but not part of the barrier. | ||
task.async do | ||
task.sleep(delay*3) | ||
end | ||
end | ||
end | ||
|
||
expect(subject).to_not be_empty | ||
expect(finished).to be < repeats | ||
|
||
duration = Async::Clock.measure{subject.wait} | ||
|
||
expect(duration).to be < (delay * 2 * Q) | ||
expect(finished).to be == repeats | ||
expect(subject).to be_empty | ||
end | ||
end | ||
|
||
describe '#wait' do | ||
it 'should wait for tasks even after exceptions' do | ||
task1 = subject.async do | ||
raise "Boom" | ||
end | ||
|
||
task2 = subject.async do | ||
end | ||
|
||
expect(task1).to be_failed | ||
expect(task2).to be_finished | ||
|
||
expect{subject.wait}.to raise_exception(/Boom/) | ||
|
||
subject.wait until subject.empty? | ||
|
||
expect(subject).to be_empty | ||
end | ||
|
||
it 'waits for tasks in order' do | ||
order = [] | ||
|
||
5.times do |i| | ||
subject.async do | ||
order << i | ||
end | ||
end | ||
|
||
subject.wait | ||
|
||
expect(order).to be == [0, 1, 2, 3, 4] | ||
end | ||
|
||
# It's possible for Barrier#wait to be interrupted with an unexpected exception, and this should not cause the barrier to incorrectly remove that task from the wait list. | ||
it 'waits for tasks with timeouts' do | ||
begin | ||
reactor.with_timeout(0.25) do | ||
5.times do |i| | ||
subject.async do |task| | ||
task.sleep(i/10.0) | ||
end | ||
end | ||
|
||
expect(subject.tasks.size).to be == 5 | ||
subject.wait | ||
end | ||
rescue Async::TimeoutError | ||
# Expected. | ||
ensure | ||
expect(subject.tasks.size).to be == 2 | ||
subject.stop | ||
end | ||
end | ||
end | ||
|
||
describe '#stop' do | ||
it "can stop several tasks" do | ||
task1 = subject.async do |task| | ||
task.sleep(10) | ||
end | ||
|
||
task2 = subject.async do |task| | ||
task.sleep(10) | ||
end | ||
|
||
subject.stop | ||
|
||
expect(task1).to be_stopped | ||
expect(task2).to be_stopped | ||
end | ||
|
||
it "can stop several tasks when waiting on barrier" do | ||
task1 = subject.async do |task| | ||
task.sleep(10) | ||
end | ||
|
||
task2 = subject.async do |task| | ||
task.sleep(10) | ||
end | ||
|
||
task3 = reactor.async do | ||
subject.wait | ||
end | ||
|
||
subject.stop | ||
|
||
task1.wait | ||
task2.wait | ||
|
||
expect(task1).to be_stopped | ||
expect(task2).to be_stopped | ||
|
||
task3.wait | ||
end | ||
|
||
it "several tasks can wait on the same barrier" do | ||
task1 = subject.async do |task| | ||
task.sleep(10) | ||
end | ||
|
||
task2 = reactor.async do |task| | ||
subject.wait | ||
end | ||
|
||
task3 = reactor.async do | ||
subject.wait | ||
end | ||
|
||
subject.stop | ||
|
||
task1.wait | ||
|
||
expect(task1).to be_stopped | ||
|
||
task2.wait | ||
task3.wait | ||
end | ||
end | ||
|
||
context 'with semaphore' do | ||
let(:capacity) {2} | ||
let(:semaphore) {Async::Semaphore.new(capacity)} | ||
let(:repeats) {capacity * 2} | ||
|
||
it 'should execute several tasks and wait using a barrier' do | ||
repeats.times do | ||
subject.async(parent: semaphore) do |task| | ||
task.sleep 0.1 | ||
end | ||
end | ||
|
||
expect(subject.size).to be == repeats | ||
subject.wait | ||
end | ||
end | ||
|
||
it_behaves_like 'chainable async' | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment about Rubocop