Manager/Service/State.pm

filter: Statementstmt 75.3% 104 / 138 Branchbran 65.6% 42 / 64 Conditioncond 43.1% 22 / 51 MC/DCmcdc 13.3% 4 / 30 Subroutinesub 68.7% 11 / 16 Podpod 100% 2 / 2 totaltotal 61.4% 185 / 301 CC 53 SCAR 35.2
CC
53
Cov
66%
ipcm_service
60.0
import
20.0
__ANON__
11.0
CRAP 33.8
↑ Summary Manager/Service/Handle.pm »
1 package IPC::Manager::Service::State;
2 454 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 454 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 454 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 454 ▶use IPC::Manager::Util qw/require_mod clone_io/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 454 ▶use IPC::Manager::Serializer::JSON();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 454 ▶use Long::Jump qw/setjump longjump havejump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 454 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 454 my $base_0 = $0;
13 454 my $service_inst;
14  
15 0 ▶my $_post_exec_run = sub { 255 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 sub import {
18 0 ▶ my $class = shift;
Subroutine import: not called CC=4 SCAR=30.0
19  
20 0 my $json = shift @ARGV;
21  
22 0 my $params = IPC::Manager::Serializer::JSON->deserialize($json);
23 0 ▶ my $exec = delete $params->{exec} // {};
Condition0%0/2
delete $params->{"exec"} // {}
l!l
00
MC/DC0%0/1
delete $params->{"exec"} // {}
delete $params->{"exec"}
Truth table0%0/2
A
A= delete $params->{"exec"}
Aresult
11
00
24  
25 $params->{orig_io} = {
26 0 stderr => clone_io('>&', \*STDERR),
27 stdout => clone_io('>&', \*STDOUT),
28 stdin => clone_io('<&', \*STDIN),
29 };
30  
31 0 ▶ my $svc_class = require_mod($params->{class} // 'IPC::Manager::Service');
Condition0%0/2
$params->{"class"} // "IPC::Manager::Service"
l!l
00
MC/DC0%0/1
$params->{"class"} // "IPC::Manager::Service"
$params->{"class"}
Truth table0%0/2
A
A= $params->{"class"}
Aresult
11
00
32 0 my $new_inst = $svc_class->new(%$params);
33  
34 0 my $exit;
35 0 ▶ my $code = sub { _ipcm_service(%$params, new_inst => $new_inst); $exit = 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
36  
37 0 ▶ if ($exec->{stay_in_begin}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->{'stay_in_begin'}) { } 0 0 0
38 0 $exit = 255;
39 0 ▶ $_post_exec_run = sub { $exit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 0 $code->();
41 }
42 else {
43 0 $_post_exec_run = $code;
44 }
45 }
46  
47 sub ipcm_worker {
48 16 ▶ my ($name, $cb) = @_;
Subroutine ipcm_worker: called CC=6 SCAR=19.1
Pod documented
49  
50 16 ▶ croak "ipcm_service_worker() can only be called from inside a service"
Branch: 1 branch
BranchTrueFalseTotal
unless $service_inst and ref $service_inst ne "CODE" 0 16 16
Condition33%1/3
$service_inst and ref $service_inst ne "CODE"
!ll&&!rl&&r
0016
MC/DC0%0/2
$service_inst and ref $service_inst ne "CODE"
$service_inst ref $service_inst ne "CODE"
Truth table33%1/3
A and B
A= $service_instB= ref $service_inst ne "CODE"
ABresult
0X0
100
111
51 unless $service_inst && ref($service_inst) ne 'CODE';
52  
53 16 ▶ my $pid = fork // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
160
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
54 16 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 12 4 16
55 12 $service_inst->register_worker($name => $pid);
56 12 return $pid;
57 }
58  
59 4 $0 = "$0 $name";
60  
61 4 $service_inst = $cb;
62 4 ▶ croak "ipcm_worker: no IPCM_SERVICE jump point on the stack (worker invoked outside a running service?)"
Branch: 1 branch
BranchTrueFalseTotal
unless havejump("IPCM_SERVICE") 0 4 4
63 unless havejump 'IPCM_SERVICE';
64 4 longjump IPCM_SERVICE => ();
65 0 die "This should not be reachable";
66 }
67  
68 sub ipcm_service {
69 268 ▶ my ($name, @args) = @_;
Subroutine ipcm_service: called CC=34 SCAR=41.0
Pod documented
70  
71 268 ▶ my $return_handle = defined(wantarray) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 260 8 268
72  
73 268 my %params;
74 268 ▶ if (@args == 1 && ref($args[0]) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'CODE') { } 16 252 268
elsif (@args == 2 and ref $args[0] eq 'HASH' and ref $args[1] eq 'CODE') { } 0 252 252
Condition66%2/3
@args == 1 and ref $args[0] eq "CODE"
!ll&&!rl&&r
252016
MC/DC50%1/2
@args == 1 and ref $args[0] eq "CODE"
@args == 1 ref $args[0] eq "CODE"
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0] eq "CODE"
ABresult
0X0
100
111
75 16 $params{class} = 'IPC::Manager::Service';
76 16 $params{on_all} = $args[0];
77 }
78 ▶ elsif (@args == 2 && ref($args[0]) eq 'HASH' && ref($args[1]) eq 'CODE') {
Condition33%2/6
@args == 2 and ref $args[0] eq "HASH" and ref $args[1] eq "CODE"
!ll&&!rl&&r
25200
@args == 2 and ref $args[0] eq "HASH"
!ll&&!rl&&r
25200
MC/DC0%0/3
@args == 2 and ref $args[0] eq "HASH" and ref $args[1] eq "CODE"
@args == 2 ref $args[0] eq "HASH" ref $args[1] eq "CODE"
Truth table25%1/4
A and B and C
A= @args == 2B= ref $args[0] eq "HASH"C= ref $args[1] eq "CODE"
ABCresult
0XX0
10X0
1100
1111
79 0 %params = %{$args[0]};
80 0 $params{class} = 'IPC::Manager::Service';
81 0 $params{on_all} = $args[1];
82 }
83 else {
84 252 %params = @args;
85 }
86  
87 268 my $skip_role_checks = delete $params{skip_role_checks};
88  
89 268 my $exec = delete $params{exec};
90  
91 268 $params{name} = $name;
92  
93 268 ▶ $params{orig_io} //= $service_inst ? $service_inst->orig_io : {
Branch: 2 branches
BranchTrueFalseTotal
unless $exec 260 8 268
$service_inst ? : 16 244 260
Condition33%1/3
$params{"orig_io"} //= $service_inst ? $service_inst->orig_io : {"stderr", clone_io(">&", \*STDERR), "stdout", clone_io(">&", \*STDOUT), "stdin", clone_io("<&", \*STDIN)}
l!l&&r!l&&!r
02600
MC/DC0%0/2
$params{"orig_io"} //= $service_inst ? $service_inst->orig_io : {"stderr", clone_io(">&", \*STDERR), "stdout", clone_io(">&", \*STDOUT), "stdin", clone_io("<&", \*STDIN)}
$params{"orig_io"} $service_inst ? $service_inst->orig_io : {"stderr", clone_io(">&", \*STDERR), "stdout", clone_io(">&", \*STDOUT), "stdin", clone_io("<&", \*STDIN)}
Truth table33%1/3
A //= B
A= $params{"orig_io"}B= $service_inst ? $service_inst->orig_io : {"stderr", clone_io(">&", \*STDERR), "stdout", clone_io(">&", \*STDOUT), "stdin", clone_io("<&", \*STDIN)}
ABresult
1X1
011
000
94 stderr => clone_io('>&', \*STDERR),
95 stdout => clone_io('>&', \*STDOUT),
96 stdin => clone_io('<&', \*STDIN),
97 } unless $exec;
98  
99 268 ▶ if ($service_inst && !$params{redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_inst and not $params{"redirect"}) 16 252 268
Condition66%2/3
$service_inst and not $params{"redirect"}
!ll&&!rl&&r
252016
MC/DC50%1/2
$service_inst and not $params{"redirect"}
$service_inst not $params{"redirect"}
Truth table66%2/3
A and not(B)
A= $service_instB= not $params{"redirect"}
ABresult
0X0
100
111
100 16 ▶ if (my $redir = $service_inst->redirect) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $redir = $service_inst->redirect) 0 16 16
101 0 ▶ $params{redirect} = $redir unless defined($redir->{inherit}) && !$redir->{inherit};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $redir->{"inherit"} and not $redir->{"inherit"} 0 0 0
Condition0%0/3
defined $redir->{"inherit"} and not $redir->{"inherit"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $redir->{"inherit"} and not $redir->{"inherit"}
defined $redir->{"inherit"} not $redir->{"inherit"}
Truth table0%0/3
A and not(B)
A= defined $redir->{"inherit"}B= not $redir->{"inherit"}
ABresult
0X0
100
111
102 }
103 }
104  
105 268 my $new_ipcm = delete $params{new_ipcm};
106  
107 268 ▶ my $handle_params = delete $params{handle_params} // {};
Condition50%1/2
delete $params{"handle_params"} // {}
l!l
0268
MC/DC0%0/1
delete $params{"handle_params"} // {}
delete $params{"handle_params"}
Truth table50%1/2
A
A= delete $params{"handle_params"}
Aresult
11
00
108 268 ▶ if ($params{ipcm_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'ipcm_info'}) { } 0 268 268
109 0 ▶ croak "'new_ipcm' and 'ipcm_info' may not be combined" if $new_ipcm;
Branch: 1 branch
BranchTrueFalseTotal
if $new_ipcm 0 0 0
110 }
111 else {
112 268 ▶ if ($service_inst && !$new_ipcm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($service_inst and not $new_ipcm) { } 16 252 268
elsif ($return_handle) { } 252 0 252
Condition66%2/3
$service_inst and not $new_ipcm
!ll&&!rl&&r
252016
MC/DC50%1/2
$service_inst and not $new_ipcm
$service_inst not $new_ipcm
Truth table66%2/3
A and not(B)
A= $service_instB= not $new_ipcm
ABresult
0X0
100
111
113 16 $handle_params->{_peer} = 1;
114 16 $params{ipcm_info} = $service_inst->ipcm_info;
115 }
116 elsif ($return_handle) {
117 252 require IPC::Manager;
118 252 $handle_params->{spawn} = IPC::Manager::ipcm_spawn();
119 252 $params{ipcm_info} = $handle_params->{spawn}->info;
120 252 $params{watch_pids} = [$$];
121 }
122 else {
123 0 croak "Cannot be called in void context without providing 'ipcm_info'";
124 }
125 }
126  
127 268 ▶ my $class = require_mod($params{class} // 'IPC::Manager::Service');
Condition50%1/2
$params{"class"} // "IPC::Manager::Service"
l!l
2680
MC/DC0%0/1
$params{"class"} // "IPC::Manager::Service"
$params{"class"}
Truth table50%1/2
A
A= $params{"class"}
Aresult
11
00
128  
129 268 ▶ croak "'$class' does not implement the 'IPC::Manager::Role::Service' role"
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_role_checks or Role::Tiny::does_role($class, "IPC::Manager::Role::Service") 0 268 268
Condition33%1/3
$skip_role_checks or Role::Tiny::does_role($class, "IPC::Manager::Role::Service")
l!l&&r!l&&!r
02680
MC/DC0%0/2
$skip_role_checks or Role::Tiny::does_role($class, "IPC::Manager::Role::Service")
$skip_role_checks Role::Tiny::does_role($class, "IPC::Manager::Role::Service")
Truth table33%1/3
A or B
A= $skip_role_checksB= Role::Tiny::does_role($class, "IPC::Manager::Role::Service")
ABresult
1X1
011
000
130 unless $skip_role_checks || Role::Tiny::does_role($class, 'IPC::Manager::Role::Service');
131  
132 268 my $new_inst = $class->new(%params);
133  
134 268 $new_inst->pre_fork_hook();
135  
136 268 ▶ $exec->{json} = IPC::Manager::Serializer::JSON->serialize(\%params)
Branch: 1 branch
BranchTrueFalseTotal
if $exec 8 260 268
137 if $exec;
138  
139 268 ▶ my $pid = fork // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
2680
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
140  
141 # In parent
142 268 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 148 120 268
143 148 ▶ return unless $return_handle || $exec;
Branch: 1 branch
BranchTrueFalseTotal
unless $return_handle or $exec 4 144 148
Condition66%2/3
$return_handle or $exec
l!l&&r!l&&!r
14404
MC/DC50%1/2
$return_handle or $exec
$return_handle $exec
Truth table66%2/3
A or B
A= $return_handleB= $exec
ABresult
1X1
011
000
144  
145 144 my $out;
146  
147 144 ▶ if (delete $handle_params->{_peer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $handle_params->{'_peer'}) { } 4 140 144
148 4 $out = $service_inst->peer($params{name}, %$handle_params);
149 }
150 else {
151 140 $out = $new_inst->handle(%$handle_params, name => "service_parent_$$");
152 }
153  
154 144 ▶ my $timeout = $params{timeout} || 10;
Condition50%1/2
$params{'timeout'} || 10
l!l
0144
MC/DC0%0/1
$params{'timeout'} || 10
$params{'timeout'}
Truth table50%1/2
A
A= $params{'timeout'}
Aresult
11
00
155  
156 144 ▶ croak "Timeout waiting for service to come up after ${timeout}s"
Branch: 1 branch
BranchTrueFalseTotal
unless $out->ready($timeout) 0 144 144
157 unless $out->ready($timeout);
158  
159 144 $out->_set_child_pid($pid);
160  
161 144 return $out;
162 }
163  
164 120 $new_inst->post_fork_hook();
165  
166 120 ▶ if ($exec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec) 4 116 120
167 4 ▶ my $cmd = $exec->{cmd} // [];
Condition50%1/2
$exec->{"cmd"} // []
l!l
40
MC/DC0%0/1
$exec->{"cmd"} // []
$exec->{"cmd"}
Truth table50%1/2
A
A= $exec->{"cmd"}
Aresult
11
00
168 4 my $json = $exec->{json};
169  
170 exec(
171 $^X,
172 4 @{$cmd},
173 4 "-M${ \__PACKAGE__ }",
174 4 "-e" => "exit(${ \__PACKAGE__ }\::_post_exec_run())",
175 $json,
176 );
177 }
178  
179 116 @_ = (%params, new_inst => $new_inst);
180 116 goto \&_ipcm_service;
181 }
182  
183 0 ▶sub _post_exec_run { $_post_exec_run->() }
Subroutine _post_exec_run: not called CC=1 SCAR=6.9
184  
185 sub _ipcm_service {
186 116 ▶ my %params = @_;
Subroutine _ipcm_service: called CC=6 SCAR=20.0
187  
188 116 my $name = $params{name};
189  
190 116 my $new_inst = delete $params{new_inst};
191 116 $new_inst->set_pid($$);
192  
193 116 ▶ $0 = delete($params{rebase_0}) ? "$base_0 $name" : "$0 $name";
Branch: 1 branch
BranchTrueFalseTotal
delete $params{'rebase_0'} ? : 0 116 116
194  
195 # Figure out if we already had a service
196 116 ▶ my $prev_service = $service_inst ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$service_inst ? : 8 108 116
197  
198 # Set the new instance as the current one
199 116 $service_inst = $new_inst;
200  
201 # If a service is already running on the stack, unwind back to its
202 # setjump so the new $service_inst replaces it rather than nesting
203 # another run loop on top.
204 116 ▶ if ($prev_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_service) 8 108 116
205 8 ▶ croak "ipcm_service: previous service detected but no IPCM_SERVICE jump point on the stack"
Branch: 1 branch
BranchTrueFalseTotal
unless havejump("IPCM_SERVICE") 0 8 8
206 unless havejump 'IPCM_SERVICE';
207 8 longjump IPCM_SERVICE => ();
208 0 die "This should not be reachable";
209 }
210  
211 # Top-level service: enter the setjump loop. ipcm_worker and nested
212 # ipcm_service() calls from inside the running service both unwind
213 # back here via `longjump IPCM_SERVICE`, after which the loop re-runs
214 # setjump() with $service_inst pointing at the replacement.
215 108 while (1) {
216 my $jumped = setjump IPCM_SERVICE => sub {
217 # Snapshot: $service_inst can be replaced before this block exits.
218 120 ▶ my $using_service = $service_inst;
Subroutine __ANON__: called CC=7 SCAR=24.0
219  
220 120 ▶ my $do_posix_exit = ref($using_service) ne 'CODE' && $using_service->use_posix_exit;
Condition66%2/3
ref $using_service ne 'CODE' && $using_service->use_posix_exit
!ll&&!rl&&r
41160
MC/DC0%0/2
ref $using_service ne 'CODE' && $using_service->use_posix_exit
ref $using_service ne 'CODE' $using_service->use_posix_exit
Truth table66%2/3
A && B
A= ref $using_service ne 'CODE'B= $using_service->use_posix_exit
ABresult
0X0
100
111
221  
222 120 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if (ref $using_service eq "CODE") { my $exit = &$using_service(); exit $exit; } else { my $exit = $using_service->run // 0; $do_posix_exit ? POSIX::_exit($exit) : exit $exit; } ; 1 } } or warn $@ 0 0 0
223 120 ▶ if (ref($using_service) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $using_service eq 'CODE') { } 4 116 120
224 4 my $exit = $using_service->();
225 0 exit($exit);
226 }
227 else {
228 116 ▶ my $exit = $using_service->run() // 0;
Condition50%1/2
$using_service->run // 0
l!l
1040
MC/DC0%0/1
$using_service->run // 0
$using_service->run
Truth table50%1/2
A
A= $using_service->run
Aresult
11
00
229 104 ▶ $do_posix_exit ? POSIX::_exit($exit) : exit($exit);
Branch: 1 branch
BranchTrueFalseTotal
$do_posix_exit ? : 0 104 104
230 }
231 0 1;
232 } or warn $@;
233  
234 0 ▶ $do_posix_exit ? POSIX::_exit(255) : exit(255);
Branch: 1 branch
BranchTrueFalseTotal
$do_posix_exit ? : 0 0 0
235 120 };
236  
237 # setjump returns undef if the sub returned normally (never, in
238 # practice -- we always exit()). Any other value means longjump
239 # was called and we loop with the updated $service_inst.
240 12 ▶ last unless $jumped;
Branch: 1 branch
BranchTrueFalseTotal
unless $jumped 0 12 12
241 }
242  
243 # This should not be reachable....
244 0 eval { warn "Scope leak in service '$name' process $$" };
245 0 POSIX::_exit(255);
246 }
247  
248 454 1;
249  
250 __END__
↑ Summary Manager/Service/Handle.pm »