blib/lib/TAP/Parser/SourceHandler/Worker.pm

filter: Statementstmt 71.1% 106 / 149 Branchbran 30.3% 17 / 56 Conditioncond 27.2% 3 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 85.0% 17 / 20 Podpod 100% 7 / 7 totaltotal 60.0% 150 / 250 CC 35 SCAR 32.6
CC
35
Cov
58%
get_active_workers
90.0
load_options
42.0
get_a_worker
19.0
CRAP 26.0
« blib/lib/App/Prove/Plugin/Distributed.pm ↑ Summary blib/lib/TAP/Parser/Iterator/Worker/PBS.pm »
1 package TAP::Parser::SourceHandler::Worker;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use vars (qw($VERSION @ISA));
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use TAP::Parser::SourceHandler ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use TAP::Parser::IteratorFactory ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use TAP::Parser::Iterator::Worker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use TAP::Parser::SourceHandler::Perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use TAP::Parser::Iterator::Stream::Selectable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 @ISA = 'TAP::Parser::SourceHandler';
17  
18 1 TAP::Parser::IteratorFactory->register_handler(__PACKAGE__);
19  
20 =head1 NAME
21  
22 TAP::Parser::SourceHandler::Worker - Stream TAP from an L<IO::Handle> or a GLOB.
23  
24 =head1 VERSION
25  
26 Version 0.08
27  
28 =cut
29  
30 1 $VERSION = '0.08';
31  
32 =head3 C<@workers>
33  
34 Class static variable to keep track of workers.
35  
36 =cut
37  
38 1 my @workers = ();
39  
40 =head3 C<$number_of_workers>
41  
42 Class static variable to keep track of number of workers.
43  
44 =cut
45  
46 1 my $number_of_workers;
47  
48 =head3 C<$listener>
49  
50 Class static variable to store the worker listener.
51  
52 =cut
53  
54 my $listener;
55  
56 =head3 C<$use_local_public_ip>
57  
58 Class static variable to flag the local public ip is needed.
59 Some of the home network might not have name server setup. Therefore,
60 the public local ip is needed.
61  
62 =cut
63  
64 1 my $use_local_public_ip;
65  
66 =head3 C<$local_public_ip>
67  
68 Class static variable to store the local public ip is needed.
69 Some of the home network might not have name server setup. Therefore,
70 the public local ip is needed.
71  
72 =cut
73  
74 1 my $local_public_ip;
75  
76 =head3 C<$sync_type>
77  
78 Syncronize the source directory that will be used for testing to the remote
79 host with the directory specified on the variable C<$destination_dir>.
80  
81 Currently it only support syncronize type of C<rsync>.
82  
83 =cut
84  
85 1 my $sync_type;
86  
87 =head3 C<$destination_dir>
88  
89 Syncronize the source to destination directory.
90  
91 If it is not specified, it will be created with L<File::Temp::tempdir>.
92  
93 =cut
94  
95 1 my $destination_dir;
96  
97 =head3 C<can_handle>
98  
99 my $vote = $class->can_handle( $source );
100  
101 Casts the following votes:
102  
103 Vote the same way as the L<TAP::Parser::SourceHandler::Perl>
104 but with 0.01 higher than perl source.
105  
106 =cut
107  
108 6 ▶sub can_handle {
Subroutine can_handle: called CC=5 SCAR=17.1
Pod documented
109 6 my ( $class, $src ) = @_;
110 6 my $vote = TAP::Parser::SourceHandler::Perl->can_handle($src);
111 6 ▶ return 0 unless ($vote);
Branch: 1 branch
BranchTrueFalseTotal
unless $vote 1 5 6
112 5 ▶ if ( $src->{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src->{"config"}) 5 0 5
113 5 my @config_keys = keys %{ $src->{config} };
114 5 ▶ if ( scalar(@config_keys) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @config_keys == 1) 0 5 5
115  
116 #LSF: If it is detach, we just run everythings.
117 0 ▶ if ( $src->{config}->{ $config_keys[0] }->{detach} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src->{"config"}{$config_keys[0]}{"detach"}) 0 0 0
118 0 $vote = 0.90;
119 }
120 }
121 }
122  
123 #LSF: If it is a subclass, we will add 0.01 for each level of subclass.
124 5 my $package = __PACKAGE__;
125 5 my $tmp = $class;
126 5 $tmp =~ s/^$package//;
127 5 my @number = split '::', $tmp;
128  
129 5 return $vote + ( 1 + scalar(@number) ) * 0.01;
130 }
131  
132 =head1 SYNOPSIS
133  
134 =cut
135  
136 =head3 C<make_iterator>
137  
138 my $iterator = $class->make_iterator( $source );
139  
140 Returns a new L<TAP::Parser::Iterator::Stream::Selectable> for the source.
141  
142 =cut
143  
144 1 ▶sub make_iterator {
Subroutine make_iterator: called CC=3 SCAR=13.6
Pod documented
145 1 my ( $class, $source, $retry ) = @_;
146  
147 1 my $worker = $class->get_a_worker($source);
148  
149 1 ▶ if ($worker) {
Branch: 2 branches
BranchTrueFalseTotal
if ($worker) { } 1 0 1
elsif (not $retry) { } 0 0 0
150 1 $worker->autoflush(1);
151 1 $worker->print( ${ $source->raw } . "\n" );
152 1 return TAP::Parser::Iterator::Stream::Selectable->new(
153 { handle => $worker } );
154 }
155 elsif ( !$retry ) {
156  
157 #LSF: Let check the worker.
158 0 my @active_workers = $class->get_active_workers();
159  
160 #unless(@active_workers) {
161 # die "failed to find any worker.\n";
162 #}
163 0 @workers = @active_workers;
164  
165 #LSF: Retry one more time.
166 0 return $class->make_iterator( $source, 1 );
167 }
168  
169 #LSF: Pass through everything now.
170 0 return;
171 }
172  
173 =head3 C<get_a_worker>
174  
175 my $worker = $class->get_a_worker();
176  
177 Returns a new workder L<IO::Socket>
178  
179 =cut
180  
181 1 ▶sub get_a_worker {
Subroutine get_a_worker: called CC=15 SCAR=29.4
Pod documented
182 1 my $class = shift;
183 1 my $source = shift;
184 1 my $package = __PACKAGE__;
185 1 my $tmp = $class;
186 1 $tmp =~ s/^$package//;
187 1 my $option_name = 'Worker' . $tmp;
188 $number_of_workers = $source->{config}->{$option_name}->{number_of_workers}
189 1 ▶ || 1;
Condition50%1/2
$source->{'config'}{$option_name}{'number_of_workers'} || 1
l!l
01
MC/DC0%0/1
$source->{'config'}{$option_name}{'number_of_workers'} || 1
$source->{'config'}{$option_name}{'number_of_workers'}
Truth table50%1/2
A
A= $source->{'config'}{$option_name}{'number_of_workers'}
Aresult
11
00
190 1 my $startup = $source->{config}->{$option_name}->{start_up};
191 1 my $teardown = $source->{config}->{$option_name}->{tear_down};
192 1 my $error_log = $source->{config}->{$option_name}->{error_log};
193 1 my $detach = $source->{config}->{$option_name}->{detach};
194 1 my $sync_type = $source->{config}->{$option_name}->{sync_type};
195 1 my $source_dir = $source->{config}->{$option_name}->{source_dir};
196 1 my $destination_dir = $source->{config}->{$option_name}->{destination_dir};
197 1 my %args = ();
198 1 ▶ $args{start_up} = $startup if ($startup);
Branch: 1 branch
BranchTrueFalseTotal
if $startup 0 1 1
199 1 ▶ $args{tear_down} = $teardown if ($teardown);
Branch: 1 branch
BranchTrueFalseTotal
if $teardown 0 1 1
200 1 ▶ $args{detach} = $detach if ($detach);
Branch: 1 branch
BranchTrueFalseTotal
if $detach 0 1 1
201 1 ▶ $args{sync_type} = $sync_type if ($sync_type);
Branch: 1 branch
BranchTrueFalseTotal
if $sync_type 0 1 1
202 1 ▶ $args{source_dir} = $source_dir if ($source_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $source_dir 0 1 1
203 1 ▶ $args{destination_dir} = $destination_dir if ($destination_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $destination_dir 0 1 1
204 1 ▶ $args{error_log} = $error_log if ($error_log);
Branch: 1 branch
BranchTrueFalseTotal
if $error_log 0 1 1
205 1 $args{switches} = $source->{switches};
206 1 ▶ $args{test_args} = $source->{test_args} if ( $source->{test_args} );
Branch: 1 branch
BranchTrueFalseTotal
if $source->{"test_args"} 0 1 1
207  
208 1 ▶ if ( @workers < $number_of_workers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@workers < $number_of_workers) 1 0 1
209 1 my $listener = $class->listener;
210 1 ▶ if ( $use_local_public_ip && !$local_public_ip ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_local_public_ip and not $local_public_ip) 0 1 1
Condition33%1/3
$use_local_public_ip and not $local_public_ip
!ll&&!rl&&r
100
MC/DC0%0/2
$use_local_public_ip and not $local_public_ip
$use_local_public_ip not $local_public_ip
Truth table33%1/3
A and not(B)
A= $use_local_public_ipB= not $local_public_ip
ABresult
0X0
100
111
211 0 require Net::Address::IP::Local;
212 0 $local_public_ip = Net::Address::IP::Local->public;
213 }
214  
215 1 ▶ my $spec = (
Branch: 1 branch
BranchTrueFalseTotal
$listener->sockhost eq '0.0.0.0' ? : 1 0 1
Condition33%1/3
$local_public_ip || ($listener->sockhost eq '0.0.0.0' ? hostname() : $listener->sockhost)
l!l&&r!l&&!r
010
MC/DC0%0/2
$local_public_ip || ($listener->sockhost eq '0.0.0.0' ? hostname() : $listener->sockhost)
$local_public_ip ($listener->sockhost eq '0.0.0.0' ? hostname() : $listener->sockhost)
Truth table33%1/3
A || B
A= $local_public_ipB= ($listener->sockhost eq '0.0.0.0' ? hostname() : $listener->sockhost)
ABresult
1X1
011
000
216 $local_public_ip
217 || (
218 $listener->sockhost eq '0.0.0.0'
219 ? hostname
220 : $listener->sockhost
221 )
222 )
223 . ':'
224 . $listener->sockport;
225 1 my $iterator_class = $class->iterator_class;
226 1 ▶ eval "use $iterator_class;";
Subroutine BEGIN: called CC=1 SCAR=0.0
227 1 $args{spec} = $spec;
228 1 my $iterator = $class->iterator_class->new( \%args );
229 1 push @workers, $iterator;
230 }
231 1 return $listener->accept();
232 }
233  
234 =head3 C<listener>
235  
236 my $listener = $class->listener();
237  
238 Returns worker listener L<IO::Socket::INET>
239  
240 =cut
241  
242 1 ▶sub listener {
Subroutine listener: called CC=2 SCAR=7.0
Pod documented
243 1 my $class = shift;
244 1 ▶ unless ($listener) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($listener) 1 0 1
245 1 $listener = IO::Socket::INET->new(
246 Listen => 5,
247 Proto => 'tcp',
248 Timeout => 40,
249 );
250 }
251 1 return $listener;
252 }
253  
254 =head3 C<iterator_class>
255  
256 The class of iterator to use, override if you're sub-classing. Defaults
257 to L<TAP::Parser::Iterator::Worker>.
258  
259 =cut
260  
261 1 ▶use constant iterator_class => 'TAP::Parser::Iterator::Worker';
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 =head3 C<workers>
264  
265 Returns list of workers.
266  
267 =cut
268  
269 0 ▶sub workers {
Subroutine workers: not called CC=1 SCAR=6.9
Pod documented
270 0 return @workers;
271 }
272  
273 =head3 C<get_active_workers>
274
275 my @active_workers = $class->get_active_workers;
276  
277 Returns list of active workers.
278  
279 =cut
280  
281 0 ▶sub get_active_workers {
Subroutine get_active_workers: not called CC=9 SCAR=45.0
Pod documented
282 0 my $class = shift;
283 0 my @workers = $class->workers;
284 0 ▶ return unless (@workers);
Branch: 1 branch
BranchTrueFalseTotal
unless @workers 0 0 0
285 0 my @active;
286 0 for my $worker (@workers) {
287 0 ▶ next unless ( $worker && $worker->{sel} );
Branch: 1 branch
BranchTrueFalseTotal
unless $worker and $worker->{"sel"} 0 0 0
Condition0%0/3
$worker and $worker->{"sel"}
!ll&&!rl&&r
000
MC/DC0%0/2
$worker and $worker->{"sel"}
$worker $worker->{"sel"}
Truth table0%0/3
A and B
A= $workerB= $worker->{"sel"}
ABresult
0X0
100
111
288 0 my @handles = $worker->{sel}->can_read();
289 0 for my $handle (@handles) {
290 0 ▶ if ( $handle == $worker->{err} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle == $worker->{"err"}) 0 0 0
291 0 my $error = '';
292 0 ▶ if ( $handle->read( $error, 640000 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->read($error, 640000)) 0 0 0
293 0 chomp($error);
294 0 print STDERR "Worker with error [$error].\n";
295  
296 #LSF: Close the handle.
297 0 $handle->close();
298 0 $worker = undef;
299 0 last;
300 }
301 }
302 }
303 0 ▶ push @active, $worker if ($worker);
Branch: 1 branch
BranchTrueFalseTotal
if $worker 0 0 0
304 }
305 0 return @active;
306 }
307  
308 =head3 C<load_options>
309
310 Setup the worker specific options.
311  
312 my @active_workers = $class->load_options($app_prove_object, \@ARGV);
313  
314 Returns boolean.
315  
316 =cut
317  
318 0 ▶sub load_options {
Subroutine load_options: not called CC=6 SCAR=37.4
Pod documented
319 0 my $class = shift;
320 0 my ( $app, $args ) = @_;
321 {
322 0 local @ARGV = @$args;
323 0 Getopt::Long::Configure(qw(no_ignore_case bundling pass_through));
324  
325 # Don't add coderefs to GetOptions
326 0 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("use-local-public-ip", \$use_local_public_ip, "sync-test-env=s", \$sync_type, "destination-dir=s", \$destination_dir) or croak("Unable to continue") 0 0 0
327 'use-local-public-ip' => \$use_local_public_ip,
328 'sync-test-env=s' => \$sync_type,
329 'destination-dir=s' => \$destination_dir
330 ) or croak('Unable to continue');
331 0 ▶ if ($sync_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sync_type) 0 0 0
332 0 ▶ if ( $sync_type eq 'rsync' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sync_type eq 'rsync') { } 0 0 0
333 0 require File::Rsync;
334 0 ▶ unless ($destination_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($destination_dir) 0 0 0
335 0 require File::Temp;
336 0 $destination_dir = File::Temp::tempdir( CLEANUP => 1 );
337 }
338  
339 #LSF: This might not support with different directory separator.
340 0 ▶ unless ( $destination_dir =~ /\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($destination_dir =~ m[/$]) 0 0 0
341 0 $destination_dir .= '/';
342 }
343 }
344 else {
345 0 die "not able to sync on the remote with type "
346 . $sync_type
347 . ".\nCurrently, only the rsync type is supported.\n";
348 }
349 }
350 }
351 0 return 1;
352 }
353  
354 1 1;
355  
356 __END__
« blib/lib/App/Prove/Plugin/Distributed.pm ↑ Summary blib/lib/TAP/Parser/Iterator/Worker/PBS.pm »