blib/lib/TAP/Parser/Scheduler.pm

filter: Statementstmt 90.5% 115 / 127 Branchbran 84.2% 59 / 70 Conditioncond 80.0% 16 / 20 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 90.4% 19 / 21 Podpod 100% 4 / 4 totaltotal 87.4% 222 / 254 CC 46 SCAR 20.6
CC
46
Cov
88%
_as_string
30.0
_glob_to_regexp
12.0
_find_next_job
9.0
CRAP 7.8
« blib/lib/TAP/Formatter/File/Session.pm ↑ Summary blib/lib/TAP/Parser/YAMLish/Reader.pm »
1 package TAP::Parser::Scheduler;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use TAP::Parser::Scheduler::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use TAP::Parser::Scheduler::Spinner;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 TAP::Parser::Scheduler - Schedule tests during parallel testing
13  
14 =head1 VERSION
15  
16 Version 3.52
17  
18 =cut
19  
20 16 our $VERSION = '3.52';
21  
22 =head1 SYNOPSIS
23  
24 use TAP::Parser::Scheduler;
25  
26 =head1 DESCRIPTION
27  
28 =head1 METHODS
29  
30 =head2 Class Methods
31  
32 =head3 C<new>
33  
34 my $sched = TAP::Parser::Scheduler->new(tests => \@tests);
35 my $sched = TAP::Parser::Scheduler->new(
36 tests => [ ['t/test_name.t','Test Description'], ... ],
37 rules => \%rules,
38 );
39  
40 Given 'tests' and optional 'rules' as input, returns a new
41 C<TAP::Parser::Scheduler> object. Each member of C<@tests> should be either a
42 a test file name, or a two element arrayref, where the first element is a test
43 file name, and the second element is a test description. By default, we'll use
44 the test name as the description.
45  
46 The optional C<rules> attribute provides direction on which tests should be run
47 in parallel and which should be run sequentially. If no rule data structure is
48 provided, a default data structure is used which makes every test eligible to
49 be run in parallel:
50  
51 { par => '**' },
52  
53 The rules data structure is documented more in the next section.
54  
55 =head2 Rules data structure
56  
57 The "C<rules>" data structure is the the heart of the scheduler. It allows you
58 to express simple rules like "run all tests in sequence" or "run all tests in
59 parallel except these five tests.". However, the rules structure also supports
60 glob-style pattern matching and recursive definitions, so you can also express
61 arbitrarily complicated patterns.
62  
63 The rule must only have one top level key: either 'par' for "parallel" or 'seq'
64 for "sequence".
65  
66 Values must be either strings with possible glob-style matching, or arrayrefs
67 of strings or hashrefs which follow this pattern recursively.
68  
69 Every element in an arrayref directly below a 'par' key is eligible to be run
70 in parallel, while values directly below a 'seq' key must be run in sequence.
71  
72 =head3 Rules examples
73  
74 Here are some examples:
75  
76 # All tests be run in parallel (the default rule)
77 { par => '**' },
78  
79 # Run all tests in sequence, except those starting with "p"
80 { par => 't/p*.t' },
81  
82 # Run all tests in parallel, except those starting with "p"
83 {
84 seq => [
85 { seq => 't/p*.t' },
86 { par => '**' },
87 ],
88 }
89  
90 # Run some startup tests in sequence, then some parallel tests then some
91 # teardown tests in sequence.
92 {
93 seq => [
94 { seq => 't/startup/*.t' },
95 { par => ['t/a/*.t','t/b/*.t','t/c/*.t'], }
96 { seq => 't/shutdown/*.t' },
97 ],
98 },
99  
100  
101 =head3 Rules resolution
102  
103 =over 4
104  
105 =item * By default, all tests are eligible to be run in parallel. Specifying any of your own rules removes this one.
106  
107 =item * "First match wins". The first rule that matches a test will be the one that applies.
108  
109 =item * Any test which does not match a rule will be run in sequence at the end of the run.
110  
111 =item * The existence of a rule does not imply selecting a test. You must still specify the tests to run.
112  
113 =item * Specifying a rule to allow tests to run in parallel does not make the run in parallel. You still need specify the number of parallel C<jobs> in your Harness object.
114  
115 =back
116  
117 =head3 Glob-style pattern matching for rules
118  
119 We implement our own glob-style pattern matching. Here are the patterns it supports:
120  
121 ** is any number of characters, including /, within a pathname
122 * is zero or more characters within a filename/directory name
123 ? is exactly one character within a filename/directory name
124 {foo,bar,baz} is any of foo, bar or baz.
125 \ is an escape character
126  
127 =cut
128  
129 87 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
130 87 my $class = shift;
131  
132 87 ▶ croak "Need a number of key, value pairs" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 87 87
133  
134 87 my %args = @_;
135 87 ▶ my $tests = delete $args{tests} || croak "Need a 'tests' argument";
Condition50%1/2
delete $args{'tests'} || croak(q[Need a 'tests' argument])
l!l
870
MC/DC0%0/1
delete $args{'tests'} || croak(q[Need a 'tests' argument])
delete $args{'tests'}
Truth table50%1/2
A
A= delete $args{'tests'}
Aresult
11
00
136 87 ▶ my $rules = delete $args{rules} || { par => '**' };
Condition100%2/2
delete $args{'rules'} || {'par', '**'}
l!l
879
MC/DC100%1/1
delete $args{'rules'} || {'par', '**'}
delete $args{'rules'}
Truth table100%2/2
A
A= delete $args{'rules'}
Aresult
11
00
137  
138 87 ▶ croak "Unknown arg(s): ", join ', ', sort keys %args
Branch: 1 branch
BranchTrueFalseTotal
if keys %args 0 87 87
139 if keys %args;
140  
141 # Turn any simple names into a name, description pair. TODO: Maybe
142 # construct jobs here?
143 87 my $self = bless {}, $class;
144  
145 87 $self->_set_rules( $rules, $tests );
146  
147 87 return $self;
148 }
149  
150 # Build the scheduler data structure.
151 #
152 # SCHEDULER-DATA ::= JOB
153 # || ARRAY OF ARRAY OF SCHEDULER-DATA
154 #
155 # The nested arrays are the key to scheduling. The outer array contains
156 # a list of things that may be executed in parallel. Whenever an
157 # eligible job is sought any element of the outer array that is ready to
158 # execute can be selected. The inner arrays represent sequential
159 # execution. They can only proceed when the first job is ready to run.
160  
161 87 ▶sub _set_rules {
Subroutine _set_rules: called CC=3 SCAR=11.0
162 87 my ( $self, $rules, $tests ) = @_;
163  
164 # Convert all incoming tests to job objects.
165 # If no test description is provided use the file name as the description.
166 503 my @tests = map { TAP::Parser::Scheduler::Job->new(@$_) }
167 87 ▶ map { 'ARRAY' eq ref $_ ? $_ : [ $_, $_ ] } @$tests;
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $_ ? : 162 341 503
168 87 my $schedule = $self->_rule_clause( $rules, \@tests );
169  
170 # If any tests are left add them as a sequential block at the end of
171 # the run.
172 87 ▶ $schedule = [ [ $schedule, @tests ] ] if @tests;
Branch: 1 branch
BranchTrueFalseTotal
if @tests 4 83 87
173  
174 87 $self->{schedule} = $schedule;
175 }
176  
177 137 ▶sub _rule_clause {
Subroutine _rule_clause: called CC=6 SCAR=18.1
178 137 my ( $self, $rule, $tests ) = @_;
179 137 ▶ croak 'Rule clause must be a hash'
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $rule 0 137 137
180 unless 'HASH' eq ref $rule;
181  
182 137 my @type = keys %$rule;
183 137 ▶ croak 'Rule clause must have exactly one key'
Branch: 1 branch
BranchTrueFalseTotal
unless @type == 1 0 137 137
184 unless @type == 1;
185  
186 my %handlers = (
187 91 ▶ par => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
188 91 [ map { [$_] } @_ ];
189 },
190 46 ▶ seq => sub { [ [@_] ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
191 137 );
192  
193 137 ▶ my $handler = $handlers{ $type[0] }
Condition50%1/2
$handlers{$type[0]} || croak('Unknown scheduler type: ', $type[0])
l!l
1370
MC/DC0%0/1
$handlers{$type[0]} || croak('Unknown scheduler type: ', $type[0])
$handlers{$type[0]}
Truth table50%1/2
A
A= $handlers{$type[0]}
Aresult
11
00
194 || croak 'Unknown scheduler type: ', $type[0];
195 137 my $val = $rule->{ $type[0] };
196  
197 return $handler->(
198 map {
199 137 ▶ 'HASH' eq ref $_
Branch: 2 branches
BranchTrueFalseTotal
'HASH' eq ref $_ ? : 50 138 188
'ARRAY' eq ref $val ? : 18 119 137
200 ? $self->_rule_clause( $_, $tests )
201 : $self->_expand( $_, $tests )
202 } 'ARRAY' eq ref $val ? @$val : $val
203 );
204 }
205  
206 158 ▶sub _glob_to_regexp {
Subroutine _glob_to_regexp: called CC=12 SCAR=24.8
207 158 my ( $self, $glob ) = @_;
208 158 my $nesting;
209 my $pattern;
210  
211 158 while (1) {
212 316 ▶ if ( $glob =~ /\G\*\*/gc ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($glob =~ /\G\*\*/cg) { } 80 236 316
elsif ($glob =~ /\G\*/cg) { } 54 182 236
elsif ($glob =~ /\G\?/cg) { } 1 181 182
elsif ($glob =~ /\G\{/cg) { } 15 166 181
elsif ($nesting and $glob =~ /\G,/cg) { } 18 148 166
elsif ($nesting and $glob =~ /\G\}/cg) { } 15 133 148
elsif ($glob =~ /\G(\\.)/cg) { } 3 130 133
elsif ($glob =~ /\G([\},])/cg) { } 8 122 130
213  
214 # ** is any number of characters, including /, within a pathname
215 80 $pattern .= '.*?';
216 }
217 elsif ( $glob =~ /\G\*/gc ) {
218  
219 # * is zero or more characters within a filename/directory name
220 54 $pattern .= '[^/]*';
221 }
222 elsif ( $glob =~ /\G\?/gc ) {
223  
224 # ? is exactly one character within a filename/directory name
225 1 $pattern .= '[^/]';
226 }
227 elsif ( $glob =~ /\G\{/gc ) {
228  
229 # {foo,bar,baz} is any of foo, bar or baz.
230 15 $pattern .= '(?:';
231 15 ++$nesting;
232 }
233 ▶ elsif ( $nesting and $glob =~ /\G,/gc ) {
Condition100%3/3
$nesting and $glob =~ /\G,/cg
!ll&&!rl&&r
975118
MC/DC100%2/2
$nesting and $glob =~ /\G,/cg
$nesting $glob =~ /\G,/cg
Truth table100%3/3
A and B
A= $nestingB= $glob =~ /\G,/cg
ABresult
0X0
100
111
234  
235 # , is only special inside {}
236 18 $pattern .= '|';
237 }
238 ▶ elsif ( $nesting and $glob =~ /\G\}/gc ) {
Condition100%3/3
$nesting and $glob =~ /\G\}/cg
!ll&&!rl&&r
973615
MC/DC100%2/2
$nesting and $glob =~ /\G\}/cg
$nesting $glob =~ /\G\}/cg
Truth table100%3/3
A and B
A= $nestingB= $glob =~ /\G\}/cg
ABresult
0X0
100
111
239  
240 # } that matches { is special. But unbalanced } are not.
241 15 $pattern .= ')';
242 15 --$nesting;
243 }
244 elsif ( $glob =~ /\G(\\.)/gc ) {
245  
246 # A quoted literal
247 3 $pattern .= $1;
248 }
249 elsif ( $glob =~ /\G([\},])/gc ) {
250  
251 # Sometimes meta characters
252 8 $pattern .= '\\' . $1;
253 }
254 else {
255  
256 # Eat everything that is not a meta character.
257 122 $glob =~ /\G([^{?*\\\},]*)/gc;
258 122 $pattern .= quotemeta $1;
259 }
260 316 ▶ return $pattern if pos $glob == length $glob;
Branch: 1 branch
BranchTrueFalseTotal
if pos $glob == length $glob 158 158 316
261 }
262 }
263  
264 138 ▶sub _expand {
Subroutine _expand: called CC=3 SCAR=11.0
265 138 my ( $self, $name, $tests ) = @_;
266  
267 138 my $pattern = $self->_glob_to_regexp($name);
268 138 $pattern = qr/^ $pattern $/x;
269 138 my @match = ();
270  
271 138 ▶ for ( my $ti = 0; $ti < @$tests; $ti++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ti < @$tests) 3622 138 3760
272 3622 ▶ if ( $tests->[$ti]->filename =~ $pattern ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tests->[$ti]->filename =~ /$pattern/) 469 3153 3622
273 469 push @match, splice @$tests, $ti, 1;
274 469 $ti--;
275 }
276 }
277  
278 138 return @match;
279 }
280  
281 =head2 Instance Methods
282  
283 =head3 C<get_all>
284  
285 Get a list of all remaining tests.
286  
287 =cut
288  
289 174 ▶sub get_all {
Subroutine get_all: called CC=1 SCAR=0.0
Pod documented
290 174 my $self = shift;
291 174 my @all = $self->_gather( $self->{schedule} );
292 174 $self->{count} = @all;
293 174 @all;
294 }
295  
296 760 ▶sub _gather {
Subroutine _gather: called CC=4 SCAR=13.9
297 760 my ( $self, $rule ) = @_;
298 760 ▶ return unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 0 760 760
299 760 ▶ return $rule unless 'ARRAY' eq ref $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $rule 503 257 760
300 257 ▶ return map { defined() ? $self->_gather($_) : () } map {@$_} @$rule;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 586 45 631
301 }
302  
303 =head3 C<get_job>
304  
305 Return the next available job as L<TAP::Parser::Scheduler::Job> object or
306 C<undef> if none are available. Returns a L<TAP::Parser::Scheduler::Spinner> if
307 the scheduler still has pending jobs but none are available to run right now.
308  
309 =cut
310  
311 651 ▶sub get_job {
Subroutine get_job: called CC=4 SCAR=13.9
Pod documented
312 651 my $self = shift;
313 651 ▶ $self->{count} ||= $self->get_all;
Condition100%3/3
$self->{"count"} ||= $self->get_all
l!l&&r!l&&!r
5541087
MC/DC100%2/2
$self->{"count"} ||= $self->get_all
$self->{"count"} $self->get_all
Truth table100%3/3
A ||= B
A= $self->{"count"}B= $self->get_all
ABresult
1X1
011
000
314 651 my @jobs = $self->_find_next_job( $self->{schedule} );
315 651 ▶ if (@jobs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@jobs) 503 148 651
316 503 --$self->{count};
317 503 return $jobs[0];
318 }
319  
320 return TAP::Parser::Scheduler::Spinner->new
321 148 ▶ if $self->{count};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"count"} 61 87 148
322  
323 87 return;
324 }
325  
326 23716 ▶sub _not_empty {
Subroutine _not_empty: called CC=4 SCAR=13.9
327 23716 my $ar = shift;
328 23716 ▶ return 1 unless 'ARRAY' eq ref $ar;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $ar 10423 13293 23716
329 13293 for (@$ar) {
330 13276 ▶ return 1 if _not_empty($_);
Branch: 1 branch
BranchTrueFalseTotal
if _not_empty($_) 13226 50 13276
331 }
332 67 return;
333 }
334  
335 10440 ▶sub _is_empty { !_not_empty(@_) }
Subroutine _is_empty: called CC=1 SCAR=0.0
336  
337 4217 ▶sub _find_next_job {
Subroutine _find_next_job: called CC=9 SCAR=22.0
338 4217 my ( $self, $rule ) = @_;
339  
340 4217 my @queue = ();
341 4217 my $index = 0;
342 4217 ▶ while ( $index < @$rule ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$rule) 10615 3714 14329
343 10615 my $seq = $rule->[$index];
344  
345 # Prune any exhausted items.
346 10615 ▶ shift @$seq while @$seq && _is_empty( $seq->[0] );
Branch: 1 branch
BranchTrueFalseTotal
@$seq and _is_empty($seq->[0]) and do { shift @$seq } 17 10615 10632
Condition100%3/3
@$seq and _is_empty($seq->[0])
!ll&&!rl&&r
1921042317
MC/DC100%2/2
@$seq and _is_empty($seq->[0])
@$seq _is_empty($seq->[0])
Truth table100%3/3
A and B
A= @$seqB= _is_empty($seq->[0])
ABresult
0X0
100
111
347 10615 ▶ if (@$seq) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$seq) { } 10423 192 10615
348 10423 ▶ if ( defined $seq->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seq->[0]) 7046 3377 10423
349 7046 ▶ if ( 'ARRAY' eq ref $seq->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $seq->[0]) { } 6543 503 7046
350 6543 push @queue, $seq;
351 }
352 else {
353 503 my $job = splice @$seq, 0, 1, undef;
354 503 ▶ $job->on_finish( sub { shift @$seq } );
Subroutine __ANON__: called CC=1 SCAR=0.0
355 503 return $job;
356 }
357 }
358 9920 ++$index;
359 }
360 else {
361  
362 # Remove the empty sub-array from the array
363 192 splice @$rule, $index, 1;
364 }
365 }
366  
367 3714 for my $seq (@queue) {
368 3566 ▶ if ( my @jobs = $self->_find_next_job( $seq->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@jobs) = $self->_find_next_job($seq->[0])) 309 3257 3566
369 309 return @jobs;
370 }
371 }
372  
373 3405 return;
374 }
375  
376 =head3 C<as_string>
377  
378 Return a human readable representation of the scheduling tree.
379 For example:
380  
381 my @tests = (qw{
382 t/startup/foo.t
383 t/shutdown/foo.t
384
385 t/a/foo.t t/b/foo.t t/c/foo.t t/d/foo.t
386 });
387 my $sched = TAP::Parser::Scheduler->new(
388 tests => \@tests,
389 rules => {
390 seq => [
391 { seq => 't/startup/*.t' },
392 { par => ['t/a/*.t','t/b/*.t','t/c/*.t'] },
393 { seq => 't/shutdown/*.t' },
394 ],
395 },
396 );
397  
398 Produces:
399  
400 par:
401 seq:
402 par:
403 seq:
404 par:
405 seq:
406 't/startup/foo.t'
407 par:
408 seq:
409 't/a/foo.t'
410 seq:
411 't/b/foo.t'
412 seq:
413 't/c/foo.t'
414 par:
415 seq:
416 't/shutdown/foo.t'
417 't/d/foo.t'
418  
419  
420 =cut
421  
422  
423 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
424 0 my $self = shift;
425 0 return $self->_as_string( $self->{schedule} );
426 }
427  
428 0 ▶sub _as_string {
Subroutine _as_string: not called CC=5 SCAR=34.0
429 0 ▶ my ( $self, $rule, $depth ) = ( shift, shift, shift || 0 );
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
430 0 my $pad = ' ' x 2;
431 0 my $indent = $pad x $depth;
432 0 ▶ if ( !defined $rule ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rule) { } 0 0 0
elsif ('ARRAY' eq ref $rule) { } 0 0 0
433 0 return "$indent(undef)\n";
434 }
435 elsif ( 'ARRAY' eq ref $rule ) {
436 0 ▶ return unless @$rule;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rule 0 0 0
437 0 my $type = ( 'par', 'seq' )[ $depth % 2 ];
438 return join(
439 '', "$indent$type:\n",
440 0 map { $self->_as_string( $_, $depth + 1 ) } @$rule
441 );
442 }
443 else {
444 0 return "$indent'" . $rule->filename . "'\n";
445 }
446 }
447  
448 16 1;
« blib/lib/TAP/Formatter/File/Session.pm ↑ Summary blib/lib/TAP/Parser/YAMLish/Reader.pm »