| 1 |
use Test2::V0 -target => 'Test2::Harness::Stall::Detector'; |
| 2 |
# HARNESS-DURATION-SHORT |
| 3 |
|
| 4 |
use ok $CLASS; |
| 5 |
|
| 6 |
use File::Spec(); |
| 7 |
use File::Temp qw/tempdir/; |
| 8 |
use Test2::Harness::Util::Queue(); |
| 9 |
|
| 10 |
my $JOB = 0; |
| 11 |
|
| 12 |
sub new_workdir { |
| 13 |
return tempdir(CLEANUP => 1); |
| 14 |
} |
| 15 |
|
| 16 |
sub queue_for { |
| 17 |
my ($dir) = @_; |
| 18 |
return Test2::Harness::Util::Queue->new(file => File::Spec->catfile($dir, 'dispatch.jsonl')); |
| 19 |
} |
| 20 |
|
| 21 |
sub record { |
| 22 |
my ($dir, $action, $item, %params) = @_; |
| 23 |
|
| 24 |
queue_for($dir)->enqueue({ |
| 25 |
action => $action, |
| 26 |
item => $item, |
| 27 |
stamp => $params{stamp} // time, |
| 28 |
pid => $params{pid} // $$, |
| 29 |
}); |
| 30 |
|
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
sub task { |
| 35 |
my (%params) = @_; |
| 36 |
|
| 37 |
return { |
| 38 |
job_id => $params{job_id} // 'job-' . ++$JOB, |
| 39 |
run_id => 'run-1', |
| 40 |
file => $params{file} // 'a.t', |
| 41 |
category => $params{category} // 'general', |
| 42 |
duration => 'short', |
| 43 |
stage => 'default', |
| 44 |
conflicts => $params{conflicts} // [], |
| 45 |
shares => $params{shares} // [], |
| 46 |
smoke => 0, |
| 47 |
}; |
| 48 |
} |
| 49 |
|
| 50 |
# A run with one ready stage and however many queued tasks, laid down far |
| 51 |
# enough in the past that any threshold under an hour has elapsed. |
| 52 |
sub seed_run { |
| 53 |
my ($dir, %params) = @_; |
| 54 |
|
| 55 |
my $old = time - 3600; |
| 56 |
|
| 57 |
record($dir, queue_run => {run_id => 'run-1'}, stamp => $old); |
| 58 |
record($dir, start_run => 'run-1', stamp => $old); |
| 59 |
record($dir, stage_ready => 'default', stamp => $old); |
| 60 |
|
| 61 |
for my $t (@{$params{tasks} // []}) { |
| 62 |
record($dir, queue_task => $t, stamp => $old); |
| 63 |
} |
| 64 |
|
| 65 |
for my $t (@{$params{started} // []}) { |
| 66 |
record($dir, queue_task => $t, stamp => $old); |
| 67 |
record( |
| 68 |
$dir, |
| 69 |
start_task => { |
| 70 |
job_id => $t->{job_id}, |
| 71 |
stage => 'default', |
| 72 |
res => {args => [], env_vars => {}, record => {}}, |
| 73 |
}, |
| 74 |
stamp => $params{start_stamp} // $old, |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
sub detector { |
| 82 |
my ($dir, %params) = @_; |
| 83 |
|
| 84 |
return $CLASS->new( |
| 85 |
workdir => $dir, |
| 86 |
job_count => 4, |
| 87 |
strong => $params{strong} // 600, |
| 88 |
loose => $params{loose} // 1200, |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
# These pids feed kill() directly, so a mistake here aims a fatal signal at the |
| 93 |
# wrong process. SIGUSR1's default action is to terminate. |
| 94 |
subtest pids_for_the_signal_whitelist => sub { |
| 95 |
my $dir = new_workdir(); |
| 96 |
my $old = time - 3600; |
| 97 |
|
| 98 |
record($dir, queue_run => {run_id => 'run-1'}, stamp => $old, pid => 111); |
| 99 |
record($dir, queue_task => task(), stamp => $old, pid => 111); |
| 100 |
record($dir, start_run => 'run-1', stamp => $old, pid => 222); |
| 101 |
record($dir, stage_ready => 'default', stamp => $old, pid => 333); |
| 102 |
record($dir, queue_task => task(), stamp => $old, pid => 111); |
| 103 |
|
| 104 |
my $one = detector($dir); |
| 105 |
$one->check(); |
| 106 |
|
| 107 |
is($one->stage_pids, {default => 333}, "stage pid from stage_ready"); |
| 108 |
|
| 109 |
my $found = detector($dir)->check; |
| 110 |
ok($found, "reported") or return; |
| 111 |
|
| 112 |
is($found->{scheduler_pid}, 222, "scheduler pid from start_run, not the main process's queue_run"); |
| 113 |
}; |
| 114 |
|
| 115 |
subtest a_stage_that_goes_down_leaves_the_whitelist => sub { |
| 116 |
my $dir = new_workdir(); |
| 117 |
my $old = time - 3600; |
| 118 |
|
| 119 |
record($dir, queue_run => {run_id => 'run-1'}, stamp => $old); |
| 120 |
record($dir, start_run => 'run-1', stamp => $old); |
| 121 |
record($dir, stage_ready => 'default', stamp => $old, pid => 333); |
| 122 |
record($dir, stage_ready => 'other', stamp => $old, pid => 444); |
| 123 |
record($dir, queue_task => task(), stamp => $old); |
| 124 |
record($dir, stage_down => 'other', stamp => $old, pid => 444); |
| 125 |
|
| 126 |
my $detector = detector($dir); |
| 127 |
$detector->check(); |
| 128 |
|
| 129 |
# A dead stage's pid can be recycled, and signalling a process that never |
| 130 |
# installed the handler kills it. |
| 131 |
is($detector->stage_pids, {default => 333}, "the downed stage is gone"); |
| 132 |
}; |
| 133 |
|
| 134 |
subtest a_replay_that_keeps_failing_gives_up => sub { |
| 135 |
my $dir = new_workdir(); |
| 136 |
seed_run($dir, tasks => [task()]); |
| 137 |
|
| 138 |
# start_task for a job that was never queued makes State's handler die. |
| 139 |
record($dir, start_task => {job_id => 'nope', stage => 'default', res => {args => [], env_vars => {}, record => {}}}); |
| 140 |
|
| 141 |
my $detector = detector($dir); |
| 142 |
|
| 143 |
my @warnings; |
| 144 |
local $SIG{__WARN__} = sub { push @warnings => @_ }; |
| 145 |
|
| 146 |
for (1 .. 6) { |
| 147 |
$detector->{+Test2::Harness::Stall::Detector::LAST_CHECK()} = 0; |
| 148 |
$detector->check(); |
| 149 |
} |
| 150 |
|
| 151 |
# Re-reading a large dispatch file every second for the rest of the run, |
| 152 |
# warning each time, would be worse than not reporting at all. |
| 153 |
ok(@warnings <= 4, "stopped warning") or diag(scalar @warnings); |
| 154 |
like($warnings[-1], qr/disabled/, "said it gave up"); |
| 155 |
}; |
| 156 |
|
| 157 |
subtest parse_spec => sub { |
| 158 |
is([$CLASS->parse_spec('600:1200')], [strong => 600, loose => 1200], "pair"); |
| 159 |
is([$CLASS->parse_spec('600')], [strong => 600, loose => 600], "single value sets both"); |
| 160 |
is([$CLASS->parse_spec('0')], [], "zero disables"); |
| 161 |
|
| 162 |
# STRONG:0 means "report only when nothing is running" and is enabled. |
| 163 |
# It is also the case a caller loses by testing parse_spec in boolean |
| 164 |
# context, since a list return yields its last value there. |
| 165 |
is([$CLASS->parse_spec('600:0')], [strong => 600, loose => 0], "strong only"); |
| 166 |
is([$CLASS->parse_spec('0:1200')], [strong => 0, loose => 1200], "loose only"); |
| 167 |
|
| 168 |
my %strong_only = $CLASS->parse_spec('600:0'); |
| 169 |
ok(%strong_only, "strong-only is enabled when the result is used as a list"); |
| 170 |
is([$CLASS->parse_spec(undef)], [], "undef disables"); |
| 171 |
is([$CLASS->parse_spec('')], [], "empty disables"); |
| 172 |
# An opt-in diagnostic that silently disables itself on a typo is a trap. |
| 173 |
like(dies { $CLASS->parse_spec('abc') }, qr/Invalid --stall-report/, "non-numeric croaks"); |
| 174 |
like(dies { $CLASS->parse_spec('600:abc') }, qr/Invalid --stall-report/, "bad second field croaks"); |
| 175 |
}; |
| 176 |
|
| 177 |
subtest the_option_default_is_off_and_bare_means_sensible => sub { |
| 178 |
require App::Yath::Options::Runner; |
| 179 |
|
| 180 |
my ($opt) = grep { $_->field eq 'stall_report' } @{App::Yath::Options::Runner->options->all}; |
| 181 |
ok($opt, "found the option") or return; |
| 182 |
|
| 183 |
# Given alone the option fills in a usable pair; not given at all it stays |
| 184 |
# off. Both matter: this is opt-in, and nobody should have to know numbers |
| 185 |
# to turn it on. |
| 186 |
is($opt->type, 'd', "takes an optional value"); |
| 187 |
is($opt->autofill, '600:1200', "bare --stall-report means a sensible pair"); |
| 188 |
is($opt->default, 0, "off unless asked for"); |
| 189 |
|
| 190 |
is([$CLASS->parse_spec($opt->autofill)], [strong => 600, loose => 1200], "the autofill parses"); |
| 191 |
is([$CLASS->parse_spec($opt->default)], [], "the default is disabled"); |
| 192 |
}; |
| 193 |
|
| 194 |
subtest no_report_before_a_stage_is_ready => sub { |
| 195 |
my $dir = new_workdir(); |
| 196 |
my $old = time - 3600; |
| 197 |
|
| 198 |
# The queue is populated by the main process well before any stage comes |
| 199 |
# up, so during a long preload this looks exactly like a stall. |
| 200 |
record($dir, queue_run => {run_id => 'run-1'}, stamp => $old); |
| 201 |
record($dir, start_run => 'run-1', stamp => $old); |
| 202 |
record($dir, queue_task => task(), stamp => $old); |
| 203 |
|
| 204 |
is(detector($dir)->check(), undef, "silent while preloading"); |
| 205 |
}; |
| 206 |
|
| 207 |
subtest strong_tier_when_nothing_is_running => sub { |
| 208 |
my $dir = new_workdir(); |
| 209 |
seed_run($dir, tasks => [task(), task()]); |
| 210 |
|
| 211 |
my $got = detector($dir)->check(); |
| 212 |
ok($got, "reported") or return; |
| 213 |
is($got->{tier}, 'strong', "strong tier"); |
| 214 |
is($got->{running}, 0, "nothing running"); |
| 215 |
is($got->{pending}, 2, "counted the pending tests"); |
| 216 |
}; |
| 217 |
|
| 218 |
subtest loose_tier_when_something_is_running => sub { |
| 219 |
my $dir = new_workdir(); |
| 220 |
seed_run($dir, tasks => [task()], started => [task()]); |
| 221 |
|
| 222 |
my $got = detector($dir)->check(); |
| 223 |
ok($got, "reported") or return; |
| 224 |
is($got->{tier}, 'loose', "loose tier"); |
| 225 |
is($got->{running}, 1, "one test running"); |
| 226 |
}; |
| 227 |
|
| 228 |
subtest loose_threshold_is_not_the_strong_one => sub { |
| 229 |
my $dir = new_workdir(); |
| 230 |
|
| 231 |
# 900 seconds idle: past a 600s strong threshold, short of a 1200s loose |
| 232 |
# one. With a test running, only the loose threshold applies. |
| 233 |
seed_run($dir, tasks => [task()], started => [task()], start_stamp => time - 900); |
| 234 |
|
| 235 |
is(detector($dir)->check(), undef, "waits for the loose threshold"); |
| 236 |
}; |
| 237 |
|
| 238 |
subtest isolation_tail_is_not_a_stall => sub { |
| 239 |
my $dir = new_workdir(); |
| 240 |
seed_run($dir, tasks => [task(category => 'isolation')], started => [task()]); |
| 241 |
|
| 242 |
is(detector($dir)->check(), undef, "isolation cannot start while a test runs"); |
| 243 |
}; |
| 244 |
|
| 245 |
subtest conflict_blocked_is_not_a_stall => sub { |
| 246 |
my $dir = new_workdir(); |
| 247 |
my $running = task(conflicts => ['db']); |
| 248 |
seed_run($dir, tasks => [task(conflicts => ['db'])], started => [$running]); |
| 249 |
|
| 250 |
is(detector($dir)->check(), undef, "pending test conflicts with a running one"); |
| 251 |
}; |
| 252 |
|
| 253 |
# These mirror the rejections State::_next makes for shared locks. Getting them |
| 254 |
# wrong means reporting a stall against a scheduler waiting exactly as it |
| 255 |
# should. |
| 256 |
subtest an_exclusive_claim_waits_on_a_shared_holder => sub { |
| 257 |
my $dir = new_workdir(); |
| 258 |
seed_run($dir, tasks => [task(conflicts => ['db'])], started => [task(shares => ['db'])]); |
| 259 |
|
| 260 |
is(detector($dir)->check(), undef, "exclusive claim blocked by a running share"); |
| 261 |
}; |
| 262 |
|
| 263 |
subtest a_shared_claim_waits_on_an_exclusive_holder => sub { |
| 264 |
my $dir = new_workdir(); |
| 265 |
seed_run($dir, tasks => [task(shares => ['db'])], started => [task(conflicts => ['db'])]); |
| 266 |
|
| 267 |
is(detector($dir)->check(), undef, "shared claim blocked by a running exclusive"); |
| 268 |
}; |
| 269 |
|
| 270 |
subtest two_shared_claims_do_not_block_each_other => sub { |
| 271 |
my $dir = new_workdir(); |
| 272 |
seed_run($dir, tasks => [task(shares => ['db'])], started => [task(shares => ['db'])]); |
| 273 |
|
| 274 |
ok(detector($dir)->check(), "a share does not block another share, so this is a stall"); |
| 275 |
}; |
| 276 |
|
| 277 |
subtest one_unblocked_pending_test_is_enough => sub { |
| 278 |
my $dir = new_workdir(); |
| 279 |
seed_run( |
| 280 |
$dir, |
| 281 |
tasks => [task(category => 'isolation'), task()], |
| 282 |
started => [task()], |
| 283 |
); |
| 284 |
|
| 285 |
ok(detector($dir)->check(), "reports when any pending test could have started"); |
| 286 |
}; |
| 287 |
|
| 288 |
subtest nothing_pending_is_not_a_stall => sub { |
| 289 |
my $dir = new_workdir(); |
| 290 |
seed_run($dir, started => [task()]); |
| 291 |
|
| 292 |
is(detector($dir)->check(), undef, "no pending tests, nothing to report"); |
| 293 |
}; |
| 294 |
|
| 295 |
subtest disabled_by_zero => sub { |
| 296 |
my $dir = new_workdir(); |
| 297 |
seed_run($dir, tasks => [task()]); |
| 298 |
|
| 299 |
is(detector($dir, strong => 0, loose => 0)->check(), undef, "no report when disabled"); |
| 300 |
}; |
| 301 |
|
| 302 |
subtest repeat_is_rate_limited_and_capped => sub { |
| 303 |
my $dir = new_workdir(); |
| 304 |
seed_run($dir, tasks => [task()]); |
| 305 |
|
| 306 |
my $one = detector($dir); |
| 307 |
ok($one->check(), "first report"); |
| 308 |
$one->{+Test2::Harness::Stall::Detector::LAST_CHECK()} = 0; |
| 309 |
is($one->check(), undef, "second report held back"); |
| 310 |
|
| 311 |
my $two = detector($dir); |
| 312 |
$two->{+Test2::Harness::Stall::Detector::REPORTS()} = [map { time - 10_000 } 1 .. 5]; |
| 313 |
is($two->check(), undef, "stops after the cap"); |
| 314 |
}; |
| 315 |
|
| 316 |
subtest a_torn_trailing_line_is_ignored => sub { |
| 317 |
my $dir = new_workdir(); |
| 318 |
seed_run($dir, tasks => [task()]); |
| 319 |
|
| 320 |
open(my $fh, '>>', File::Spec->catfile($dir, 'dispatch.jsonl')) or die "Could not open queue: $!"; |
| 321 |
print $fh '{"action":"queue_task","item":'; # no newline |
| 322 |
close($fh); |
| 323 |
|
| 324 |
ok(detector($dir)->check(), "still reported despite the partial record"); |
| 325 |
}; |
| 326 |
|
| 327 |
done_testing; |