Skip to content

Commit bc9faab

Browse files
committed
Fix failure logs and add explicit tests.
1 parent 2889407 commit bc9faab

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

gems.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
gem "decode"
2424

2525
gem "sus-fixtures-async"
26+
gem "sus-fixtures-console", "~> 0.3"
2627

2728
gem "bake-test"
2829
gem "bake-test-external"

lib/async/task.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def run(*arguments)
191191
rescue => error
192192
# 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.
193193
if @finished.nil?
194-
Console::Event::Failure.for(error).emit("Task may have ended with unhandled exception.", severity: :warn)
194+
Console::Event::Failure.for(error).emit(self, "Task may have ended with unhandled exception.", severity: :warn)
195195
# else
196196
# Console::Event::Failure.for(error).emit(self, severity: :debug)
197197
end

test/async/task.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
require 'async/clock'
1010
require 'async/queue'
1111

12+
require 'sus/fixtures/console'
13+
1214
require 'timer_quantum'
1315

1416
describe Async::Task do
@@ -909,4 +911,48 @@ def sleep_forever
909911
expect(child_task).to be(:stopped?)
910912
end
911913
end
914+
915+
with "failing task" do
916+
include_context Sus::Fixtures::Console::CapturedLogger
917+
918+
it "logs a warning if a task fails without being waited on" do
919+
failed_task = nil
920+
921+
reactor.async do |task|
922+
task.async do |task|
923+
failed_task = task
924+
raise "boom"
925+
end
926+
end
927+
928+
reactor.run
929+
930+
expect_console.to have_logged(
931+
severity: be == :warn,
932+
subject: be_equal(failed_task),
933+
message: be == "Task may have ended with unhandled exception."
934+
)
935+
end
936+
937+
it "does not log a warning if a task fails and is waited on" do
938+
failed_task = nil
939+
940+
reactor.async do |task|
941+
expect do
942+
task.async do |task|
943+
# This ensures #wait is called by the parent before proceeding to raise the exception:
944+
task.yield
945+
failed_task = task
946+
raise "boom"
947+
end.wait
948+
end.to raise_exception(RuntimeError, message: be =~ /boom/)
949+
end
950+
951+
reactor.run
952+
953+
expect_console.not.to have_logged(
954+
severity: be == :warn,
955+
)
956+
end
957+
end
912958
end

0 commit comments

Comments
 (0)