blib/lib/Langertha/Holmgang/Batch.pm

filter: Statementstmt 98.4% 254 / 258 Branchbran 69.5% 64 / 92 Conditioncond 51.3% 56 / 109 MC/DCmcdc 20.0% 12 / 60 Subroutinesub 95.2% 40 / 42 Podpod 60.0% 6 / 10 totaltotal 75.6% 432 / 571 CC 101 SCAR 31.4
CC
101
Cov
81%
BUILD
73.4
_take_over
16.8
report
12.1
CRAP 23.1
« blib/lib/Langertha/Holmgang/Haystack.pm ↑ Summary blib/lib/Langertha/Holmgang/Report.pm »
1 package Langertha::Holmgang::Batch;
2 # ABSTRACT: Suites against targets and their variants, repeated, with bounded concurrent requests per target, resumable
3  
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use Future::Utils qw( fmap_void );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Langertha::Holmgang::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Langertha::Holmgang::Runner;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Langertha::Holmgang::Target;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 18 our $VERSION = '0.002';
17  
18  
19 18 has suites => ( is => 'ro', required => 1 );
20  
21  
22 18 has targets => ( is => 'ro', required => 1 );
23  
24  
25 18 has repeat => ( is => 'ro' );
26  
27  
28 18 has resume => ( is => 'ro' );
29  
30  
31 18 has sandbox => ( is => 'ro' );
32 18 has sandbox_label => ( is => 'ro' );
33  
34  
35 18 has on_event => ( is => 'ro' );
36  
37  
38 159 ▶sub runner_class { 'Langertha::Holmgang::Runner' }
Subroutine runner_class: called CC=1 SCAR=0.0
Pod undocumented
39 299 ▶sub target_class { 'Langertha::Holmgang::Target' }
Subroutine target_class: called CC=1 SCAR=0.0
Pod undocumented
40 71 ▶sub report_class { 'Langertha::Holmgang::Report' }
Subroutine report_class: called CC=1 SCAR=0.0
Pod undocumented
41  
42 26 ▶sub error_max_length { 2000 }
Subroutine error_max_length: called CC=1 SCAR=0.0
Pod documented
43  
44  
45 18 my $JSON = JSON::MaybeXS->new( utf8 => 0, canonical => 1 );
46  
47 #### Building
48  
49 68 ▶sub BUILD {
Subroutine BUILD: called CC=35 SCAR=43.0
Pod undocumented
50 68 my ( $self ) = @_;
51 68 my $suites = $self->suites;
52 croak __PACKAGE__.'->new: suites must be a non-empty array of suites'
53 68 ▶ unless ref $suites eq 'ARRAY' && @$suites && !grep { !( blessed $_ && $_->can('cases') ) } @$suites;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $suites eq "ARRAY" and @$suites and not grep({not builtin::blessed($_) && $_->can("cases");} @$suites) 0 68 68
Condition33%3/9
ref $suites eq "ARRAY" and @$suites and not grep({not builtin::blessed($_) && $_->can("cases");} @$suites)
!ll&&!rl&&r
0068
ref $suites eq "ARRAY" and @$suites
!ll&&!rl&&r
0068
builtin::blessed($_) && $_->can('cases')
!ll&&!rl&&r
0090
MC/DC0%0/3
ref $suites eq "ARRAY" and @$suites and not grep({not builtin::blessed($_) && $_->can("cases");} @$suites)
ref $suites eq "ARRAY" @$suites not grep({not builtin::blessed($_) && $_->can("cases");} @$suites)
MC/DC0%0/2
builtin::blessed($_) && $_->can('cases')
builtin::blessed($_) $_->can('cases')
Truth table25%1/4
A and B and not(C)
A= ref $suites eq "ARRAY"B= @$suitesC= not grep({not builtin::blessed($_) && $_->can("cases");} @$suites)
ABCresult
0XX0
10X0
1100
1111
Truth table33%1/3
A && B
A= builtin::blessed($_)B= $_->can('cases')
ABresult
0X0
100
111
54 68 my $targets = $self->targets;
55 68 ▶ croak __PACKAGE__.'->new: targets must be a non-empty array' unless ref $targets eq 'ARRAY' && @$targets;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $targets eq "ARRAY" and @$targets 0 68 68
Condition33%1/3
ref $targets eq "ARRAY" and @$targets
!ll&&!rl&&r
0068
MC/DC0%0/2
ref $targets eq "ARRAY" and @$targets
ref $targets eq "ARRAY" @$targets
Truth table33%1/3
A and B
A= ref $targets eq "ARRAY"B= @$targets
ABresult
0X0
100
111
56 68 ▶ croak __PACKAGE__.'->new: repeat must be a whole number from 1 to 100'
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->repeat and not $self->repeat =~ /\A[0-9]+\z/ && $self->repeat >= 1 && $self->repeat <= 100 0 68 68
Condition44%4/9
defined $self->repeat and not $self->repeat =~ /\A[0-9]+\z/ && $self->repeat >= 1 && $self->repeat <= 100
!ll&&!rl&&r
48200
$self->repeat =~ /\A[0-9]+\z/ && $self->repeat >= 1 && $self->repeat <= 100
!ll&&!rl&&r
0020
$self->repeat =~ /\A[0-9]+\z/ and $self->repeat >= 1
!ll&&!rl&&r
0020
MC/DC0%0/4
defined $self->repeat and not $self->repeat =~ /\A[0-9]+\z/ && $self->repeat >= 1 && $self->repeat <= 100
defined $self->repeat $self->repeat =~ /\A[0-9]+\z/ $self->repeat >= 1 $self->repeat <= 100
Truth table40%2/5
A and not(B and C && D)
A= defined $self->repeatB= $self->repeat =~ /\A[0-9]+\z/C= $self->repeat >= 1D= $self->repeat <= 100
ABCDresult
0XXX0
11110
10XX1
110X1
11101
57 if defined $self->repeat && !( $self->repeat =~ /\A[0-9]+\z/ && $self->repeat >= 1 && $self->repeat <= 100 );
58 68 ▶ croak __PACKAGE__.'->new: on_event must be a code reference' if defined $self->on_event && ref $self->on_event ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->on_event and ref $self->on_event ne "CODE" 0 68 68
Condition66%2/3
defined $self->on_event and ref $self->on_event ne "CODE"
!ll&&!rl&&r
27410
MC/DC0%0/2
defined $self->on_event and ref $self->on_event ne "CODE"
defined $self->on_event ref $self->on_event ne "CODE"
Truth table66%2/3
A and B
A= defined $self->on_eventB= ref $self->on_event ne "CODE"
ABresult
0X0
100
111
59 68 my ($first) = map { @{ $_->cases } } @$suites;
60 68 my @runs;
61 68 for my $ti ( 0 .. $#$targets ) {
62 69 my $t = $targets->[$ti];
63 69 my $where = __PACKAGE__.'->new: targets['.$ti.']';
64 69 ▶ croak $where.' must be a hash' unless ref $t eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $t eq "HASH" 0 69 69
65 croak $where.'.engine must be a code reference or an engine with chat_f'
66 69 ▶ unless ref $t->{engine} eq 'CODE' || ( blessed $t->{engine} && $t->{engine}->can('chat_f') );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$t{"engine"} eq "CODE" or builtin::blessed($$t{"engine"}) and $$t{"engine"}->can("chat_f") 0 69 69
Condition50%3/6
ref $$t{"engine"} eq "CODE" or builtin::blessed($$t{"engine"}) and $$t{"engine"}->can("chat_f")
l!l&&r!l&&!r
6450
builtin::blessed($$t{"engine"}) and $$t{"engine"}->can("chat_f")
!ll&&!rl&&r
005
MC/DC0%0/3
ref $$t{"engine"} eq "CODE" or builtin::blessed($$t{"engine"}) and $$t{"engine"}->can("chat_f")
ref $$t{"engine"} eq "CODE" builtin::blessed($$t{"engine"}) $$t{"engine"}->can("chat_f")
Truth table50%2/4
A or B and C
A= ref $$t{"engine"} eq "CODE"B= builtin::blessed($$t{"engine"})C= $$t{"engine"}->can("chat_f")
ABCresult
1XX1
0111
00X0
0100
67 69 ▶ my $conc = $t->{concurrency} // 1;
Condition100%2/2
$$t{"concurrency"} // 1
l!l
4128
MC/DC100%1/1
$$t{"concurrency"} // 1
$$t{"concurrency"}
Truth table100%2/2
A
A= $$t{"concurrency"}
Aresult
11
00
68 69 ▶ croak $where.'.concurrency must be a whole number from 1' unless $conc =~ /\A[0-9]+\z/ && $conc >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $conc =~ /\A[0-9]+\z/ and $conc >= 1 0 69 69
Condition33%1/3
$conc =~ /\A[0-9]+\z/ and $conc >= 1
!ll&&!rl&&r
0069
MC/DC0%0/2
$conc =~ /\A[0-9]+\z/ and $conc >= 1
$conc =~ /\A[0-9]+\z/ $conc >= 1
Truth table33%1/3
A and B
A= $conc =~ /\A[0-9]+\z/B= $conc >= 1
ABresult
0X0
100
111
69 69 ▶ my $variants = $t->{variants} // [ { name => 'default', settings => {} } ];
Condition100%2/2
$$t{"variants"} // [{"name", "default", "settings", {}}]
l!l
5019
MC/DC100%1/1
$$t{"variants"} // [{"name", "default", "settings", {}}]
$$t{"variants"}
Truth table100%2/2
A
A= $$t{"variants"}
Aresult
11
00
70 69 ▶ croak $where.'.variants must be a non-empty array' unless ref $variants eq 'ARRAY' && @$variants;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $variants eq "ARRAY" and @$variants 0 69 69
Condition33%1/3
ref $variants eq "ARRAY" and @$variants
!ll&&!rl&&r
0069
MC/DC0%0/2
ref $variants eq "ARRAY" and @$variants
ref $variants eq "ARRAY" @$variants
Truth table33%1/3
A and B
A= ref $variants eq "ARRAY"B= @$variants
ABresult
0X0
100
111
71 69 my %seen;
72 my @variants;
73 69 for my $vi ( 0 .. $#$variants ) {
74 72 my $v = $variants->[$vi];
75 72 ▶ croak $where.'.variants['.$vi.'] needs a name' unless ref $v eq 'HASH' && defined $v->{name} && length $v->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "HASH" and defined $$v{"name"} and length $$v{"name"} 0 72 72
Condition33%2/6
ref $v eq "HASH" and defined $$v{"name"} and length $$v{"name"}
!ll&&!rl&&r
0072
ref $v eq "HASH" and defined $$v{"name"}
!ll&&!rl&&r
0072
MC/DC0%0/3
ref $v eq "HASH" and defined $$v{"name"} and length $$v{"name"}
ref $v eq "HASH" defined $$v{"name"} length $$v{"name"}
Truth table25%1/4
A and B and C
A= ref $v eq "HASH"B= defined $$v{"name"}C= length $$v{"name"}
ABCresult
0XX0
10X0
1100
1111
76 72 ▶ croak $where.': variant '.$v->{name}.' named twice' if $seen{ $v->{name} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$$v{"name"}}++ 1 71 72
77 # the settings checked once without and once against the engine (the first case, slot 0)
78 71 ▶ my $settings = $self->target_class->check_variant( $v->{settings} // {} );
Condition100%2/2
$$v{"settings"} // {}
l!l
692
MC/DC100%1/1
$$v{"settings"} // {}
$$v{"settings"}
Truth table100%2/2
A
A= $$v{"settings"}
Aresult
11
00
79 64 $self->target_class->check_variant( $settings, $self->_engine( $t, $first, 0 ) );
80 60 push @variants, { name => ''.$v->{name}, settings => $settings };
81 }
82 57 $self->{_target}[$ti] = { %$t, concurrency => 0 + $conc, variants => \@variants };
83 57 for my $v ( @variants ) {
84 59 for my $suite ( @$suites ) {
85 81 ▶ push @runs, {
Condition80%4/5
($self->repeat // $suite->repeat) // 1
l!l
2754
$self->repeat // $suite->repeat
l!l&&r!l&&!r
27054
MC/DC50%1/2
($self->repeat // $suite->repeat) // 1
$self->repeat $suite->repeat
Truth table66%2/3
A // B
A= $self->repeatB= $suite->repeat
ABresult
1X1
011
000
86 ti => $ti,
87 variant => $v,
88 suite => $suite,
89 repeat => 0 + ( $self->repeat // $suite->repeat // 1 ),
90 results => {},
91 };
92 }
93 }
94 }
95 56 $self->{_runs} = \@runs;
96 56 ▶ $self->_take_over( $self->resume ) if defined $self->resume;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->resume 8 48 56
97 51 return;
98 }
99  
100 228 ▶sub _engine {
Subroutine _engine: called CC=5 SCAR=18.1
101 228 my ( $self, $t, $case, $slot ) = @_;
102 228 ▶ return $t->{engine} unless ref $t->{engine} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $$t{"engine"} eq "CODE" 26 202 228
103 202 my $engine = $t->{engine}->( $case, $slot );
104 202 ▶ croak __PACKAGE__.': the engine code of target '.( $t->{target}{label} // '?' ).' returned no engine with chat_f'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($engine) and $engine->can("chat_f") 0 202 202
Condition20%1/5
builtin::blessed($engine) and $engine->can("chat_f")
!ll&&!rl&&r
00202
$$t{"target"}{"label"} // "?"
l!l
00
MC/DC0%0/2
builtin::blessed($engine) and $engine->can("chat_f")
builtin::blessed($engine) $engine->can("chat_f")
MC/DC0%0/1
$$t{"target"}{"label"} // "?"
$$t{"target"}{"label"}
Truth table33%1/3
A and B
A= builtin::blessed($engine)B= $engine->can("chat_f")
ABresult
0X0
100
111
Truth table0%0/2
A
A= $$t{"target"}{"label"}
Aresult
11
00
105 unless blessed $engine && $engine->can('chat_f');
106 202 return $engine;
107 }
108  
109 # the identity of a run in a report: target, variant and suite id, as the
110 # report shapes them (so a url loses its credentials on both sides alike)
111 22 ▶sub _key {
Subroutine _key: called CC=1 SCAR=0.0
112 22 my ( $self, $target, $variant, $suite_id ) = @_;
113 22 return $JSON->encode( [ $target, $variant, $suite_id ] );
114 }
115  
116 12 ▶sub _shaped {
Subroutine _shaped: called CC=2 SCAR=7.0
117 12 my ( $self, $run ) = @_;
118 12 my $scratch = $self->report_class->new;
119 my $shaped = $scratch->add_run(
120 target => $self->{_target}[ $run->{ti} ]{target} // {},
121 variant => $run->{variant},
122 suite => { id => $run->{suite}->id },
123 repeat => $run->{repeat},
124 12 ▶ status => 'complete',
Condition50%1/2
$$self{"_target"}[$$run{"ti"}]{"target"} // {}
l!l
120
MC/DC0%0/1
$$self{"_target"}[$$run{"ti"}]{"target"} // {}
$$self{"_target"}[$$run{"ti"}]{"target"}
Truth table50%1/2
A
A= $$self{"_target"}[$$run{"ti"}]{"target"}
Aresult
11
00
125 cases => [],
126 );
127 12 return $self->_key( $shaped->{target}, $shaped->{variant}, $run->{suite}->id );
128 }
129  
130 8 ▶sub _take_over {
Subroutine _take_over: called CC=15 SCAR=28.2
131 8 my ( $self, $old ) = @_;
132 8 ▶ croak __PACKAGE__.'->new: resume must be a '.$self->report_class
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($old) and $old->isa($self->report_class) 0 8 8
Condition33%1/3
builtin::blessed($old) and $old->isa($self->report_class)
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($old) and $old->isa($self->report_class)
builtin::blessed($old) $old->isa($self->report_class)
Truth table33%1/3
A and B
A= builtin::blessed($old)B= $old->isa($self->report_class)
ABresult
0X0
100
111
133 unless blessed $old && $old->isa( $self->report_class );
134 8 my %run = map { ( $self->_shaped($_) => $_ ) } @{ $self->{_runs} };
135 8 for my $o ( @{ $old->runs } ) {
136 10 my $sid = $o->{suite}{id};
137 10 ▶ my $name = 'run of suite '.$sid.', target '.( $o->{target}{label} // '?' ).', variant '.( $o->{variant}{name} // '?' );
Condition50%2/4
$$o{"target"}{"label"} // "?"
l!l
100
$$o{"variant"}{"name"} // "?"
l!l
100
MC/DC0%0/1
$$o{"target"}{"label"} // "?"
$$o{"target"}{"label"}
MC/DC0%0/1
$$o{"variant"}{"name"} // "?"
$$o{"variant"}{"name"}
Truth table50%1/2
A
A= $$o{"target"}{"label"}
Aresult
11
00
Truth table50%1/2
A
A= $$o{"variant"}{"name"}
Aresult
11
00
138 10 ▶ my $run = $run{ $self->_key( $o->{target}, $o->{variant}, $sid ) }
Branch: 1 branch
BranchTrueFalseTotal
my $run = $run{$self->_key($$o{"target"}, $$o{"variant"}, $sid)} or croak('Langertha::Holmgang::Batch->new: resume: the earlier report has a ' . $name . ' that this batch does not cover') 8 2 10
139 or croak __PACKAGE__.'->new: resume: the earlier report has a '.$name.' that this batch does not cover';
140 8 my $digest = $run->{suite}->digest;
141 croak __PACKAGE__.'->new: resume: suite '.$sid.' has changed since the earlier report (digest)'
142 8 ▶ unless defined $o->{suite}{digest} && defined $digest && $o->{suite}{digest} eq $digest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$o{"suite"}{"digest"} and defined $digest and $$o{"suite"}{"digest"} eq $digest 1 7 8
Condition50%3/6
defined $$o{"suite"}{"digest"} and defined $digest and $$o{"suite"}{"digest"} eq $digest
!ll&&!rl&&r
017
defined $$o{"suite"}{"digest"} and defined $digest
!ll&&!rl&&r
008
MC/DC33%1/3
defined $$o{"suite"}{"digest"} and defined $digest and $$o{"suite"}{"digest"} eq $digest
defined $$o{"suite"}{"digest"} defined $digest $$o{"suite"}{"digest"} eq $digest
Truth table50%2/4
A and B and C
A= defined $$o{"suite"}{"digest"}B= defined $digestC= $$o{"suite"}{"digest"} eq $digest
ABCresult
0XX0
10X0
1100
1111
143 croak __PACKAGE__.'->new: resume: the earlier '.$name.' has repeat '.$o->{repeat}.', this batch '.$run->{repeat}
144 7 ▶ unless $o->{repeat} == $run->{repeat};
Branch: 1 branch
BranchTrueFalseTotal
unless $$o{"repeat"} == $$run{"repeat"} 2 5 7
145 5 ▶ croak __PACKAGE__.'->new: resume: the earlier report holds the '.$name.' twice' if $run->{resumed}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$run{"resumed"}++ 0 5 5
146 5 my %case = map { $_->id => 1 } @{ $run->{suite}->cases };
147 5 for my $c ( @{ $o->{cases} } ) {
148 croak __PACKAGE__.'->new: resume: case '.$c->{case_id}.' of the earlier '.$name.' is not in the suite'
149 13 ▶ unless $case{ $c->{case_id} };
Branch: 1 branch
BranchTrueFalseTotal
unless $case{$$c{"case_id"}} 0 13 13
150 13 for my $r ( @{ $c->{repetitions} } ) {
151 13 $run->{results}{ $c->{case_id} }{ $r->{repetition} } = { %$r, case_id => $c->{case_id} };
152 }
153 }
154 }
155 3 $self->{_started_at} = $old->started_at;
156 3 return;
157 }
158  
159 #### Running
160  
161 2 ▶sub cancel { my ( $self ) = @_; $self->{_cancelled} = 1; return }
Subroutine cancel: called CC=1 SCAR=0.0
Pod documented
162  
163  
164 0 ▶sub cancelled { $_[0]->{_cancelled} ? 1 : 0 }
Subroutine cancelled: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'_cancelled'} ? : 0 0 0
165  
166  
167 50 ▶sub run_f {
Subroutine run_f: called CC=2 SCAR=7.0
Pod documented
168 50 my ( $self ) = @_;
169 50 ▶ croak __PACKAGE__.'->run_f: a batch runs once' if $self->{_run_f};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_run_f"} 0 50 50
170 50 my @per_target = map { $self->_target_f($_) } 0 .. $#{ $self->{_target} };
171 50 ▶ return $self->{_run_f} = Future->needs_all(@per_target)->then( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 50 $self->_finish;
173 50 return Future->done( $self->report );
174 50 } );
175 }
176  
177  
178 45 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
179 45 my ( $self ) = @_;
180 45 my $f = $self->run_f;
181 45 ▶ unless ( $f->is_ready ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($f->is_ready) 1 44 45
182 # requests on an event loop (Net::Async::HTTP): run the loop until done;
183 # IO::Async is optional, as it is for Langertha
184 1 ▶ my $loop = eval { require IO::Async::Loop; IO::Async::Loop->new }
Branch: 1 branch
BranchTrueFalseTotal
my $loop = eval { do { require IO::Async::Loop; "IO::Async::Loop"->new } } or croak("Langertha::Holmgang::Batch->run: the batch waits on requests and IO::Async is not available") 1 0 1
185 or croak __PACKAGE__.'->run: the batch waits on requests and IO::Async is not available';
186 1 $loop->await($f);
187 }
188 45 return $f->get;
189 }
190  
191  
192 51 ▶sub _target_f {
Subroutine _target_f: called CC=6 SCAR=17.9
193 51 my ( $self, $ti ) = @_;
194 51 my @jobs;
195 51 for my $run ( grep { $_->{ti} == $ti } @{ $self->{_runs} } ) {
196 72 for my $n ( 1 .. $run->{repeat} ) {
197 92 for my $case ( @{ $run->{suite}->cases } ) {
198 182 ▶ push @jobs, { run => $run, case => $case, n => $n } unless $run->{results}{ $case->id }{$n};
Branch: 1 branch
BranchTrueFalseTotal
unless $$run{"results"}{$case->id}{$n} 173 9 182
199 }
200 }
201 }
202 51 my @free = ( 0 .. $self->{_target}[$ti]{concurrency} - 1 );
203 51 ▶ return Future->done unless @jobs;
Branch: 1 branch
BranchTrueFalseTotal
unless @jobs 0 51 51
204 173 ▶ return fmap_void {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 173 my ( $job ) = @_;
206 173 my $slot = shift @free;
207 173 ▶ $self->_job_f( $job, $slot )->followed_by( sub { unshift @free, $slot; Future->done } );
Subroutine __ANON__: called CC=1 SCAR=0.0
208 51 } foreach => \@jobs, concurrent => $self->{_target}[$ti]{concurrency};
209 }
210  
211 # one case: never fails; a throw aborts its run
212 173 ▶sub _job_f {
Subroutine _job_f: called CC=6 SCAR=18.5
213 173 my ( $self, $job, $slot ) = @_;
214 173 my $run = $job->{run};
215 173 ▶ return Future->done if $self->{_cancelled} || $run->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_cancelled"} or $$run{"aborted"} 14 159 173
Condition100%3/3
$$self{"_cancelled"} or $$run{"aborted"}
l!l&&r!l&&!r
95159
MC/DC100%2/2
$$self{"_cancelled"} or $$run{"aborted"}
$$self{"_cancelled"} $$run{"aborted"}
Truth table100%3/3
A or B
A= $$self{"_cancelled"}B= $$run{"aborted"}
ABresult
1X1
011
000
216 159 my $f = eval { $self->_case_f( $job, $slot ) };
217 159 ▶ $f = Future->fail( $@ || 'the case returned no Future' ) unless blessed $f && $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("Future") 0 159 159
Condition20%1/5
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
00159
$@ || 'the case returned no Future'
l!l
00
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
MC/DC0%0/1
$@ || 'the case returned no Future'
$@
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
Truth table0%0/2
A
A= $@
Aresult
11
00
218 return $f->then(
219 155 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
220 155 my ( $result ) = @_;
221 155 ▶ return Future->done unless $result; # the run was aborted while it waited
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 155 155
222 155 my $ok = eval { $self->_record( $job, $result ); 1 };
223 155 ▶ $self->_abort( $job, $@ ) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 155 155
224 155 return Future->done;
225 },
226 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
227 4 my ( $err ) = @_;
228 4 $self->_abort( $job, $err );
229 4 return Future->done;
230 },
231 159 );
232 }
233  
234 159 ▶async sub _case_f {
Subroutine _case_f: called CC=3 SCAR=11.0
235 159 my ( $self, $job, $slot ) = @_;
236 159 my $run = $job->{run};
237 159 my $no_vision = await $self->_vision_f( $run, $slot );
238 159 ▶ return if $run->{aborted};
Branch: 1 branch
BranchTrueFalseTotal
if $$run{"aborted"} 0 159 159
239 159 my $t = $self->{_target}[ $run->{ti} ];
240 my $target = $job->{target} = $self->target_class->for_variant(
241 engine => $self->_engine( $t, $job->{case}, $slot ),
242 settings => $run->{variant}{settings},
243 159 );
244 159 ▶ my $runner = $self->runner_class->new( target => $target, ( $self->sandbox ? ( sandbox => $self->sandbox ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->sandbox ? : 1 158 159
245 159 return await $runner->run_case_f( $job->{case}, repetition => $job->{n}, no_vision => $no_vision );
246 18 }
247  
248 # the vision pre-check of a run, once, before its cases (k27): a Future of
249 # the no_vision reason or undef; a run without images asks nothing
250 159 ▶sub _vision_f {
Subroutine _vision_f: called CC=6 SCAR=19.4
251 159 my ( $self, $run, $slot ) = @_;
252 159 ▶ return $run->{vision_f} //= do {
Condition66%2/3
$$run{"vision_f"} //= do { my($seeing) = grep({$_->has_images;} @{$$run{"suite"}->cases;}); if ($seeing) { my $f = eval { do { my $target = ($$run{"vision_target"} = $self->target_class->for_variant("engine", $self->_engine($$self{"_target"}[$$run{"ti"}], $seeing, $slot), "settings", $$run{"variant"}{"settings"})); $target->vision_check_f } }; $f = "Future"->fail($@ || "the vision check returned no Future") unless builtin::blessed($f) and $f->isa("Future"); $f->then(sub { my($check) = @_; $self->_event($run, "type", "vision", "check", $check); return "Future"->done($$check{"no_vision"}); } , sub { my($err) = @_; $self->_abort({"run", $run}, $err); return "Future"->done(undef); } ); } else { "Future"->done(undef); } }
l!l&&r!l&&!r
88710
MC/DC0%0/2
$$run{"vision_f"} //= do { my($seeing) = grep({$_->has_images;} @{$$run{"suite"}->cases;}); if ($seeing) { my $f = eval { do { my $target = ($$run{"vision_target"} = $self->target_class->for_variant("engine", $self->_engine($$self{"_target"}[$$run{"ti"}], $seeing, $slot), "settings", $$run{"variant"}{"settings"})); $target->vision_check_f } }; $f = "Future"->fail($@ || "the vision check returned no Future") unless builtin::blessed($f) and $f->isa("Future"); $f->then(sub { my($check) = @_; $self->_event($run, "type", "vision", "check", $check); return "Future"->done($$check{"no_vision"}); } , sub { my($err) = @_; $self->_abort({"run", $run}, $err); return "Future"->done(undef); } ); } else { "Future"->done(undef); } }
$$run{"vision_f"} do { my($seeing) = grep({$_->has_images;} @{$$run{"suite"}->cases;}); if ($seeing) { my $f = eval { do { my $target = ($$run{"vision_target"} = $self->target_class->for_variant("engine", $self->_engine($$self{"_target"}[$$run{"ti"}], $seeing, $slot), "settings", $$run{"variant"}{"settings"})); $target->vision_check_f } }; $f = "Future"->fail($@ || "the vision check returned no Future") unless builtin::blessed($f) and $f->isa("Future"); $f->then(sub { my($check) = @_; $self->_event($run, "type", "vision", "check", $check); return "Future"->done($$check{"no_vision"}); } , sub { my($err) = @_; $self->_abort({"run", $run}, $err); return "Future"->done(undef); } ); } else { "Future"->done(undef); } }
Truth table66%2/3
A //= B
A= $$run{"vision_f"}B= do { my($seeing) = grep({$_->has_images;} @{$$run{"suite"}->cases;}); if ($seeing) { my $f = eval { do { my $target = ($$run{"vision_target"} = $self->target_class->for_variant("engine", $self->_engine($$self{"_target"}[$$run{"ti"}], $seeing, $slot), "settings", $$run{"variant"}{"settings"})); $target->vision_check_f } }; $f = "Future"->fail($@ || "the vision check returned no Future") unless builtin::blessed($f) and $f->isa("Future"); $f->then(sub { my($check) = @_; $self->_event($run, "type", "vision", "check", $check); return "Future"->done($$check{"no_vision"}); } , sub { my($err) = @_; $self->_abort({"run", $run}, $err); return "Future"->done(undef); } ); } else { "Future"->done(undef); } }
ABresult
1X1
011
000
253 71 my ( $seeing ) = grep { $_->has_images } @{ $run->{suite}->cases };
254 71 ▶ if ( $seeing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seeing) { } 5 66 71
255 5 my $f = eval {
256 my $target = $run->{vision_target} = $self->target_class->for_variant(
257 engine => $self->_engine( $self->{_target}[ $run->{ti} ], $seeing, $slot ),
258 settings => $run->{variant}{settings},
259 5 );
260 5 $target->vision_check_f;
261 };
262 5 ▶ $f = Future->fail( $@ || 'the vision check returned no Future' ) unless blessed $f && $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("Future") 0 5 5
Condition20%1/5
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
005
$@ || 'the vision check returned no Future'
l!l
00
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
MC/DC0%0/1
$@ || 'the vision check returned no Future'
$@
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
Truth table0%0/2
A
A= $@
Aresult
11
00
263 $f->then(
264 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 5 my ( $check ) = @_;
266 5 $self->_event( $run, type => 'vision', check => $check );
267 5 return Future->done( $check->{no_vision} );
268 },
269 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
270 0 my ( $err ) = @_;
271 0 $self->_abort( { run => $run }, $err );
272 0 return Future->done(undef);
273 },
274 5 );
275 }
276 else {
277 66 Future->done(undef);
278 }
279 };
280 }
281  
282 159 ▶sub _cut {
Subroutine _cut: called CC=3 SCAR=11.0
283 159 my ( $self, $text ) = @_;
284 159 ▶ return $text unless defined $text && length $text > $self->error_max_length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text > $self->error_max_length 157 2 159
Condition100%3/3
defined $text and length $text > $self->error_max_length
!ll&&!rl&&r
135222
MC/DC100%2/2
defined $text and length $text > $self->error_max_length
defined $text length $text > $self->error_max_length
Truth table100%3/3
A and B
A= defined $textB= length $text > $self->error_max_length
ABresult
0X0
100
111
285 2 return substr( $text, 0, $self->error_max_length );
286 }
287  
288 155 ▶sub _record {
Subroutine _record: called CC=1 SCAR=0.0
289 155 my ( $self, $job, $result ) = @_;
290 155 my $run = $job->{run};
291 155 my %keep = %$result;
292 155 $keep{error} = $self->_cut( $keep{error} );
293 # what a report never holds stays out of the batch's memory: the end state,
294 # the last text, the transcript
295 155 delete @keep{qw( state answer transcript )};
296 155 $run->{results}{ $job->{case}->id }{ $job->{n} } = \%keep;
297 155 $self->_event( $run, type => 'result', case => $job->{case}, repetition => $job->{n}, result => $result );
298 155 return;
299 }
300  
301 # a throw aborts the run, once; the text through the target's scrub (with the
302 # engine's key where a target was built), cut (ADR 0006 amendment k38)
303 4 ▶sub _abort {
Subroutine _abort: called CC=7 SCAR=21.3
304 4 my ( $self, $job, $err ) = @_;
305 4 my $run = $job->{run};
306 4 ▶ return if $run->{aborted}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$run{"aborted"}++ 0 4 4
307 4 ▶ my $text = defined $err ? "$err" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 4 0 4
308 4 ▶ $text = 'unknown error' unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 4 4
309 4 ▶ my $scrubber = $job->{target} // $run->{vision_target} // $self->target_class;
Condition33%2/6
($$job{"target"} // $$run{"vision_target"}) // $self->target_class
l!l&&r!l&&!r
400
$$job{"target"} // $$run{"vision_target"}
l!l&&r!l&&!r
400
MC/DC0%0/3
($$job{"target"} // $$run{"vision_target"}) // $self->target_class
$$job{"target"} $$run{"vision_target"} $self->target_class
Truth table25%1/4
A // B // C
A= $$job{"target"}B= $$run{"vision_target"}C= $self->target_class
ABCresult
1XX1
01X1
0011
0000
310 4 $text = $scrubber->scrub($text) =~ s/\s+\z//r;
311 4 $run->{error} = $self->_cut($text);
312 4 eval {
313 $self->_event( $run, type => 'aborted', error => $run->{error},
314 4 ▶ ( $job->{case} ? ( case => $job->{case}, repetition => $job->{n} ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$$job{'case'} ? : 4 0 4
315 4 1;
316 };
317 4 return;
318 }
319  
320 164 ▶sub _event {
Subroutine _event: called CC=2 SCAR=6.9
321 164 my ( $self, $run, %e ) = @_;
322 164 ▶ my $cb = $self->on_event or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = $self->on_event or return 124 40 164
323 $cb->( { %e,
324 target => $self->{_target}[ $run->{ti} ]{target},
325 variant => $run->{variant}{name},
326 suite => $run->{suite},
327 124 } );
328 124 return;
329 }
330  
331 68 ▶sub _missing {
Subroutine _missing: called CC=2 SCAR=6.9
332 68 my ( $self, $run ) = @_;
333 68 my $n = 0;
334 68 for my $case ( @{ $run->{suite}->cases } ) {
335 130 $n += grep { !$run->{results}{ $case->id }{$_} } 1 .. $run->{repeat};
336 }
337 68 return $n;
338 }
339  
340 50 ▶sub _finish {
Subroutine _finish: called CC=4 SCAR=13.9
341 50 my ( $self ) = @_;
342 50 for my $run ( @{ $self->{_runs} } ) {
343 72 ▶ $run->{status} = $run->{aborted} ? 'aborted'
Branch: 2 branches
BranchTrueFalseTotal
$$run{'aborted'} ? : 4 68 72
$self->_missing($run) ? : 2 66 68
344 : $self->_missing($run) ? 'cancelled'
345 : 'complete';
346 }
347 50 $self->{_finished} = 1;
348 50 return;
349 }
350  
351 #### The report
352  
353 51 ▶sub report {
Subroutine report: called CC=12 SCAR=24.9
Pod documented
354 51 my ( $self ) = @_;
355 51 ▶ my $report = $self->report_class->new( defined $self->{_started_at} ? ( started_at => $self->{_started_at} ) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{'_started_at'} ? : 3 48 51
356 51 for my $run ( @{ $self->{_runs} } ) {
357 73 my $suite = $run->{suite};
358 73 ▶ my $status = $run->{status} // ( $run->{aborted} ? 'aborted' : undef );
Branch: 1 branch
BranchTrueFalseTotal
$$run{'aborted'} ? : 0 1 1
Condition100%2/2
$$run{"status"} // ($$run{"aborted"} ? "aborted" : undef)
l!l
721
MC/DC100%1/1
$$run{"status"} // ($$run{"aborted"} ? "aborted" : undef)
$$run{"status"}
Truth table100%2/2
A
A= $$run{"status"}
Aresult
11
00
359 $report->add_run(
360 target => $self->{_target}[ $run->{ti} ]{target} // {},
361 variant => $run->{variant},
362 suite => { id => $suite->id, version => $suite->version, topic => $suite->topic, digest => $suite->digest },
363 repeat => $run->{repeat},
364 status => $status // 'aborted',
365 error => defined $status ? $run->{error} : 'unfinished: the batch was still running when this report was written',
366 141 cases => [ map { $_->report_meta } @{ $suite->cases } ],
367 results => [ map {
368 141 my $c = $_;
369 141 ▶ map { $run->{results}{ $c->id }{$_} // () } 1 .. $run->{repeat};
Condition66%2/3
$$run{"results"}{$c->id}{$_} // ()
l!l&&r!l&&!r
165190
MC/DC0%0/2
$$run{"results"}{$c->id}{$_} // ()
$$run{"results"}{$c->id}{$_} ()
Truth table66%2/3
A // B
A= $$run{"results"}{$c->id}{$_}B= ()
ABresult
1X1
011
000
370 73 ▶ } @{ $suite->cases } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 72 1 73
Condition100%4/4
$$self{"_target"}[$$run{"ti"}]{"target"} // {}
l!l
6211
$status // "aborted"
l!l
721
MC/DC100%1/1
$$self{"_target"}[$$run{"ti"}]{"target"} // {}
$$self{"_target"}[$$run{"ti"}]{"target"}
MC/DC100%1/1
$status // "aborted"
$status
Truth table100%2/2
A
A= $$self{"_target"}[$$run{"ti"}]{"target"}
Aresult
11
00
Truth table100%2/2
A
A= $status
Aresult
11
00
371 );
372 }
373 51 ▶ $report->generator->{sandbox} = $self->sandbox_label // ref $self->sandbox if $self->sandbox;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sandbox 1 50 51
Condition50%1/2
$self->sandbox_label // ref $self->sandbox
l!l
10
MC/DC0%0/1
$self->sandbox_label // ref $self->sandbox
$self->sandbox_label
Truth table50%1/2
A
A= $self->sandbox_label
Aresult
11
00
374 51 ▶ $report->finish if $self->{_finished};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_finished"} 50 1 51
375 51 return $report;
376 }
377  
378  
379 18 __PACKAGE__->meta->make_immutable;
380  
381 18 1;
382  
383 __END__
« blib/lib/Langertha/Holmgang/Haystack.pm ↑ Summary blib/lib/Langertha/Holmgang/Report.pm »