Skip to content

Commit

Permalink
Tidy up some tests, introduce internal Task#warn for skipping warni…
Browse files Browse the repository at this point in the history
…ngs.
  • Loading branch information
ioquatix committed Nov 8, 2024
1 parent a6617e3 commit 7bb3193
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
6 changes: 5 additions & 1 deletion lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def run(*arguments)
rescue => error
# I'm not completely happy with this overhead, but the alternative is to not log anything which makes debugging extremely difficult. Maybe we can introduce a debug wrapper which adds extra logging.
if @finished.nil?
Console.warn(self, "Task may have ended with unhandled exception.", exception: error)
warn(self, "Task may have ended with unhandled exception.", exception: error)
end

raise
Expand Down Expand Up @@ -360,6 +360,10 @@ def current?

private

def warn(...)
Console.warn(...)
end

# Finish the current task, moving any children to the parent.
def finish!
# Don't hold references to the fiber or block after the task has finished:
Expand Down
10 changes: 6 additions & 4 deletions test/async/barrier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,25 @@

with "#wait" do
it "should wait for tasks even after exceptions" do
task1 = barrier.async do
task1 = barrier.async do |task|
expect(task).to receive(:warn).and_return(nil)

raise "Boom"
end

task2 = barrier.async do
end

expect(task1).to be(:failed?)
expect(task2).to be(:finished?)

expect{barrier.wait}.to raise_exception(RuntimeError, message: be =~ /Boom/)

barrier.wait until barrier.empty?

expect{task1.wait}.to raise_exception(RuntimeError, message: be =~ /Boom/)

expect(barrier).to be(:empty?)

expect(task1).to be(:failed?)
expect(task2).to be(:finished?)
end

it "waits for tasks in order" do
Expand Down
12 changes: 8 additions & 4 deletions test/async/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# Copyright, 2017, by Devin Christensen.

require "async"
require "async/variable"
require "sus/fixtures/async"
require "benchmark/ips"

describe Async::Reactor do
let(:reactor) {subject.new}
Expand Down Expand Up @@ -198,6 +198,8 @@
it "can't return" do
expect do
Async do |task|
expect(task).to receive(:warn).and_return(nil)

return
end.wait
end.to raise_exception(LocalJumpError)
Expand Down Expand Up @@ -229,14 +231,14 @@
start_time = Time.now

subject.run do |task|
condition = Async::Condition.new
variable = Async::Variable.new

task.with_timeout(duration) do
task.async do
condition.wait
variable.wait
end

condition.signal
variable.resolve

task.yield

Expand All @@ -254,6 +256,8 @@
it "raises specified exception" do
expect do
subject.run do |task|
expect(task).to receive(:warn).and_return(nil)

task.with_timeout(0.0, timeout_class) do
task.sleep(1.0)
end
Expand Down
38 changes: 10 additions & 28 deletions test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@
it "can yield back to scheduler" do
state = nil

reactor.async do |task|
reactor.run do |task|
child = task.async do
state = :yielding
Async::Task.yield
state = :yielded
end

# Async::Task.

Fiber.scheduler.resume(child.fiber)
end

reactor.run

expect(state).to be == :yielded
end
end
Expand Down Expand Up @@ -563,26 +563,10 @@
end

with "#with_timeout" do
it "can extend timeout" do
reactor.async do |task|
task.with_timeout(0.02) do |timer|
task.sleep(0.01)

expect(timer.fires_in).to be_within(Q).of(0.01)

timer.reset

expect(timer.fires_in).to be_within(Q).of(0.02)
end
end

reactor.run
end

it "will timeout if execution takes too long" do
state = nil

reactor.async do |task|
Sync do |task|
begin
task.with_timeout(0.01) do
state = :started
Expand All @@ -594,16 +578,14 @@
end
end

reactor.run

expect(state).to be == :timeout
end

it "will timeout while getting from input" do
input, output = IO.pipe
error = nil

reactor.async do |task|
Sync do |task|
begin
# This can invoke `io_wait`, which previously had `rescue TimeoutError`, causing the timeout to be ignored.
task.with_timeout(0.1) {input.gets}
Expand All @@ -612,8 +594,6 @@
end
end

reactor.run

expect(error).to be_a(Async::TimeoutError)
ensure
input.close
Expand Down Expand Up @@ -750,9 +730,11 @@ def sleep_forever
raise "The space time converter has failed."
end

expect do
task.wait
end.to raise_exception(RuntimeError, message: be =~ /space time converter/)
reactor.run do
expect do
task.wait
end.to raise_exception(RuntimeError, message: be =~ /space time converter/)
end

expect(task.result).to be_a(RuntimeError)
end
Expand Down
6 changes: 3 additions & 3 deletions test/kernel/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@


it "can propagate error without logging them" do
expect(Console).not.to receive(:error)

expect do
Sync do
Sync do |task|
expect(task).not.to receive(:warn)

raise StandardError, "brain not provided"
end
end.to raise_exception(StandardError, message: be =~ /brain/)
Expand Down

0 comments on commit 7bb3193

Please sign in to comment.