| 1 | package Test::Class::Moose::Executor::Parallel; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Execute tests in parallel (parallelized by instance) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use 5.010000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Moose 2.0000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | with 'Test::Class::Moose::Role::Executor'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Needs to come before we load other test tools | ||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use Test2::IPC; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | ▶ | use List::SomeUtils qw( none part ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 11 | ▶ | use Parallel::ForkManager; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 11 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 11 | ▶ | use TAP::Formatter::Color 3.29; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 11 | ▶ | use Test2::API qw( test2_stack ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 11 | ▶ | use Test2::AsyncSubtest 1.302212 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 11 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 11 | require Test2::AsyncSubtest::Hub; | |||||||||||||||||||||||||||
| 28 | 11 | Test2::AsyncSubtest::Hub->do_not_warn_on_plan(); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | 11 | ▶ | use Test::Class::Moose::AttributeRegistry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | use Test::Class::Moose::Report::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 11 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 11 | has 'jobs' => ( | |||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => 'Int', | ||||||||||||||||||||||||||||
| 37 | required => 1, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 11 | has color_output => ( | |||||||||||||||||||||||||||
| 41 | is => 'ro', | ||||||||||||||||||||||||||||
| 42 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 43 | default => 1, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 11 | has show_parallel_progress => ( | |||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||
| 48 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 49 | default => 1, | ||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 11 | has '_fork_manager' => ( | |||||||||||||||||||||||||||
| 53 | is => 'ro', | ||||||||||||||||||||||||||||
| 54 | isa => 'Parallel::ForkManager', | ||||||||||||||||||||||||||||
| 55 | init_arg => undef, | ||||||||||||||||||||||||||||
| 56 | lazy => 1, | ||||||||||||||||||||||||||||
| 57 | builder => '_build_fork_manager', | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | has '_subtests' => ( | ||||||||||||||||||||||||||||
| 61 | traits => ['Hash'], | ||||||||||||||||||||||||||||
| 62 | is => 'bare', | ||||||||||||||||||||||||||||
| 63 | isa => 'HashRef[Test2::AsyncSubtest]', | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | init_arg => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 11 | handles => { | |||||||||||||||||||||||||||
| 66 | _save_subtest => 'set', | ||||||||||||||||||||||||||||
| 67 | _saved_subtest => 'get', | ||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 11 | has '_color' => ( | |||||||||||||||||||||||||||
| 72 | is => 'ro', | ||||||||||||||||||||||||||||
| 73 | isa => 'TAP::Formatter::Color', | ||||||||||||||||||||||||||||
| 74 | lazy => 1, | ||||||||||||||||||||||||||||
| 75 | builder => '_build_color', | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 11 | ▶ | around _run_test_classes => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 79 | 11 | my $orig = shift; | |||||||||||||||||||||||||||
| 80 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 81 | 11 | my @test_classes = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | my ( $seq, $par ) | ||||||||||||||||||||||||||||
| 84 | 11 | ▶ | = part { $self->_test_class_is_parallelizable($_) } @test_classes; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 11 | $self->_run_test_classes_in_parallel($par); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | $self->$orig( @{$seq} ) | |||||||||||||||||||||||||||
| 89 | 1 | ▶ | if $seq && @{$seq}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$seq and @{$seq;}
MC/DC0%0/2
$seq and @{$seq;}
$seq
@{$seq;}
Truth table33%1/3
A and B
A= $seqB= @{$seq;}
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | return; | |||||||||||||||||||||||||||
| 92 | 11 | }; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 121 | ▶ | sub _test_class_is_parallelizable { | ||||||||||||||||||||||||||
|
Subroutine
_test_class_is_parallelizable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 121 | my ( $self, $test_class ) = @_; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 297 | ▶ | return none { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | 297 | Test::Class::Moose::AttributeRegistry->method_has_tag( | |||||||||||||||||||||||||||
| 99 | $test_class, | ||||||||||||||||||||||||||||
| 100 | $_, | ||||||||||||||||||||||||||||
| 101 | 'noparallel' | ||||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 121 | $self->_test_methods_for($test_class); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 11 | ▶ | sub _run_test_classes_in_parallel { | ||||||||||||||||||||||||||
|
Subroutine
_run_test_classes_in_parallel: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 108 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 11 | my $test_classes = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 11 | for my $test_class ( @{$test_classes} ) { | |||||||||||||||||||||||||||
| 112 | 65 | my $subtest = Test2::AsyncSubtest->new( | |||||||||||||||||||||||||||
| 113 | name => $test_class, | ||||||||||||||||||||||||||||
| 114 | hub_init_args => { manual_skip_all => 1 }, | ||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||
| 116 | 65 | my $id = $subtest->cleave; | |||||||||||||||||||||||||||
| 117 | 65 | ▶ | if ( my $pid = $self->_fork_manager->start ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 55 | $self->_save_subtest( $pid => $subtest ); | |||||||||||||||||||||||||||
| 119 | 55 | next; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # This chunk of code only runs in child processes | ||||||||||||||||||||||||||||
| 123 | 10 | my $class_report; | |||||||||||||||||||||||||||
| 124 | 10 | $subtest->attach($id); | |||||||||||||||||||||||||||
| 125 | $subtest->run( | ||||||||||||||||||||||||||||
| 126 | 10 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 127 | 10 | $class_report = $self->run_test_class($test_class); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 10 | ); | |||||||||||||||||||||||||||
| 130 | 10 | $subtest->detach; | |||||||||||||||||||||||||||
| 131 | 10 | $self->_fork_manager->finish( 0, \$class_report ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | $self->_fork_manager->wait_all_children; | |||||||||||||||||||||||||||
| 135 | 1 | test2_stack()->top->cull; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | return; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 11 | ▶ | sub _build_fork_manager { | ||||||||||||||||||||||||||
|
Subroutine
_build_fork_manager: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 141 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 11 | my $pfm = Parallel::ForkManager->new( $self->jobs ); | |||||||||||||||||||||||||||
| 144 | $pfm->run_on_finish( | ||||||||||||||||||||||||||||
| 145 | 50 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 146 | 50 | my ( $pid, $class_report ) = @_[ 0, 5 ]; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 50 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 50 | $self->test_report->add_test_class( ${$class_report} ); | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 152 | 0 | warn $_; | |||||||||||||||||||||||||||
| 153 | 50 | }; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 50 | my $subtest = $self->_saved_subtest($pid); | |||||||||||||||||||||||||||
| 156 | 50 | ▶ | unless ($subtest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | warn | |||||||||||||||||||||||||||
| 158 | "Child process $pid ended but there is no active subtest for that pid!"; | ||||||||||||||||||||||||||||
| 159 | 0 | return; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 50 | $subtest->finish; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | 11 | ); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 11 | return $pfm; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 26 | ▶ | around run_test_method => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=28.6
| |||||||||||||||||||||||||||||
| 170 | 26 | my $orig = shift; | |||||||||||||||||||||||||||
| 171 | 26 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 26 | my $method_report = $self->$orig(@_); | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 26 | ▶ | return $method_report unless $self->show_parallel_progress; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # we're running under parallel testing, so rather than having | ||||||||||||||||||||||||||||
| 178 | # the code look like it's stalled, we'll output a dot for | ||||||||||||||||||||||||||||
| 179 | # every test method. | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ( $color, $text ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | = $method_report->passed | ||||||||||||||||||||||||||||
| 182 | ? ( 'green', '.' ) | ||||||||||||||||||||||||||||
| 183 | : ( 'red', 'X' ); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | # The set_color() method from TAP::Formatter::Color is just ugly. | ||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( $self->color_output ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | $self->_color->set_color( | ||||||||||||||||||||||||||||
| 188 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 189 | 0 | ▶ | print STDERR shift, $text | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | or die $!; | ||||||||||||||||||||||||||||
| 191 | }, | ||||||||||||||||||||||||||||
| 192 | 0 | $color, | |||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | $self->_color->set_color( | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 196 | 0 | ▶ | print STDERR shift | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | or die $!; | ||||||||||||||||||||||||||||
| 198 | }, | ||||||||||||||||||||||||||||
| 199 | 0 | 'reset' | |||||||||||||||||||||||||||
| 200 | ); | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | else { | ||||||||||||||||||||||||||||
| 203 | 0 | ▶ | print STDERR $text | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | or die $!; | ||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | return $method_report; | |||||||||||||||||||||||||||
| 208 | 11 | }; | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | ▶ | sub _build_color { | ||||||||||||||||||||||||||
|
Subroutine
_build_color: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 211 | 0 | return TAP::Formatter::Color->new; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 11 | 1; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||