Debian Codegraph @

t/integration/stall_report/BlockingResource.pm

raw
1 package BlockingResource;
2 use strict;
3 use warnings;
4
5 use Time::HiRes qw/sleep time/;
6
7 use parent 'Test2::Harness::Runner::Resource';
8
9 # Longest to block for. Bounded on purpose: killing a stalled run is out of
10 # scope, so an unbounded block would leave the run for App::Yath::Tester's own
11 # timeout to kill, and that kill lands in stop() -> poll() -> release() and
12 # hangs the test process itself. A run that expects a stack trace out of this
13 # raises the bound, because it stops blocking the moment the trace is taken.
14 sub block_for { $ENV{BLOCKING_RESOURCE_MAX} || 8 }
15
16 sub available {
17 my $self = shift;
18 my ($task) = @_;
19
20 # Resources are constructed in several processes. Only the scheduler is
21 # being wedged here; blocking the others proves nothing and hangs the run.
22 return 1 unless $0 =~ m/scheduler/;
23
24 # Let one test through so the run gets going and a stage comes up.
25 return 1 unless $self->{_started};
26
27 # Block once, then let everything through so the run finishes on its own.
28 # Nothing ends a stalled run -- reporting never kills -- so a fixture that
29 # blocks forever would sit until App::Yath::Tester's own timeout, and that
30 # kill lands in stop() -> poll() -> release() and hangs the test process.
31 return 1 if $self->{_done_blocking};
32
33 $self->{_blocked_at} //= time;
34
35 # Stop blocking once this process has been asked for its stack, so the
36 # trace is taken while the scheduler really is stuck here however slow the
37 # machine is. The harness installed the handler that writes the trace;
38 # this only notices that it ran.
39 my $traced = 0;
40 my $handler = $SIG{USR1};
41 local $SIG{USR1} = sub {
42 $traced++;
43 return unless $handler && ref($handler) eq 'CODE';
44 $handler->(@_);
45 };
46
47 # A loop of short sleeps, not one long one: the detector sends SIGUSR1 and
48 # a single sleep would be cut short by it, un-wedging the fixture early.
49 my $block_for = $self->block_for;
50 while (!$traced && time - $self->{_blocked_at} < $block_for) {
51 sleep 0.2;
52 }
53
54 $self->{_done_blocking} = 1;
55
56 return 1;
57 }
58
59 sub assign {
60 my $self = shift;
61 my ($task, $state) = @_;
62 $state->{record} = 1;
63 }
64
65 sub record {
66 my $self = shift;
67 my ($job_id, $val) = @_;
68 $self->{_started}++;
69 }
70
71 sub release { }
72
73 1;