From 9338fc4384cb0dddecd6895db1d98a578ebefc67 Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 1 Oct 2024 18:01:07 +0200 Subject: [PATCH 1/7] fix dependencies so tests run through --- gems.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gems.rb b/gems.rb index 120e8af7..d5badfd3 100644 --- a/gems.rb +++ b/gems.rb @@ -20,7 +20,7 @@ end group :test do - gem "sus", "~> 0.29", ">= 0.29.1" + gem "sus", "~> 0.31" gem "covered" gem "decode" gem "rubocop" From 5ab35c5baa1780c7dab19c6c16cd25609e2f7950 Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 1 Oct 2024 18:16:54 +0200 Subject: [PATCH 2/7] Kernel#Async: test passing through of annotation: keyword --- test/kernel/async.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/kernel/async.rb b/test/kernel/async.rb index 52f78d22..c59b9c50 100644 --- a/test/kernel/async.rb +++ b/test/kernel/async.rb @@ -17,6 +17,10 @@ Async(transient: true) do |task| expect(task).to be(:transient?) end + + Async(annotation: 'foobar') do |task| + expect(task.annotation).to be == 'foobar' + end end end end From 665534ddc1a3de9f3d43aa81a338eb5210ffa520 Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 1 Oct 2024 18:17:59 +0200 Subject: [PATCH 3/7] Kernel#Sync: pass annotation: keyword to newly created Reactor --- lib/kernel/sync.rb | 4 ++-- test/kernel/sync.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/kernel/sync.rb b/lib/kernel/sync.rb index df384439..5eedf5a4 100644 --- a/lib/kernel/sync.rb +++ b/lib/kernel/sync.rb @@ -15,7 +15,7 @@ module Kernel # # @public Since `stable-v1`. # @asynchronous Will block until given block completes executing. - def Sync(&block) + def Sync(*arguments, **options, &block) if task = ::Async::Task.current? yield task elsif scheduler = Fiber.scheduler @@ -25,7 +25,7 @@ def Sync(&block) reactor = Async::Reactor.new begin - return reactor.run(finished: ::Async::Condition.new, &block).wait + return reactor.run(*arguments, **options, finished: ::Async::Condition.new, &block).wait ensure Fiber.set_scheduler(nil) end diff --git a/test/kernel/sync.rb b/test/kernel/sync.rb index a7b477f8..54146933 100644 --- a/test/kernel/sync.rb +++ b/test/kernel/sync.rb @@ -21,6 +21,12 @@ expect(result).to be == value end + + it "passes options through to initial task" do + Sync(annotation: 'foobar') do |task| + expect(task.annotation).to be == 'foobar' + end + end it "can run inside reactor" do Async do |task| From 528e314f927800ae8dd00c8c07eba7d1f02da7cb Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 29 Oct 2024 11:18:46 +0100 Subject: [PATCH 4/7] Kernel#Sync: replace and restore existing task's annotation --- lib/kernel/sync.rb | 6 +++++- test/kernel/sync.rb | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/kernel/sync.rb b/lib/kernel/sync.rb index 5eedf5a4..0a520b25 100644 --- a/lib/kernel/sync.rb +++ b/lib/kernel/sync.rb @@ -17,7 +17,11 @@ module Kernel # @asynchronous Will block until given block completes executing. def Sync(*arguments, **options, &block) if task = ::Async::Task.current? - yield task + if annotation = options[:annotation] + task.annotate(annotation) { yield task } + else + yield task + end elsif scheduler = Fiber.scheduler ::Async::Task.run(scheduler, &block).wait else diff --git a/test/kernel/sync.rb b/test/kernel/sync.rb index 54146933..854760e1 100644 --- a/test/kernel/sync.rb +++ b/test/kernel/sync.rb @@ -27,19 +27,39 @@ expect(task.annotation).to be == 'foobar' end end - + it "can run inside reactor" do Async do |task| result = Sync do |sync_task| expect(Async::Task.current).to be == task expect(sync_task).to be == task - + next value end - + expect(result).to be == value end end + + with "parent task" do + it "replaces and restores existing task's annotation" do + annotations = [] + + Async(annotation: "foo") do |t1| + annotations << t1.annotation + + Sync(annotation: "bar") do |t2| + expect(t2).to be_equal(t1) + annotations << t1.annotation + end + + annotations << t1.annotation + end.wait + + expect(annotations).to be == %w[foo bar foo] + end + end + it "can propagate error without logging them" do expect(Console).not.to receive(:error) From c166dcc0e7dd993e91ee8f50b1f8d94754d27205 Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 29 Oct 2024 11:25:04 +0100 Subject: [PATCH 5/7] Kernel#Sync: only pass :annotation kwarg --- lib/kernel/sync.rb | 12 +++++++++--- test/kernel/sync.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/kernel/sync.rb b/lib/kernel/sync.rb index 0a520b25..b42a907c 100644 --- a/lib/kernel/sync.rb +++ b/lib/kernel/sync.rb @@ -15,9 +15,9 @@ module Kernel # # @public Since `stable-v1`. # @asynchronous Will block until given block completes executing. - def Sync(*arguments, **options, &block) + def Sync(annotation: nil, &block) if task = ::Async::Task.current? - if annotation = options[:annotation] + if annotation task.annotate(annotation) { yield task } else yield task @@ -29,7 +29,13 @@ def Sync(*arguments, **options, &block) reactor = Async::Reactor.new begin - return reactor.run(*arguments, **options, finished: ::Async::Condition.new, &block).wait + options = { + finished: ::Async::Condition.new + } + + options[:annotation] = annotation if annotation + + return reactor.run(**options, &block).wait ensure Fiber.set_scheduler(nil) end diff --git a/test/kernel/sync.rb b/test/kernel/sync.rb index 854760e1..249fee4f 100644 --- a/test/kernel/sync.rb +++ b/test/kernel/sync.rb @@ -22,7 +22,7 @@ expect(result).to be == value end - it "passes options through to initial task" do + it "passes annotation through to initial task" do Sync(annotation: 'foobar') do |task| expect(task.annotation).to be == 'foobar' end From b116c23faab435f534eae231a07649390e16fdd0 Mon Sep 17 00:00:00 2001 From: Patrik Wenger Date: Tue, 29 Oct 2024 14:58:47 +0100 Subject: [PATCH 6/7] Kernel#Sync: refactor --- lib/kernel/sync.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/kernel/sync.rb b/lib/kernel/sync.rb index b42a907c..77a081a0 100644 --- a/lib/kernel/sync.rb +++ b/lib/kernel/sync.rb @@ -29,13 +29,7 @@ def Sync(annotation: nil, &block) reactor = Async::Reactor.new begin - options = { - finished: ::Async::Condition.new - } - - options[:annotation] = annotation if annotation - - return reactor.run(**options, &block).wait + return reactor.run(annotation: annotation, finished: ::Async::Condition.new, &block).wait ensure Fiber.set_scheduler(nil) end From ff88c14bab392ae41f23ebf2c9d494fc23188bd1 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 30 Oct 2024 08:41:03 +1300 Subject: [PATCH 7/7] Update lib/kernel/sync.rb --- lib/kernel/sync.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kernel/sync.rb b/lib/kernel/sync.rb index 77a081a0..40ac242f 100644 --- a/lib/kernel/sync.rb +++ b/lib/kernel/sync.rb @@ -18,7 +18,7 @@ module Kernel def Sync(annotation: nil, &block) if task = ::Async::Task.current? if annotation - task.annotate(annotation) { yield task } + task.annotate(annotation) {yield task} else yield task end