| 1 |
use Test2::V0; |
| 2 |
use Test2::Require::AuthorTesting; |
| 3 |
|
| 4 |
use App::Yath::Tester qw/yath/; |
| 5 |
use Test2::Harness::Util::JSON qw/decode_json/; |
| 6 |
use File::Spec(); |
| 7 |
use File::Temp qw/tempdir/; |
| 8 |
|
| 9 |
# Always somewhere of our own. The bundle defaults to the directory yath was |
| 10 |
# run from, and the suite must not need the distribution directory to be |
| 11 |
# writable -- at install time it may not be. |
| 12 |
my $BUNDLE_DIR = tempdir(CLEANUP => 1); |
| 13 |
|
| 14 |
sub bundles { |
| 15 |
return glob(File::Spec->catfile($BUNDLE_DIR, 'yath-stall-report-*.json')); |
| 16 |
} |
| 17 |
|
| 18 |
my $dir = __FILE__; |
| 19 |
$dir =~ s{\.t$}{}g; |
| 20 |
$dir =~ s{^\./}{}; |
| 21 |
|
| 22 |
# BlockingResource lets one test start, then blocks the scheduler inside |
| 23 |
# available() for a bounded period before letting the rest through. With a |
| 24 |
# short --stall-report the main process should notice and print a report while |
| 25 |
# the scheduler is still stuck, and the run should still finish normally -- |
| 26 |
# reporting never ends a run. |
| 27 |
yath( |
| 28 |
command => 'test', |
| 29 |
args => [$dir, '--ext=tx', '-j2', "-D$dir", '-R+BlockingResource', '--stall-report=3:3', "--stall-report-dir=$BUNDLE_DIR"], |
| 30 |
# The fixture stops blocking as soon as its stack has been taken, so this |
| 31 |
# is a ceiling for a machine too slow to get there, not a wait. It stays |
| 32 |
# well under App::Yath::Tester's own timeout, which would kill the run. |
| 33 |
env => {BLOCKING_RESOURCE_MAX => 30}, |
| 34 |
exit => 0, |
| 35 |
test => sub { |
| 36 |
my $out = shift; |
| 37 |
|
| 38 |
like($out->{output}, qr/YATH STALL REPORT BEGIN/, "printed a stall report"); |
| 39 |
like($out->{output}, qr/no test has started in/, "said what it observed"); |
| 40 |
like($out->{output}, qr/may be benign/, "said the report may be benign"); |
| 41 |
|
| 42 |
# The whole signal-to-trace chain: four separate call sites have to |
| 43 |
# agree on the stall directory, and if any disagrees the report says |
| 44 |
# "No stack traces were produced" -- indistinguishable from the |
| 45 |
# legitimate case where a process could not run its handler. |
| 46 |
like($out->{output}, qr/BlockingResource::available/, "captured the stack of the stuck scheduler"); |
| 47 |
|
| 48 |
my @bundles = bundles(); |
| 49 |
ok(@bundles, "wrote a bundle that outlives the run") or return; |
| 50 |
|
| 51 |
my $size = -s $bundles[0]; |
| 52 |
ok($size > 1024, "bundle has the detail the text report leaves out") or diag("size: $size"); |
| 53 |
|
| 54 |
like($bundles[0], qr/yath-stall-report-[0-9A-F-]+-\d+\.json$/i, "named by run id and report number"); |
| 55 |
|
| 56 |
# The bundle also goes into the aux log, which the collector forwards |
| 57 |
# to the yath UI. It has to be one physical line there, or the |
| 58 |
# collector makes an info facet per line instead of one for the whole |
| 59 |
# bundle. encode_json escapes embedded newlines, which is what keeps |
| 60 |
# that true however large the bundle gets. |
| 61 |
# The renderer prefixes the tag it derived from the aux log's name; |
| 62 |
# what matters is that the bundle is one line behind that prefix. |
| 63 |
my ($line) = map { m/^\(\s*STALL\s*\)\s+(\{.*)$/ ? $1 : () } split /\n/, $out->{output}; |
| 64 |
ok($line, "the bundle reached the log as a single line") or return; |
| 65 |
|
| 66 |
my $data = eval { decode_json($line) }; |
| 67 |
ok($data, "and it parses") or diag($@); |
| 68 |
ok($data->{samples} && @{$data->{samples}}, "with the per-process samples in it"); |
| 69 |
}, |
| 70 |
); |
| 71 |
|
| 72 |
# A healthy run says nothing about stalls and is not truncated. |
| 73 |
yath( |
| 74 |
command => 'test', |
| 75 |
args => [$dir, '--ext=tx', '-j2', '--stall-report=60:60', "--stall-report-dir=$BUNDLE_DIR"], |
| 76 |
exit => 0, |
| 77 |
test => sub { |
| 78 |
my $out = shift; |
| 79 |
unlike($out->{output}, qr/STALL REPORT/, "no report on a healthy run"); |
| 80 |
}, |
| 81 |
); |
| 82 |
|
| 83 |
# Disabled by default. |
| 84 |
yath( |
| 85 |
command => 'test', |
| 86 |
args => [$dir, '--ext=tx', '-j2', "-D$dir", '-R+BlockingResource', "--stall-report-dir=$BUNDLE_DIR"], |
| 87 |
# No detector runs at all here, so the length of the block has no bearing |
| 88 |
# on the assertion. Keep it short instead of sitting out the fixture default. |
| 89 |
env => {BLOCKING_RESOURCE_MAX => 2}, |
| 90 |
exit => 0, |
| 91 |
test => sub { |
| 92 |
my $out = shift; |
| 93 |
unlike($out->{output}, qr/STALL REPORT/, "no report when the option is not given"); |
| 94 |
}, |
| 95 |
); |
| 96 |
|
| 97 |
done_testing; |