blib/lib/App/TestOnTap/Dispenser.pm

filter: Statementstmt 93.1% 135 / 145 Branchbran 76.1% 32 / 42 Conditioncond 85.0% 17 / 20 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 3 totaltotal 86.8% 205 / 236 SCAR 37.9
CC
42
Cov
89%
__analyze
22.5
getEligibleTests
16.0
__ANON__
6.0
CRAP 44.4
« blib/lib/App/TestOnTap/Util.pm ↑ Summary blib/lib/App/TestOnTap/ParallelGroupManager.pm »
1 package App::TestOnTap::Dispenser;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use App::TestOnTap::Util qw(slashify);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use App::TestOnTap::OrderStrategy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 19 ▶use List::Util qw(shuffle max);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # CTOR
13 #
14 sub new
15 {
16 27 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 27 my $args = shift;
18  
19 27 my $self = bless( { args => $args, inprogress => {}, orderstrategy => App::TestOnTap::OrderStrategy->new() }, $class);
20 27 $self->__analyze();
21
22 26 return $self;
23 }
24  
25 sub __analyze
26 {
27 27 ▶ my $self = shift;
Subroutine __analyze: called CC=15 SCAR=31.1
28  
29 # find all tests in the suite root
30 # (subject to config skip filtering)
31 #
32 27 my $tests = $self->__scan();
33  
34 # create a graph with all the tests as 'from' vertices, begin with no dependencies
35 #
36 27 my %graph = map { $_ => [] } @$tests;
37
38 # figure out the longest dep rule name with some extra to produce some
39 # nice delimiters...
40 #
41 27 my @depRuleNames = $self->{args}->getConfig()->getDependencyRuleNames();
42 27 my $longestDepRuleName = 0;
43 27 $longestDepRuleName = max($longestDepRuleName, length($_)) foreach (@depRuleNames);
44 27 $longestDepRuleName += 18;
45 27 my $topDelimLine = '*' x $longestDepRuleName;
46  
47 # iterate over all dependency rules and add edges from => to vertices
48 #
49 27 foreach my $depRuleName (@depRuleNames)
50 {
51 5 my ($fromVertices, $toVertices) = $self->{args}->getConfig()->getMatchesAndDependenciesForRule($depRuleName, $tests);
52 5 ▶ if ($self->{args}->doDryRun())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'args'}->doDryRun) { } 0 5 5
53 {
54 0 print "$topDelimLine\n";
55 0 print "Dependency rule '$depRuleName'\n";
56 0 print "Query for 'match' matches:\n";
57 0 ▶ if (@$fromVertices)
Branch: 1 branch
BranchTrueFalseTotal
if (@$fromVertices) { } 0 0 0
58 {
59 0 print " $_\n" foreach (@$fromVertices);
60 }
61 else
62 {
63 0 print " (nothing)\n";
64 }
65 0 print "Query for 'dependson' matches:\n";
66 0 ▶ if (@$toVertices)
Branch: 1 branch
BranchTrueFalseTotal
if (@$toVertices) { } 0 0 0
67 {
68 0 print " $_\n" foreach (@$toVertices);
69 }
70 else
71 {
72 0 print "(nothing)\n";
73 }
74 }
75 else
76 {
77 5 ▶ warn("WARNING: No tests selected by 'match' in dependency rule '$depRuleName'\n") unless @$fromVertices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$fromVertices 0 5 5
78 5 ▶ warn("WARNING: No tests selected by 'dependson' in dependency rule '$depRuleName'\n") unless @$toVertices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$toVertices 0 5 5
79 }
80 5 push(@{$graph{$_}}, @$toVertices) foreach (@$fromVertices);
81 }
82
83 27 $self->{args}->getWorkDirManager()->recordFullGraph(%graph);
84
85 # trap any cyclic dependency problems right now
86 #
87 27 $self->__toposort(\%graph);
88  
89 # tentatively store the full graph
90 #
91 26 $self->{graph} = \%graph;
92
93 # if user decided to supply a skip filter, now try to create a
94 # graph filtered to matching tests, but without including dependencies...
95 #
96 26 my $prunedTests = $self->{args}->include($tests);
97
98 # ...but now make sure dependencies are brought along whether
99 # they we're filtered in or not...
100 #
101 26 ▶ if ($prunedTests && scalar(@$prunedTests) != scalar(@$tests))
Branch: 1 branch
BranchTrueFalseTotal
if ($prunedTests and scalar @$prunedTests != scalar @$tests) 2 24 26
Condition66%2/3
$prunedTests and scalar @$prunedTests != scalar @$tests
!ll&&!rl&&r
2402
MC/DC50%1/2
$prunedTests and scalar @$prunedTests != scalar @$tests
$prunedTests scalar @$prunedTests != scalar @$tests
Truth table66%2/3
A and B
A= $prunedTestsB= scalar @$prunedTests != scalar @$tests
ABresult
0X0
100
111
102 {
103 2 my %prunedGraph;
104  
105 # iteratively pick up all tests and dependencies
106 # as long as we're picking up new deps, we need to iterate again
107 #
108 2 my @newDeps = @$prunedTests;
109 2 ▶ while (my $t = shift(@newDeps))
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = shift @newDeps) 2 2 4
110 {
111 2 ▶ if (!exists($prunedGraph{$t}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $prunedGraph{$t}) 2 0 2
112 {
113 2 $prunedGraph{$t} = $graph{$t};
114 2 push(@newDeps, @{$prunedGraph{$t}});
115 }
116 }
117
118 # store the pruned graph instead
119 #
120 2 $self->{graph} = \%prunedGraph;
121
122 2 $self->{args}->getWorkDirManager()->recordPrunedGraph(%prunedGraph);
123 }
124 }
125  
126 sub getAllTests
127 {
128 26 ▶ my $self = shift;
Subroutine getAllTests: called CC=1 SCAR=0.0
Pod undocumented
129
130 26 return keys(%{$self->{graph}});
131 }
132  
133 sub getEligibleTests
134 {
135 81 ▶ my $self = shift;
Subroutine getEligibleTests: called CC=16 SCAR=27.7
Pod undocumented
136 81 ▶ my $completed = shift || [];
Condition50%1/2
shift() || []
l!l
810
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
137  
138 # remove items that have been completed from the graph
139 #
140 81 foreach my $t (@$completed)
141 {
142 48 delete($self->{graph}->{$t});
143 48 delete($self->{inprogress}->{$t});
144 }
145  
146 # no more items to run at all - we're finished
147 #
148 81 ▶ return unless keys(%{$self->{graph}});
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"graph"};} 26 55 81
149  
150 # if we're still here, remove any references to completed tests
151 # from the remaining tests
152 #
153 55 foreach my $removed (@$completed)
154 {
155 22 foreach my $t (keys(%{$self->{graph}}))
156 {
157 32 $self->{graph}->{$t} = [ grep( !/^\Q$removed\E$/, @{$self->{graph}->{$t}} ) ];
158 }
159 }
160  
161 # extract those ready to run and separate them into parallelizable and not parallelizable
162 #
163 55 my @parallelizable;
164 my @nonParallelizable;
165 55 foreach my $t (keys(%{$self->{graph}}))
166 {
167 # tests that have no dependencies and are not already in progress are
168 # now ready to run
169 #
170 98 ▶ if (!@{$self->{graph}->{$t}} && !$self->{inprogress}->{$t})
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"graph"}{$t};} or $self->{"inprogress"}{$t}) 82 16 98
Condition100%3/3
@{$self->{"graph"}{$t};} or $self->{"inprogress"}{$t}
l!l&&r!l&&!r
41282
MC/DC100%2/2
@{$self->{"graph"}{$t};} or $self->{"inprogress"}{$t}
@{$self->{"graph"}{$t};} $self->{"inprogress"}{$t}
Truth table100%3/3
A or B
A= @{$self->{"graph"}{$t};}B= $self->{"inprogress"}{$t}
ABresult
1X1
011
000
171 {
172 82 ▶ if ($self->{args}->getConfig()->parallelizable($t))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'args'}->getConfig->parallelizable($t)) { } 6 76 82
173 {
174 6 push(@parallelizable, $t);
175 }
176 else
177 {
178 76 push(@nonParallelizable, $t);
179 }
180 }
181 }
182  
183 # order them according to the chosen strategy
184 #
185 55 ▶ my $orderstrategy = $self->{args}->getOrderStrategy() || $self->{args}->getConfig()->getOrderStrategy() || $self->{orderstrategy};
Condition83%5/6
$self->{'args'}->getOrderStrategy || $self->{'args'}->getConfig->getOrderStrategy || $self->{'orderstrategy'}
l!l&&r!l&&!r
18370
$self->{"args"}->getOrderStrategy or $self->{"args"}->getConfig->getOrderStrategy
l!l&&r!l&&!r
12637
MC/DC0%0/3
$self->{'args'}->getOrderStrategy || $self->{'args'}->getConfig->getOrderStrategy || $self->{'orderstrategy'}
$self->{"args"}->getOrderStrategy $self->{"args"}->getConfig->getOrderStrategy $self->{'orderstrategy'}
Truth table75%3/4
A or B || C
A= $self->{"args"}->getOrderStrategyB= $self->{"args"}->getConfig->getOrderStrategyC= $self->{'orderstrategy'}
ABCresult
1XX1
01X1
0011
0000
186 55 $self->{args}->getWorkDirManager()->recordOrderStrategy($orderstrategy);
187 55 @parallelizable = $orderstrategy->orderList(@parallelizable);
188 55 @nonParallelizable = $orderstrategy->orderList(@nonParallelizable);
189
190 # check the parallel groups for max concurrency and cull the overflow (don't forget to account for in progress jobs)
191 #
192 55 @parallelizable = $self->{args}->getConfig()->getParallelGroupManager()->cull([ keys(%{$self->{inprogress}}) ], \@parallelizable);
193
194 # now finally select those eligible - try to do away with parallelizable first
195 #
196 55 my @eligible = @parallelizable;
197
198 # we only deal with non-parallelizables if:
199 # - nothing else already is eligible
200 # - there are any to deal out at all...
201 # - nothing else is presently in progress
202 # if so, just pick the first
203 #
204 55 ▶ if (!@eligible && @nonParallelizable && !keys(%{$self->{inprogress}}))
Branch: 1 branch
BranchTrueFalseTotal
if (not @eligible and @nonParallelizable and not keys %{$$self{"inprogress"};}) 42 13 55
Condition100%6/6
not @eligible and @nonParallelizable and not keys %{$$self{"inprogress"};}
!ll&&!rl&&r
7642
not @eligible and @nonParallelizable
!ll&&!rl&&r
2548
MC/DC100%3/3
not @eligible and @nonParallelizable and not keys %{$$self{"inprogress"};}
not @eligible @nonParallelizable not keys %{$$self{"inprogress"};}
Truth table100%4/4
not(A) and B and not(C)
A= not @eligibleB= @nonParallelizableC= not keys %{$$self{"inprogress"};}
ABCresult
0XX0
10X0
1100
1111
205 {
206 42 @eligible = $nonParallelizable[0];
207 }
208
209 # make a note that those we return are in progress
210 #
211 55 $self->{inprogress}->{$_} = 1 foreach (@eligible);
212  
213 55 $self->{args}->getWorkDirManager()->recordDispensedOrder(@eligible);
214  
215 55 return \@eligible;
216 }
217  
218 # SUB helpers
219 #
220  
221 # scan the suite root and find all tests
222 # (subject to the config skip filter)
223 #
224 sub __scan
225 {
226 27 ▶ my $self = shift;
Subroutine __scan: called CC=1 SCAR=0.0
227  
228 27 my $config = $self->{args}->getConfig();
229
230 27 my @tests;
231
232 # to simplify during preprocessing, ensure we have a suite root using forward slashes
233 #
234 27 my $srfs = slashify($self->{args}->getSuiteRoot(), '/');
235
236 # set up a File::Find preprocessor in order to weed out parts of the scanned tree
237 # that are selected by the optional config skip filter
238 #
239 my $preprocess =
240 sub
241 {
242 # stash entries that should be further processed here
243 #
244 27 ▶ my @keep;
Subroutine __ANON__: called CC=6 SCAR=17.9
245
246 27 foreach my $entry (@_)
247 {
248 # skip the '.' and '..' entries
249 #
250 139 ▶ next if $entry =~ /^\.\.?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\.\.?$/ 54 85 139
251
252 # skip any config entries
253 #
254 85 ▶ next if $entry eq $config->getName();
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq $config->getName 27 58 85
255
256 # skip entries that are not selected by the config filter
257 # however, filters are written assuming they're passed paths
258 # relative from the suiteroot, and delimited by '/' so construct strings
259 # to conform by first normalizing and then stripping the absolute part
260 # to the suite root; ensure dirs are suffixed by a '/'.
261 #
262 58 my $p = slashify("$File::Find::dir/$entry", '/');
263 58 ▶ $p .= '/' if -d $p;
Branch: 1 branch
BranchTrueFalseTotal
if -d $p 0 58 58
264 58 $p =~ s#^\Q$srfs\E/##;
265 58 ▶ next if $config->skip($p);
Branch: 1 branch
BranchTrueFalseTotal
if $config->skip($p) 2 56 58
266
267 56 push(@keep, $entry);
268 }
269
270 # return the list of entries that should be further processed by the wanted function
271 #
272 27 return @keep;
273 27 };
274
275 # set up a wanted processor to select tests based on the execmapper
276 #
277 my $wanted =
278 sub
279 {
280 # normalize the full name
281 #
282 83 ▶ my $fn = slashify($File::Find::name, '/');
Subroutine __ANON__: called CC=3 SCAR=11.0
283
284 # ignore any directories (they can't be tests anyway)
285 #
286 83 ▶ return if -d $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -d $fn 27 56 83
287
288 # normalize to test name
289 #
290 56 $fn =~ s#^\Q$srfs\E/##;
291  
292 # ignore files with extensions not handled by the exec mapper
293 #
294 56 ▶ return unless $config->hasExecMapping($fn);
Branch: 1 branch
BranchTrueFalseTotal
unless $config->hasExecMapping($fn) 2 54 56
295  
296 # store it as a test!
297 #
298 54 push(@tests, $fn);
299 27 };
300
301 # execute the find
302 #
303 27 find( { preprocess => $preprocess, wanted => $wanted }, $srfs);
304
305 27 $self->{args}->getWorkDirManager()->recordFoundTests(@tests);
306
307 27 return \@tests;
308 }
309  
310 # essentially follows the algorithm for depth-first search as described
311 # at https://en.wikipedia.org/wiki/Topological_sorting
312 #
313 # minor change is that since we will use the toposort
314 # bottom-up, we push to tail of L instead of unshift to head
315 #
316 sub __toposort
317 {
318 27 ▶ my $self = shift;
Subroutine __toposort: called CC=3 SCAR=11.0
319 27 my $graph = shift;
320
321 27 my ($unmarked, $tmpmarked, $permmarked) = (0, 1, 2);
322 27 my @keys = keys(%$graph);
323 27 my %g = map { $_ => { deps => $graph->{$_}, mark => $unmarked } } @keys;
324  
325 27 my @sorted;
326  
327 my $visitor;
328 $visitor =
329 sub
330 {
331 55 ▶ my $node = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
332 55 my @route = @_;
333
334 55 ▶ die("ERROR: Cyclic dependency detected: " . join(' => ', @route, $node) . "!\n") if ($g{$node}->{mark} == $tmpmarked);
Branch: 1 branch
BranchTrueFalseTotal
if $g{$node}{"mark"} == $tmpmarked 1 54 55
335  
336 54 ▶ return if $g{$node}->{mark};
Branch: 1 branch
BranchTrueFalseTotal
if $g{$node}{"mark"} 0 54 54
337
338 54 $g{$node}->{mark} = $tmpmarked;
339 54 $visitor->($_, @route, $node) foreach (@{$g{$node}->{deps}});
340 52 $g{$node}->{mark} = $permmarked;
341 52 push(@sorted, $node);
342 27 };
343
344 27 foreach my $node (@keys)
345 {
346 53 ▶ next unless $g{$node}->{mark} == $unmarked;
Branch: 1 branch
BranchTrueFalseTotal
unless $g{$node}{"mark"} == $unmarked 3 50 53
347 50 $visitor->($node);
348 }
349  
350 26 return @sorted;
351 }
352  
353 19 1;
« blib/lib/App/TestOnTap/Util.pm ↑ Summary blib/lib/App/TestOnTap/ParallelGroupManager.pm »