blib/lib/Langertha/Holmgang/Batch.pm

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