Debian Codegraph @

t/integration/resource.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 yath(
16 command => 'test',
17 args => [$dir, '--ext=tx', '-j4', "-D$dir", '-R+Resource'],
18 log => 1,
19 exit => 0,
20 test => sub {
21 my $out = shift;
22 my $log = $out->{log};
23
24 my @events = $log->poll();
25
26 my %pids;
27 my %msgs;
28 for my $event (@events) {
29 my $f = $event->{facet_data};
30 my $info = $f->{info} or next;
31 for my $i (@$info) {
32 next unless $i->{tag} eq 'INTERNAL';
33 if ($i->{details} =~ m/^(\S+) - (yath-\S+)$/) {
34 $pids{$1} = $2;
35 next;
36 }
37
38 next unless $i->{details} =~ m/^(\S+) - (?:(\S+): \S+ - (\d)|(.+))$/;
39 my ($pid, $action, $res_id) = ($1, ($2 || $4), $3);
40
41 $pid = $pids{$pid} // $pid;
42
43 if ($res_id) {
44 push @{$msgs{$pid}->{$res_id}} => $action;
45 }
46 else {
47 push @{$msgs{$pid}->{$_}} => $action for keys %{$msgs{$pid}};
48 }
49 }
50 }
51
52 # Which slot serves which test is up to timing: a machine where one
53 # test finishes early lets its slot take the next two, and nothing is
54 # wrong with that. What has to hold is that each slot is used and
55 # freed in order, that all four tests went through, and that the
56 # scheduler noticed when it ran out of slots.
57 my $cycle = sub {
58 my ($actions, @pattern) = @_;
59
60 my $i = 0;
61 for my $action (@$actions) {
62 return "expected $pattern[$i], got $action" unless $action eq $pattern[$i];
63 $i = ($i + 1) % @pattern;
64 }
65
66 return "ended mid-cycle, expected $pattern[$i]" if $i;
67 return undef;
68 };
69
70 my $runner = $msgs{'yath-nested-runner'};
71 is([sort keys %$runner], [1, 2], "The runner saw both slots");
72
73 my %runner_counts;
74 for my $slot (sort keys %$runner) {
75 my @actions = @{$runner->{$slot}};
76
77 is(pop(@actions), 'RESOURCE CLEANUP', "Slot $slot was cleaned up at the end of the run");
78 $runner_counts{$_}++ for @actions;
79
80 is($cycle->(\@actions, 'Record', 'Release'), undef, "Slot $slot was recorded and released in turn by the runner")
81 or diag(join(', ' => @{$runner->{$slot}}));
82 }
83
84 is(\%runner_counts, {Record => 4, Release => 4}, "The runner ran all 4 tests");
85
86 my $scheduler = $msgs{'yath-nested-scheduler'};
87 is([sort keys %$scheduler], [1, 2], "The scheduler saw both slots");
88
89 my %scheduler_counts;
90 my $no_slots = 0;
91 for my $slot (sort keys %$scheduler) {
92 my @actions = grep { $_ ne 'No Slots' } @{$scheduler->{$slot}};
93
94 $no_slots++ if @actions != @{$scheduler->{$slot}};
95 $scheduler_counts{$_}++ for @actions;
96
97 is($cycle->(\@actions, 'Assigned', 'Record', 'Release'), undef, "Slot $slot was assigned, recorded and released in turn")
98 or diag(join(', ' => @{$scheduler->{$slot}}));
99 }
100
101 is(\%scheduler_counts, {Assigned => 4, Record => 4, Release => 4}, "The scheduler assigned all 4 tests");
102 ok($no_slots, "The scheduler ran out of slots at some point");
103 },
104 );
105
106 done_testing;
107
108 1;