From 89a1dbe5dbcdd73b88c8b3d6bae6d1d2af4be450 Mon Sep 17 00:00:00 2001 From: Bryan Powell Date: Mon, 11 Jan 2021 10:13:18 -0800 Subject: [PATCH 1/2] Update editorconfig to not automatically trim whitespace --- .editorconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/.editorconfig b/.editorconfig index 538ba2b2..0a673d8a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -3,3 +3,4 @@ root = true [*] indent_style = tab indent_size = 2 +trim_trailing_whitespace = false From 41c0a2488e6736c885aa6b74e715b10f2e45537b Mon Sep 17 00:00:00 2001 From: Bryan Powell Date: Mon, 11 Jan 2021 10:14:02 -0800 Subject: [PATCH 2/2] Async::Task#wait_all Waits on all children to complete, including subtasks. Returns the result of the toplevel task. --- lib/async/task.rb | 10 +++++++++ spec/async/task_spec.rb | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib/async/task.rb b/lib/async/task.rb index e620106e..aabe3b6a 100644 --- a/lib/async/task.rb +++ b/lib/async/task.rb @@ -156,6 +156,16 @@ def wait # Deprecated. alias result wait # Soon to become attr :result + + # Wait on all children to complete, including subtasks. + # @return [Object] the final expression/result of the task's block + def wait_all(task = self) + task.children&.each do |child| + wait_all(child) + end + + task.wait + end # Stop the task and all of its children. # @return [void] diff --git a/spec/async/task_spec.rb b/spec/async/task_spec.rb index 12679577..197034b0 100644 --- a/spec/async/task_spec.rb +++ b/spec/async/task_spec.rb @@ -468,6 +468,52 @@ def sleep_forever expect(innocent_task).to be_finished end end + + describe '#wait_all' do + it "will wait on all subtasks to complete" do + result = nil + + reactor.async do |task| + wait_task = task.async do |subtask1| + subtask1.async do |subtask2| + subtask2.async do |subtask3| + subtask3.sleep(0.25) + + result = :subtask3 + end + end + end + + wait_task.wait_all + end + + reactor.run + + expect(result).to eq(:subtask3) + end + + it "will return the result" do + result = nil + + reactor.async do |task| + wait_task = task.async do |subtask1| + subtask1.async do |subtask2| + subtask2.async do |subtask3| + subtask3.sleep(0.25) + end + end + + :subtask1 + end + + result = wait_task.wait_all + end + + reactor.run + + expect(result).to eq(:subtask1) + end + end describe '#children' do it "enumerates children in same order they are created" do