forked from houseabsolute/test-class-moose
/
Parallel.pm
executable file
·206 lines (165 loc) · 5.39 KB
/
Parallel.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package Test::Class::Moose::Executor::Parallel;
# ABSTRACT: Execute tests in parallel (parallelized by instance)
use 5.10.0;
our $VERSION = '0.67';
use Moose 2.0000;
use Carp;
use namespace::autoclean;
with 'Test::Class::Moose::Role::Executor';
use List::MoreUtils qw(none);
use Parallel::ForkManager;
use TAP::Stream 0.44;
use Test::Builder;
use Test::Class::Moose::AttributeRegistry;
use Test::Class::Moose::Report::Class;
use List::MoreUtils qw(uniq);
has 'jobs' => (
is => 'ro',
isa => 'Int',
required => 1,
);
has 'color_output' => (
is => 'ro',
isa => 'Bool',
required => 1,
);
has '_color' => (
is => 'ro',
isa => 'TAP::Formatter::Color',
lazy_build => 1,
);
sub runtests {
my $self = shift;
local $Test::Builder::Level = $Test::Builder::Level + 4;
my $jobs = $self->jobs;
# We need to fetch this output handle before forking off jobs. Otherwise,
# we lose our test builder output if we have a sequential job after the
# parallel jobs. This happens because we explicitly set the builder's
# output to a scalar ref in our $run_instance sub above.
my $test_builder_output = Test::Builder->new->output;
my $stream = TAP::Stream->new;
my $fork = $self->_make_fork_manager($stream);
my @sequential;
$self->_run_parallel_jobs( $fork, \@sequential );
for my $pair (@sequential) {
my $output = $self->_run_instance( @{$pair} );
$stream->add_to_stream(
TAP::Stream::Text->new(
text => $output,
name =>
"Sequential tests for $pair->[0] run after parallel tests",
)
);
}
# this prevents overwriting the line of dots output from
# $RUN_TEST_CONTROL_METHOD
print STDERR "\n";
# this is where we print the TAP results
print $test_builder_output $stream->to_string;
return $self;
}
sub _make_fork_manager {
my ( $self, $stream ) = @_;
my $fork = Parallel::ForkManager->new( $self->jobs );
$fork->run_on_finish(
sub {
my ($pid, $exit_code, $ident, $exit_signal, $core_dump,
$result
) = @_;
if ( defined($result) ) {
my ( $job_num, $tap ) = @$result;
$stream->add_to_stream(
TAP::Stream::Text->new(
text => $tap, name => "Job #$job_num (pid: $pid)"
)
);
}
else
{ # problems occuring during storage or retrieval will throw a warning
carp("No TAP received from child process $pid!");
}
}
);
return $fork;
}
sub _run_parallel_jobs {
my ( $self, $fork, $sequential ) = @_;
my @test_classes = $self->test_classes;
my $job_num = 0;
foreach my $test_class ( $self->test_classes ) {
my $class_report
= Test::Class::Moose::Report::Class->new( name => $test_class );
$self->test_report->add_test_class($class_report);
my %test_instances = $test_class->_tcm_make_test_class_instances(
$self->test_configuration->args,
test_report => $self->test_report,
);
foreach my $test_instance_name ( sort keys %test_instances ) {
my $test_instance = $test_instances{$test_instance_name};
if ( $self->_test_instance_is_parallelizable($test_instance) ) {
$job_num++;
my $pid = $fork->start and next;
my $output = $self->_run_instance(
$test_instance_name,
$test_instance
);
$fork->finish( 0, [ $job_num, $output ] );
}
else {
push @{$sequential}, [ $test_instance_name, $test_instance ];
}
}
}
$fork->wait_all_children;
return;
}
sub _test_instance_is_parallelizable {
my ( $self, $test_instance ) = @_;
my $test_class = $test_instance->test_class;
return none {
Test::Class::Moose::AttributeRegistry->method_has_tag(
$test_class,
$_,
'noparallel'
);
}
$self->_tcm_test_methods_for_instance($test_instance);
}
sub _run_instance {
my ( $self, $test_instance_name, $test_instance ) = @_;
my $builder = Test::Builder->new;
my $output;
$builder->output( \$output );
$builder->failure_output( \$output );
$builder->todo_output( \$output );
$self->_tcm_run_test_instance( $test_instance_name, $test_instance );
return $output;
}
after '_tcm_run_test_method' => sub {
my $self = shift;
my $config = $self->test_configuration;
my $builder = $config->builder;
# we're running under parallel testing, so rather than having
# the code look like it's stalled, we'll output a dot for
# every test method.
my ( $color, $text )
= ( $builder->details )[-1]{ok}
? ( 'green', '.' )
: ( 'red', 'X' );
# The set_color() method from Test::Formatter::Color is just ugly.
if ( $self->color_output ) {
$self->_color->set_color(
sub { print STDERR shift, $text },
$color,
);
$self->_color->set_color( sub { print STDERR shift }, 'reset' );
}
else {
print STDERR $text;
}
};
sub _build__color {
return TAP::Formatter::Color->new;
}
1;
=for Pod::Coverage Tags Tests runtests