Files
ubicloud/spec/lib/thread_printer_spec.rb
Daniel Farina fce1ce0eb4 Improve thread dump reporting
It's been a while since this has been revised.  This revision seeks to
make the following easier:

It's hard to find the beginning and end of thread dumps, particularly
if they are consecutive.  Solution: add `--BEGIN...` and `--END...`
framing, plus a disambiguating timestamp that matches between the two.
The rough inspiration is MIME format of email.

The thread dumps are often triggered by apoptosis, which needs to exit
the entire process in a timely manner to maintain mutual exclusion.
In doing so, it does collatoral damage to other threads that happen to
be running in the same process at the time.

The thread dump emitted likewise includes every thread, not just the
problematic one.  So, print the thread age, when available.  The
oldest are most likely to be implicated.

To make the thread age available in the case I care about most, the
`created_at` thread local variable is set in the dispatcher which runs
Strands.
2025-01-25 15:44:27 -08:00

28 lines
1.2 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ThreadPrinter do
describe "#print" do
it "can dump threads" do
expect(described_class).to receive(:puts).with(/--BEGIN THREAD DUMP, .*/)
expect(described_class).to receive(:puts).with(/Thread: #<Thread:.*>/)
expect(described_class).to receive(:puts).with(/backtrace/)
expect(described_class).to receive(:puts).with(/--END THREAD DUMP, .*/)
described_class.run
end
it "can handle threads with a nil backtrace and/or a created_at" do
# The documentation calls out that the backtrace is an array or
# nil.
expect(described_class).to receive(:puts).with(/--BEGIN THREAD DUMP, .*/)
expect(described_class).to receive(:puts).with(/Thread: #<InstanceDouble.*>/)
expect(described_class).to receive(:puts).with(/Created at: .*/)
expect(described_class).to receive(:puts).with(nil)
expect(described_class).to receive(:puts).with(/--END THREAD DUMP, .*/)
th = instance_double(Thread, backtrace: nil)
expect(th).to receive(:[]).with(:created_at).and_return(Time.now - 30)
expect(Thread).to receive(:list).and_return([th])
described_class.run
end
end
end