blib/lib/Proc/Topus.pm

filter: Statementstmt 100% 112 / 112 Branchbran 86.6% 52 / 60 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 96.0% 193 / 201 CC 39 SCAR 21.4
CC
39
Cov
96%
spawn
16.0
_alloc_pairs
8.0
_alloc_pair
6.1
CRAP 8.5
↑ Summary
1 package Proc::Topus;
2  
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 34 our $VERSION = '0.02';
11  
12 34 our @ISA = qw( Exporter );
13 34 our @EXPORT_OK = qw( spawn );
14  
15  
16 sub _CONDUIT () { 'socketpair' }
17 sub _PARENT () { 0 }
18 sub _WORKER () { 1 }
19 sub _READER () { 0 }
20 sub _WRITER () { 1 }
21  
22  
23 193 ▶sub spawn {
Subroutine spawn: called CC=16 SCAR=27.7
Pod documented
24 193 my $args = _args( @_ );
25  
26 187 my $workers = $args->{workers};
27 187 ▶ die 'No workers defined'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $workers and keys %$workers > 0 4 183 187
Condition100%3/3
defined $workers and keys %$workers > 0
!ll&&!rl&&r
22183
MC/DC100%2/2
defined $workers and keys %$workers > 0
defined $workers keys %$workers > 0
Truth table100%3/3
A and B
A= defined $workersB= keys %$workers > 0
ABresult
0X0
100
111
28 unless defined $workers && keys %$workers > 0;
29  
30 183 my $pairs = _alloc_pairs( $workers, $args->{conduit}, $args->{autoflush} );
31  
32 177 my %pids;
33 177 for my $name ( keys %$workers ) {
34 177 my $config = $workers->{$name};
35  
36 177 my $pid = fork;
37 177 ▶ defined $pid
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or die "fork: ", $! 177 0 177
38 or die 'fork: ', $!;
39  
40 177 $pids{$pid} = $name;
41 next
42 177 ▶ if $pid != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pid != 0 146 31 177
43  
44 31 my @pairs = map { $_->[_WORKER] } @{ delete $pairs->{$name} };
45 delete $pairs->{$_}
46 31 for keys %$pairs;
47  
48 31 ▶ if( $config->{setsid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"setsid"}) 3 28 31
49 3 require POSIX;
50 3 ▶ POSIX::setsid()
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() or die "setsid: ", $! 3 0 3
51 or die 'setsid: ', $!;
52 }
53  
54 31 my $loader = $config->{loader};
55 31 ▶ $loader->( @pairs )
Branch: 1 branch
BranchTrueFalseTotal
if ref $loader eq "CODE" 4 27 31
56 if ref $loader eq 'CODE';
57  
58 30 ▶ while( @pairs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs) 50 10 60
59 50 my $pair = shift @pairs;
60  
61 50 my $pid = fork;
62 50 ▶ defined $pid
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or die "fork: ", $! 50 0 50
63 or die 'fork: ', $!;
64  
65 next
66 50 ▶ if $pid != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pid != 0 30 20 50
67  
68 20 splice @pairs;
69  
70 20 my $main = $config->{main};
71 18 ▶ $main = sub { $pair }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 20 ▶ unless ref $main eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $main eq "CODE" 18 2 20
73  
74 20 return $main->( @$pair[_READER, _WRITER] );
75 }
76  
77 10 exit 0;
78 }
79  
80 146 for my $name ( keys %$pairs ) {
81 $pairs->{$name} = [
82 291 map { { reader => $_->[_READER], writer => $_->[_WRITER] } }
83 291 map { $_->[_PARENT] }
84 146 @{ $pairs->{$name} }
85 ];
86 }
87  
88 146 _wait( \%pids );
89  
90 145 my $main = $args->{main};
91 120 ▶ $main = sub { $pairs }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 145 ▶ unless ref $main eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $main eq "CODE" 120 25 145
93  
94 145 $main->( $pairs )
95 }
96  
97 193 ▶sub _args {
Subroutine _args: called CC=5 SCAR=16.1
98 193 ▶ if( scalar @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1) { } 179 14 193
elsif (@_ % 2) { } 2 12 14
99 179 ▶ unless( defined $_[0] && ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0] and ref $_[0] eq "HASH") 4 175 179
Condition100%3/3
defined $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
22175
MC/DC100%2/2
defined $_[0] and ref $_[0] eq "HASH"
defined $_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= defined $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
100 4 die "Single arguments must be a HASH ref";
101 }
102  
103 175 return $_[0]
104 }
105 elsif( @_ % 2 ) {
106 2 die "Odd number of arguments";
107 }
108 else {
109 12 return { @_ };
110 }
111 }
112  
113 183 ▶sub _alloc_pairs {
Subroutine _alloc_pairs: called CC=8 SCAR=20.8
114 183 my ( $workers, $_conduit, $_autoflush ) = @_;
115  
116 183 ▶ $_conduit = _CONDUIT
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_conduit 139 44 183
117 unless defined $_conduit;
118  
119 183 ▶ $_autoflush = 1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_autoflush 163 20 183
120 unless defined $_autoflush;
121  
122 183 my %pairs;
123 183 for my $name ( keys %$workers ) {
124 183 my $config = $workers->{$name};
125  
126 183 my $count = $config->{count};
127 183 ▶ die "Invalid worker count ($name)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count and $count > 0 4 179 183
Condition100%3/3
defined $count and $count > 0
!ll&&!rl&&r
22179
MC/DC100%2/2
defined $count and $count > 0
defined $count $count > 0
Truth table100%3/3
A and B
A= defined $countB= $count > 0
ABresult
0X0
100
111
128 unless defined $count && $count > 0;
129  
130 179 my $conduit = $config->{conduit};
131 179 ▶ $conduit = $_conduit
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conduit 139 40 179
132 unless defined $conduit;
133  
134 179 my $autoflush = $config->{autoflush};
135 179 ▶ $autoflush = $_autoflush
Branch: 1 branch
BranchTrueFalseTotal
unless defined $autoflush 165 14 179
136 unless defined $autoflush;
137  
138 $pairs{$name} =
139 179 [ map { _alloc_pair( $name, $conduit, $autoflush ) } 1 .. $count ];
140 }
141  
142 177 \%pairs
143 }
144  
145 354 ▶sub _alloc_pair {
Subroutine _alloc_pair: called CC=6 SCAR=18.1
146 354 my ( $name, $conduit, $autoflush ) = @_;
147  
148 354 ▶ if( $conduit eq 'socketpair' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($conduit eq 'socketpair') { } 282 72 354
elsif ($conduit eq 'pipe') { } 70 2 72
149 282 ▶ socketpair my $ps, my $ws, AF_UNIX, SOCK_STREAM, PF_UNSPEC
Branch: 1 branch
BranchTrueFalseTotal
socketpair my $ps, my $ws, 1, 1, 0 or die "socketpair: ", $! 282 0 282
150 or die 'socketpair: ', $!;
151  
152 282 _autoflush( $autoflush, $ps, $ws );
153  
154 282 return _pair( $ps, $ps, $ws, $ws );
155 }
156 elsif( $conduit eq 'pipe' ) {
157 70 ▶ pipe my $pr, my $ww
Branch: 1 branch
BranchTrueFalseTotal
pipe my $pr, my $ww or die "pipe: ", $! 70 0 70
158 or die 'pipe: ', $!;
159 70 ▶ pipe my $wr, my $pw
Branch: 1 branch
BranchTrueFalseTotal
pipe my $wr, my $pw or die "pipe: ", $! 70 0 70
160 or die 'pipe: ', $!;
161  
162 70 _autoflush( $autoflush, $pr, $pw, $wr, $ww );
163  
164 70 return _pair( $pr, $pw, $wr, $ww );
165 }
166  
167 2 die "Invalid worker conduit '$conduit' for worker '$name'";
168 }
169  
170 352 ▶sub _autoflush {
Subroutine _autoflush: called CC=3 SCAR=11.0
171 return
172 352 ▶ unless shift;
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 46 306 352
173  
174 306 my $fh = select;
175 752 do { select $_; $| = 1 }
176 306 for @_;
177 306 select $fh
178 }
179  
180 352 ▶sub _pair {
Subroutine _pair: called CC=1 SCAR=0.0
181 352 my ( $pr, $pw, $wr, $ww ) = @_;
182  
183 352 my $pp; @$pp[_READER, _WRITER] = ( $pr, $pw );
184 352 my $wp; @$wp[_READER, _WRITER] = ( $wr, $ww );
185  
186 352 my $pair; @$pair[_PARENT, _WORKER] = ( $pp, $wp );
187 352 $pair
188 }
189  
190 146 ▶sub _wait {
Subroutine _wait: called CC=6 SCAR=18.0
191 146 my ( $pids ) = @_;
192  
193 146 my $errors = 0;
194  
195 146 ▶ until( keys %$pids == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %$pids == 0) 146 146 292
196 146 my $pid = waitpid -1, 0;
197 146 ▶ die 'waitpid: ', $!
Branch: 1 branch
BranchTrueFalseTotal
if $pid == -1 0 146 146
198 if $pid == -1;
199  
200 146 my $name = delete $pids->{$pid};
201 next
202 146 ▶ unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 146 146
203  
204 146 my $rc = $? >> 8;
205 146 ▶ $errors++
Branch: 1 branch
BranchTrueFalseTotal
unless $rc == 0 1 145 146
206 unless $rc == 0;
207 }
208  
209 146 ▶ die "One or more loaders failed"
Branch: 1 branch
BranchTrueFalseTotal
if $errors 1 145 146
210 if $errors;
211 }
212  
213  
214 34 1
215 __END__
↑ Summary