Debian Codegraph @

t/integration/concurrency.t

raw
1 use Test2::V0;
2
3 use File::Temp qw/tempdir/;
4 use File::Spec;
5
6 use App::Yath::Tester qw/yath/;
7 use Test2::Harness::Util::File::JSONL;
8
9 use Test2::Harness::Util::JSON qw/decode_json/;
10
11 my $dir = __FILE__;
12 $dir =~ s{\.t$}{}g;
13 $dir =~ s{^\./}{};
14
15 # The order jobs start and stop in is not fixed: a machine slow enough to take
16 # longer starting a job than a job takes to run will interleave them
17 # differently every time. What is fixed is how many the scheduler allows at
18 # once, and the fixtures hold each other until that many are live so the log
19 # is guaranteed to show it rather than merely likely to.
20 sub concurrency {
21 my ($log) = @_;
22
23 my @order;
24 my @events = $log->poll();
25 while (@events) {
26 if (my $event = shift @events) {
27 my $f = $event->{facet_data};
28
29 if (my $e = $f->{harness_job_exit}) {
30 push @order => [$e->{stamp}, -1];
31 }
32
33 if (my $l = $f->{harness_job_start}) {
34 push @order => [$l->{stamp}, 1];
35 }
36 }
37
38 # Check for additional events, probably should not have any, but we may hit
39 # a buffering limit in the log reader and need additional polls.
40 push @events => $log->poll;
41 }
42
43 # We care about the order in which events happened based on time stamp, not
44 # the order in which they were collected, which may be different. A start
45 # and an exit that share a stamp are counted as the exit first, so a slow
46 # clock cannot inflate the count.
47 @order = sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @order;
48
49 my ($running, $max, $starts, $exits) = (0, 0, 0, 0);
50 for my $item (@order) {
51 my $delta = $item->[1];
52
53 $running += $delta;
54 $max = $running if $running > $max;
55
56 $delta > 0 ? $starts++ : $exits++;
57 }
58
59 return {max => $max, starts => $starts, exits => $exits};
60 }
61
62 # Each run gets its own barrier directory: markers left by an earlier run
63 # would let a later one satisfy its barrier without ever overlapping.
64 run_at(4);
65 run_at(2);
66
67 sub run_at {
68 my ($jobs) = @_;
69
70 my $barrier = tempdir(CLEANUP => 1);
71
72 yath(
73 command => 'test',
74 args => [
75 $dir, '--ext=tx', "-j$jobs",
76 '-It/lib',
77 '--env-var' => "TEST_BARRIER_DIR=$barrier",
78 '--env-var' => "TEST_BARRIER_COUNT=$jobs",
79 ],
80 log => 1,
81 exit => 0,
82 test => sub {
83 my $out = shift;
84 my $stats = concurrency($out->{log});
85
86 is($stats->{starts}, 5, "All 5 tests started");
87 is($stats->{exits}, 5, "All 5 tests exited");
88
89 # The barrier holds every job in the wave until the full $jobs of them
90 # are live, so this is exact now, not a range: fewer means the limiter
91 # or the barrier failed, more means the limiter did.
92 is($stats->{max}, $jobs, "Ran exactly $jobs jobs at once, never more");
93 },
94 );
95 }
96
97 done_testing;