Skip to content

Commit

Permalink
Fix failure logs and add explicit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Jul 17, 2024
1 parent 2889407 commit bc9faab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
gem "decode"

gem "sus-fixtures-async"
gem "sus-fixtures-console", "~> 0.3"

gem "bake-test"
gem "bake-test-external"
Expand Down
2 changes: 1 addition & 1 deletion lib/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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::Event::Failure.for(error).emit("Task may have ended with unhandled exception.", severity: :warn)
Console::Event::Failure.for(error).emit(self, "Task may have ended with unhandled exception.", severity: :warn)
# else
# Console::Event::Failure.for(error).emit(self, severity: :debug)
end
Expand Down
46 changes: 46 additions & 0 deletions test/async/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require 'async/clock'
require 'async/queue'

require 'sus/fixtures/console'

require 'timer_quantum'

describe Async::Task do
Expand Down Expand Up @@ -909,4 +911,48 @@ def sleep_forever
expect(child_task).to be(:stopped?)
end
end

with "failing task" do
include_context Sus::Fixtures::Console::CapturedLogger

it "logs a warning if a task fails without being waited on" do
failed_task = nil

reactor.async do |task|
task.async do |task|
failed_task = task
raise "boom"
end
end

reactor.run

expect_console.to have_logged(
severity: be == :warn,
subject: be_equal(failed_task),
message: be == "Task may have ended with unhandled exception."
)
end

it "does not log a warning if a task fails and is waited on" do
failed_task = nil

reactor.async do |task|
expect do
task.async do |task|
# This ensures #wait is called by the parent before proceeding to raise the exception:
task.yield
failed_task = task
raise "boom"
end.wait
end.to raise_exception(RuntimeError, message: be =~ /boom/)
end

reactor.run

expect_console.not.to have_logged(
severity: be == :warn,
)
end
end
end

0 comments on commit bc9faab

Please sign in to comment.