Skip to content

Commit

Permalink
Delegate to Fiber#annotate where possible. (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Jun 5, 2023
1 parent a0d1c6a commit 0561343
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
1 change: 1 addition & 0 deletions async.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "console", "~> 1.10"
spec.add_dependency "io-event", "~> 1.1"
spec.add_dependency "timers", "~> 4.1"
spec.add_dependency "fiber-annotate"

spec.add_development_dependency "bake-test"
spec.add_development_dependency "bake-test-external"
Expand Down
21 changes: 15 additions & 6 deletions lib/async/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Copyright, 2017, by Kent Gruber.
# Copyright, 2022, by Shannon Skipper.

require 'fiber/annotate'

require_relative 'list'

module Async
Expand Down Expand Up @@ -109,20 +111,27 @@ def transient?

def annotate(annotation)
if block_given?
previous_annotation = @annotation
@annotation = annotation
yield
@annotation = previous_annotation
begin
current_annotation = @annotation
@annotation = annotation
return yield
ensure
@annotation = current_annotation
end
else
@annotation = annotation
end
end

def annotation
@annotation
end

def description
@object_name ||= "#{self.class}:#{format '%#018x', object_id}#{@transient ? ' transient' : nil}"

if @annotation
"#{@object_name} #{@annotation}"
if annotation = self.annotation
"#{@object_name} #{annotation}"
elsif line = self.backtrace(0, 1)&.first
"#{@object_name} #{line}"
else
Expand Down
18 changes: 17 additions & 1 deletion lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ def backtrace(*arguments)
@fiber&.backtrace(*arguments)
end

def annotate(annotation, &block)
if @fiber
@fiber.annotate(annotation, &block)
else
super
end
end

def annotation
if @fiber
@fiber.annotation
else
super
end
end

def to_s
"\#<#{self.description} (#{@status})>"
end
Expand Down Expand Up @@ -309,7 +325,7 @@ def stop!
end

def schedule(&block)
@fiber = Fiber.new do
@fiber = Fiber.new(annotation: self.annotation) do
set!

begin
Expand Down
26 changes: 26 additions & 0 deletions test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ def after
super
end

with '#annotate' do
it "can annotate the current task that has not started yet" do
task = Async::Task.new(reactor) do |task|
sleep
end

task.annotate("Hello World")

expect(task.annotation).to be == "Hello World"
end

it "can annotate the current task that has started" do
task = Async::Task.new(reactor) do |task|
task.annotate("Hello World")

sleep
end

expect(task.fiber).to be_nil

task.run

expect(task.fiber.annotation).to be == "Hello World"
end
end

with '.yield' do
it "can yield back to scheduler" do
state = nil
Expand Down

0 comments on commit 0561343

Please sign in to comment.