blib/lib/IPC/Manager/Service/State.pm

filter: Statementstmt 76.1% 112 / 147 Branchbran 66.6% 48 / 72 Conditioncond 41.6% 25 / 60 MC/DCmcdc 11.7% 4 / 34 Subroutinesub 68.7% 11 / 16 Podpod 100% 2 / 2 totaltotal 61.0% 202 / 331 CC 60 SCAR 35.4
CC
60
Cov
66%
ipcm_service
61.4
import
20.0
__ANON__
19.4
CRAP 34.4
↑ Summary blib/lib/IPC/Manager/Service/Handle.pm »
1 package IPC::Manager::Service::State;
2 456 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 456 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 456 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 456 ▶use IPC::Manager::Util qw/require_mod clone_io/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 456 ▶use IPC::Manager::Serializer::JSON();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 456 ▶use Long::Jump qw/setjump longjump havejump/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 456 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 456 my $base_0 = $0;
13 456 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 269 ▶ my ($name, @args) = @_;
Subroutine ipcm_service: called CC=35 SCAR=41.2
Pod documented
70  
71 269 ▶ my $return_handle = defined(wantarray) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 261 8 269
72  
73 269 my %params;
74 269 ▶ if (@args == 1 && ref($args[0]) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'CODE') { } 16 253 269
elsif (@args == 2 and ref $args[0] eq 'HASH' and ref $args[1] eq 'CODE') { } 0 253 253
Condition66%2/3
@args == 1 and ref $args[0] eq "CODE"
!ll&&!rl&&r
253016
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
25300
@args == 2 and ref $args[0] eq "HASH"
!ll&&!rl&&r
25300
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 253 %params = @args;
85 }
86  
87 269 my $skip_role_checks = delete $params{skip_role_checks};
88  
89 269 my $exec = delete $params{exec};
90  
91 269 $params{name} = $name;
92  
93 269 ▶ $params{orig_io} //= $service_inst ? $service_inst->orig_io : {
Branch: 2 branches
BranchTrueFalseTotal
unless $exec 260 9 269
$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 269 ▶ if ($service_inst && !$params{redirect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_inst and not $params{"redirect"}) 16 253 269
Condition66%2/3
$service_inst and not $params{"redirect"}
!ll&&!rl&&r
253016
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 269 my $new_ipcm = delete $params{new_ipcm};
106  
107 269 ▶ my $handle_params = delete $params{handle_params} // {};
Condition50%1/2
delete $params{"handle_params"} // {}
l!l
0269
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 269 ▶ if ($params{ipcm_info}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'ipcm_info'}) { } 0 269 269
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 269 ▶ if ($service_inst && !$new_ipcm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($service_inst and not $new_ipcm) { } 16 253 269
elsif ($return_handle) { } 253 0 253
Condition66%2/3
$service_inst and not $new_ipcm
!ll&&!rl&&r
253016
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 253 require IPC::Manager;
118 253 $handle_params->{spawn} = IPC::Manager::ipcm_spawn();
119 253 $params{ipcm_info} = $handle_params->{spawn}->info;
120 253 $params{watch_pids} = [$$];
121 }
122 else {
123 0 croak "Cannot be called in void context without providing 'ipcm_info'";
124 }
125 }
126  
127 269 ▶ my $class = require_mod($params{class} // 'IPC::Manager::Service');
Condition50%1/2
$params{"class"} // "IPC::Manager::Service"
l!l
2690
MC/DC0%0/1
$params{"class"} // "IPC::Manager::Service"
$params{"class"}
Truth table50%1/2
A
A= $params{"class"}
Aresult
11
00
128  
129 269 ▶ 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 269 269
Condition33%1/3
$skip_role_checks or Role::Tiny::does_role($class, "IPC::Manager::Role::Service")
l!l&&r!l&&!r
02690
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 269 my $new_inst = $class->new(%params);
133  
134 269 $new_inst->pre_fork_hook();
135  
136 # Forward the stay_in_begin flag (only) into the child's params.
137 # The 'exec' key is otherwise stripped on the parent side and the
138 # child needs to know whether import() should call _ipcm_service
139 # synchronously at BEGIN time (stay_in_begin = 1) or stash the
140 # closure for the -e snippet to fire at runtime.
141 $exec->{json} = IPC::Manager::Serializer::JSON->serialize({
142 %params,
143 269 ▶ exec => { stay_in_begin => $exec->{stay_in_begin} ? 1 : 0 },
Branch: 2 branches
BranchTrueFalseTotal
if $exec 9 260 269
$exec->{'stay_in_begin'} ? : 1 8 9
144 }) if $exec;
145  
146 269 ▶ my $pid = fork // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
2690
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
147  
148 # In parent
149 269 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 148 121 269
150 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
151  
152 144 my $out;
153  
154 144 ▶ if (delete $handle_params->{_peer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $handle_params->{'_peer'}) { } 4 140 144
155 4 $out = $service_inst->peer($params{name}, %$handle_params);
156 }
157 else {
158 140 $out = $new_inst->handle(%$handle_params, name => "service_parent_$$");
159 }
160  
161 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
162  
163 144 ▶ croak "Timeout waiting for service to come up after ${timeout}s"
Branch: 1 branch
BranchTrueFalseTotal
unless $out->ready($timeout) 0 144 144
164 unless $out->ready($timeout);
165  
166 144 $out->_set_child_pid($pid);
167  
168 144 return $out;
169 }
170  
171 121 $new_inst->post_fork_hook();
172  
173 121 ▶ if ($exec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exec) 5 116 121
174 5 ▶ my $cmd = $exec->{cmd} // [];
Condition50%1/2
$exec->{"cmd"} // []
l!l
50
MC/DC0%0/1
$exec->{"cmd"} // []
$exec->{"cmd"}
Truth table50%1/2
A
A= $exec->{"cmd"}
Aresult
11
00
175 5 my $json = $exec->{json};
176  
177 exec(
178 $^X,
179 5 @{$cmd},
180 5 "-M${ \__PACKAGE__ }",
181 5 "-e" => "exit(${ \__PACKAGE__ }\::_post_exec_run())",
182 $json,
183 );
184 }
185  
186 116 @_ = (%params, new_inst => $new_inst);
187 116 goto \&_ipcm_service;
188 }
189  
190 0 ▶sub _post_exec_run { $_post_exec_run->() }
Subroutine _post_exec_run: not called CC=1 SCAR=6.9
191  
192 sub _ipcm_service {
193 116 ▶ my %params = @_;
Subroutine _ipcm_service: called CC=7 SCAR=22.1
194  
195 116 my $name = $params{name};
196  
197 116 my $new_inst = delete $params{new_inst};
198 116 $new_inst->set_pid($$);
199  
200 116 ▶ $0 = delete($params{rebase_0}) ? "$base_0 $name" : "$0 $name";
Branch: 1 branch
BranchTrueFalseTotal
delete $params{'rebase_0'} ? : 0 116 116
201  
202 # Figure out if we already had a service
203 116 ▶ my $prev_service = $service_inst ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$service_inst ? : 8 108 116
204  
205 # Set the new instance as the current one
206 116 $service_inst = $new_inst;
207  
208 # If a service is already running on the stack, unwind back to its
209 # setjump so the new $service_inst replaces it rather than nesting
210 # another run loop on top.
211 116 ▶ if ($prev_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_service) 8 108 116
212 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
213 unless havejump 'IPCM_SERVICE';
214 8 longjump IPCM_SERVICE => ();
215 0 die "This should not be reachable";
216 }
217  
218 # Top-level service: enter the setjump loop. ipcm_worker and nested
219 # ipcm_service() calls from inside the running service both unwind
220 # back here via `longjump IPCM_SERVICE`, after which the loop re-runs
221 # setjump() with $service_inst pointing at the replacement.
222 #
223 # If the running service consumes Role::Service with
224 # run_returns_to_caller() true, run() returning normally stores the
225 # exit code in $returning_exit and lets _ipcm_service return it to
226 # its caller (used by the exec.stay_in_begin = 1 BEGIN-return path).
227 108 my $returning_exit;
228 108 while (1) {
229 my $jumped = setjump IPCM_SERVICE => sub {
230 # Snapshot: $service_inst can be replaced before this block exits.
231 120 ▶ my $using_service = $service_inst;
Subroutine __ANON__: called CC=12 SCAR=29.7
232  
233 120 my $is_code = ref($using_service) eq 'CODE';
234 120 ▶ my $do_posix_exit = !$is_code && $using_service->use_posix_exit;
Condition66%2/3
!$is_code && $using_service->use_posix_exit
!ll&&!rl&&r
41160
MC/DC0%0/2
!$is_code && $using_service->use_posix_exit
!$is_code $using_service->use_posix_exit
Truth table66%2/3
not(A) && B
A= !$is_codeB= $using_service->use_posix_exit
ABresult
0X0
100
111
235  
236 120 my $exit;
237 120 my $ok = eval {
238 120 ▶ if ($is_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_code) { } 4 116 120
239 4 $exit = $using_service->();
240 }
241 else {
242 116 ▶ $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
243 }
244 104 1;
245 };
246 104 ▶ warn $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 104 104
247  
248 104 ▶ if ( $ok
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and not $is_code and $using_service->can("run_returns_to_caller") and $using_service->run_returns_to_caller) 0 104 104
Condition33%3/9
$ok and not $is_code and $using_service->can("run_returns_to_caller") and $using_service->run_returns_to_caller
!ll&&!rl&&r
01040
$ok and not $is_code and $using_service->can("run_returns_to_caller")
!ll&&!rl&&r
00104
$ok and not $is_code
!ll&&!rl&&r
00104
MC/DC0%0/4
$ok and not $is_code and $using_service->can("run_returns_to_caller") and $using_service->run_returns_to_caller
$ok not $is_code $using_service->can("run_returns_to_caller") $using_service->run_returns_to_caller
Truth table20%1/5
A and not(B) and C and D
A= $okB= not $is_codeC= $using_service->can("run_returns_to_caller")D= $using_service->run_returns_to_caller
ABCDresult
0XXX0
10XX0
110X0
11100
11111
249 && !$is_code
250 && $using_service->can('run_returns_to_caller')
251 && $using_service->run_returns_to_caller)
252 {
253 0 $returning_exit = \$exit;
254 0 return;
255 }
256  
257 104 ▶ my $code = $ok ? $exit : 255;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 104 0 104
258 104 ▶ if ($is_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_code) { } 0 104 104
259 0 exit($code);
260 }
261 else {
262 104 ▶ $do_posix_exit ? POSIX::_exit($code) : exit($code);
Branch: 1 branch
BranchTrueFalseTotal
$do_posix_exit ? : 0 104 104
263 }
264 120 };
265  
266 # setjump returns undef if the sub returned normally. Two
267 # cases reach here: (a) the run-returns-to-caller opt-in
268 # stored an exit in $returning_exit -- break and let
269 # _ipcm_service return it; (b) the (unexpected) scope-leak
270 # path -- fall through to the warn + _exit(255) below. Any
271 # other value means longjump was called and we re-run setjump
272 # with the updated $service_inst.
273 12 ▶ last unless $jumped;
Branch: 1 branch
BranchTrueFalseTotal
unless $jumped 0 12 12
274 }
275  
276 0 ▶ return $$returning_exit if $returning_exit;
Branch: 1 branch
BranchTrueFalseTotal
if $returning_exit 0 0 0
277  
278 # This should not be reachable....
279 0 eval { warn "Scope leak in service '$name' process $$" };
280 0 POSIX::_exit(255);
281 }
282  
283 456 1;
284  
285 __END__
↑ Summary blib/lib/IPC/Manager/Service/Handle.pm »