Skip to content

Commit

Permalink
Ensure the scheduler is not leaked between tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 19, 2024
1 parent 6dd0d8c commit 2a2b4ee
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 32 deletions.
7 changes: 7 additions & 0 deletions lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def self.yield
Fiber.scheduler.transfer
end

# Run the given block of code in a task, asynchronously, in the given scheduler.
def self.run(scheduler, *arguments, **options, &block)
self.new(scheduler, **options, &block).tap do |task|
task.run(*arguments)
end
end

# Create a new task.
# @parameter reactor [Reactor] the reactor this task will run within.
# @parameter parent [Task] the parent task.
Expand Down
4 changes: 1 addition & 3 deletions lib/kernel/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def Async(...)
if current = ::Async::Task.current?
return current.async(...)
elsif scheduler = Fiber.scheduler
task = ::Async::Task.new(scheduler, transient: true)
task.run(...)
return task
::Async::Task.run(scheduler, ...)
else
# This calls Fiber.set_scheduler(self):
reactor = ::Async::Reactor.new
Expand Down
4 changes: 1 addition & 3 deletions lib/kernel/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def Sync(&block)
if task = ::Async::Task.current?
yield task
elsif scheduler = Fiber.scheduler
task = ::Async::Task.new(scheduler, transient: true)
task.run(&block)
return task.wait
::Async::Task.run(scheduler, &block).wait
else
# This calls Fiber.set_scheduler(self):
reactor = Async::Reactor.new
Expand Down
58 changes: 33 additions & 25 deletions test/async/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

describe Async::Reactor do
let(:reactor) {subject.new}

after do
Fiber.set_scheduler(nil)
end

with '#run' do
it "can run tasks on different fibers" do
Expand Down Expand Up @@ -218,24 +222,6 @@
expect(result).to be_a(Async::Task)
end

with '#async' do
include Sus::Fixtures::Async::ReactorContext

it "can pass in arguments" do
reactor.async(:arg) do |task, arg|
expect(arg).to be == :arg
end.wait
end

it "passes in the correct number of arguments" do
reactor.async(:arg1, :arg2, :arg3) do |task, arg1, arg2, arg3|
expect(arg1).to be == :arg1
expect(arg2).to be == :arg2
expect(arg3).to be == :arg3
end.wait
end
end

with '#with_timeout' do
let(:duration) {1}

Expand Down Expand Up @@ -282,16 +268,38 @@
end
end

it "validates scheduler assignment" do
it "reuses existing scheduler" do
# Assign the scheduler:
reactor = self.reactor

# Close the previous scheduler:
Async {}
# Re-use the previous scheduler:
state = nil
Async do
state = :started
end

reactor.run

expect do
# The reactor is closed:
reactor.async {}
end.to raise_exception(Async::Scheduler::ClosedError)
expect(state).to be == :started
end
end

describe Async::Reactor do
include Sus::Fixtures::Async::ReactorContext

with '#async' do
it "can pass in arguments" do
reactor.async(:arg) do |task, arg|
expect(arg).to be == :arg
end.wait
end

it "passes in the correct number of arguments" do
reactor.async(:arg1, :arg2, :arg3) do |task, arg1, arg2, arg3|
expect(arg1).to be == :arg1
expect(arg2).to be == :arg2
expect(arg3).to be == :arg3
end.wait
end
end
end
2 changes: 1 addition & 1 deletion test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
let(:reactor) {Async::Reactor.new}

after do
reactor.close
Fiber.set_scheduler(nil)
end

with '#annotate' do
Expand Down

0 comments on commit 2a2b4ee

Please sign in to comment.