Manager/Test.pm

filter: Statementstmt 89.9% 686 / 763 Branchbran 57.5% 114 / 198 Conditioncond 50.5% 50 / 99 MC/DCmcdc 25.8% 16 / 62 Subroutinesub 91.0% 92 / 101 Podpod 93.5% 29 / 31 totaltotal 78.7% 987 / 1254 CC 146 SCAR 18.8
CC
146
Cov
80%
_run_subtest_in_child
23.7
test_sync_request_peer_exits_after_response
20.9
tests
12.9
CRAP 6.5
« Manager/Client/ConnectionUnix.pm ↑ Summary Manager.pm »
1 package IPC::Manager::Test;
2 452 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 452 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 452 ▶use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 452 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 452 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 452 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 452 ▶use POSIX();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 452 ▶use Test2::V1 -ip;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 452 ▶use Test2::IPC;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 452 ▶use IPC::Manager::Serializer::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 452 ▶use IPC::Manager::Util qw/pid_is_running/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 452 ▶use IPC::Manager qw/ipcm_service ipcm_worker ipcm_connect ipcm_reconnect ipcm_spawn ipcm_default_protocol/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub run_all {
17 0 ▶ my $class = shift;
Subroutine run_all: not called CC=3 SCAR=24.8
Pod documented
18 0 my %params = @_;
19  
20 0 ▶ my $protocol = $params{protocol} or croak "'protocol' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $protocol = $params{"protocol"} or croak("'protocol' is required") 0 0 0
21 0 ipcm_default_protocol($protocol);
22  
23 0 for my $test ($class->tests) {
24 0 $class->_run_subtest_in_child($test);
25 }
26 }
27  
28 sub run_one {
29 340 ▶ my $class = shift;
Subroutine run_one: called CC=5 SCAR=17.5
Pod undocumented
30 340 my %params = @_;
31  
32 340 ▶ my $protocol = $params{protocol} or croak "'protocol' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $protocol = $params{"protocol"} or croak("'protocol' is required") 0 340 340
33 340 ▶ my $test = $params{test} or croak "'test' is required";
Branch: 1 branch
BranchTrueFalseTotal
my $test = $params{"test"} or croak("'test' is required") 0 340 340
34  
35 340 ▶ croak "No such test '$test'" unless $test =~ m/^test_/ && $class->can($test);
Branch: 1 branch
BranchTrueFalseTotal
unless $test =~ /^test_/ and $class->can($test) 0 340 340
Condition33%1/3
$test =~ /^test_/ and $class->can($test)
!ll&&!rl&&r
00340
MC/DC0%0/2
$test =~ /^test_/ and $class->can($test)
$test =~ /^test_/ $class->can($test)
Truth table33%1/3
A and B
A= $test =~ /^test_/B= $class->can($test)
ABresult
0X0
100
111
36  
37 340 ipcm_default_protocol($protocol);
38  
39 340 $class->_run_subtest_in_child($test);
40 }
41  
42 sub _run_subtest_in_child {
43 340 ▶ my ($class, $test) = @_;
Subroutine _run_subtest_in_child: called CC=11 SCAR=31.7
44  
45 340 ▶ my $timeout = $ENV{IPC_MANAGER_TEST_TIMEOUT} || 180;
Condition50%1/2
$ENV{'IPC_MANAGER_TEST_TIMEOUT'} || 180
l!l
0340
MC/DC0%0/1
$ENV{'IPC_MANAGER_TEST_TIMEOUT'} || 180
$ENV{'IPC_MANAGER_TEST_TIMEOUT'}
Truth table50%1/2
A
A= $ENV{'IPC_MANAGER_TEST_TIMEOUT'}
Aresult
11
00
46  
47 340 ▶ my $pid = fork // die "Could not fork: $!";
Condition50%1/2
fork // die("Could not fork: $!")
l!l
3400
MC/DC0%0/1
fork // die("Could not fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
48 340 ▶ if (!$pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 228 112 340
49 # Become the leader of a new process group so the parent can kill
50 # the whole subtree (services, workers, double-forked daemons) on
51 # timeout via kill(-$$). POSIX::setpgid(0, 0) is a no-op on platforms
52 # without process groups; ignore failures rather than abort.
53 228 eval { POSIX::setpgid(0, 0) };
54  
55 228 my $ok = eval { subtest $test => $class->can($test); 1 };
56 112 my $err = $@;
57 112 ▶ warn $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 112 112
58 112 ▶ exit($ok ? 0 : 255);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 112 0 112
59 }
60 # Mirror the setpgid in the parent to close the race where the parent
61 # signals before the child runs setpgid itself. Either side wins; both
62 # idempotent.
63 112 eval { POSIX::setpgid($pid, $pid) };
64  
65 # Parent. On timeout, kill the subtest child (and the process group it
66 # leads) before confessing so any grandchildren — service processes,
67 # workers, etc. — are reaped instead of being orphaned to keep writing
68 # Test2::IPC events into the parent's tempdir, which would later trip
69 # "Leftover files in the directory" bailouts at process exit.
70 112 my $timed_out = 0;
71 112 ▶ local $SIG{ALRM} = sub { $timed_out = 1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
72  
73 112 alarm $timeout;
74 112 waitpid($pid, 0);
75 112 alarm 0;
76  
77 112 ▶ if ($timed_out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed_out) 0 112 112
78 0 local $SIG{ALRM} = 'IGNORE';
79 0 kill 'TERM', $pid, -$pid;
80 # Give the child a moment to exit cleanly, then escalate.
81 0 my $deadline = Time::HiRes::time() + 2;
82 0 ▶ while (kill(0, $pid) && Time::HiRes::time() < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid and &Time::HiRes::time() < $deadline) 0 0 0
Condition0%0/3
kill 0, $pid and &Time::HiRes::time() < $deadline
!ll&&!rl&&r
000
MC/DC0%0/2
kill 0, $pid and &Time::HiRes::time() < $deadline
kill 0, $pid &Time::HiRes::time() < $deadline
Truth table0%0/3
A and B
A= kill 0, $pidB= &Time::HiRes::time() < $deadline
ABresult
0X0
100
111
83 0 ▶ last if waitpid($pid, POSIX::WNOHANG()) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if waitpid($pid, 1) > 0 0 0 0
84 0 Time::HiRes::sleep(0.05);
85 }
86 0 ▶ if (kill(0, $pid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
87 0 kill 'KILL', $pid, -$pid;
88 0 waitpid($pid, 0);
89 }
90 0 confess("Subtest '$test' timed out after ${timeout} seconds");
91 }
92  
93 112 return;
94 }
95  
96 sub tests {
97 0 ▶ my $class = shift;
Subroutine tests: not called CC=4 SCAR=25.6
Pod documented
98  
99 452 ▶ my $stash = do { no strict 'refs'; \%{"$class\::"} };
Subroutine BEGIN: called CC=1 SCAR=1.2
100  
101 0 my @out;
102  
103 0 for my $sym (sort keys %$stash) {
104 0 ▶ next unless $sym =~ m/^test_/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sym =~ /^test_/ 0 0 0
105 0 ▶ next unless $class->can($sym);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($sym) 0 0 0
106  
107 0 push @out => $sym;
108 }
109  
110 0 return @out;
111 }
112  
113 sub test_simple_service {
114 8 ▶ my $parent_pid = $$;
Subroutine test_simple_service: called CC=1 SCAR=0.0
Pod documented
115  
116 8 my $got_req = 0;
117 my $handle = ipcm_service foo => sub {
118 10 ▶ my $self = shift;
Subroutine __ANON__: called CC=8 SCAR=20.8
119 10 my ($activity) = @_;
120  
121 10 ▶ return if $activity->{interval} && keys(%$activity) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $activity->{"interval"} and keys %$activity == 1 1 9 10
Condition100%3/3
$activity->{"interval"} and keys %$activity == 1
!ll&&!rl&&r
541
MC/DC100%2/2
$activity->{"interval"} and keys %$activity == 1
$activity->{"interval"} keys %$activity == 1
Truth table100%3/3
A and B
A= $activity->{"interval"}B= keys %$activity == 1
ABresult
0X0
100
111
122  
123 9 ▶ if (my $msgs = $activity->{messages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msgs = $activity->{"messages"}) 8 1 9
124 8 for my $msg (@$msgs) {
125 8 my $c = $msg->content;
126  
127 8 ▶ if (ref($c) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq "HASH") 8 0 8
128 8 ▶ if ($c->{ipcm_request_id}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->{'ipcm_request_id'}) { } 4 4 8
elsif ($c->{'terminate'}) { } 4 0 4
129 4 $got_req = 1;
130 4 is($msg->content->{request}, "${parent_pid} blah?", "Got request from parent pid");
131 4 $self->send_response($msg->from, $c->{ipcm_request_id}, "$$ blah!");
132 }
133 elsif ($c->{terminate}) {
134 4 ok($got_req, "$$ Got the request!");
135 }
136 }
137 }
138 }
139 8 };
140  
141 4 my $service_pid = $handle->service_pid;
142  
143 4 my $got_resp = 0;
144 $handle->send_request(
145 foo => "$$ blah?",
146 sub {
147 4 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
148 4 $got_resp = 1;
149 4 is($msg->content->{response}, "${service_pid} blah!", "Got expected response");
150 }
151 4 );
152  
153 4 $handle->await_all_responses;
154  
155 4 ok($got_resp, "Got response!");
156  
157 4 $handle = undef;
158 }
159  
160 sub test_generic {
161 4 ▶ my $guard = ipcm_spawn(do_sanity_check => 1);
Subroutine test_generic: called CC=7 SCAR=19.5
Pod documented
162 4 my $info = "$guard";
163  
164 4 isa_ok($guard, ['IPC::Manager::Spawn'], "Got a spawn object");
165 4 is($info, $guard->info, "Stringifies");
166 4 like(
167 IPC::Manager::Serializer::JSON->deserialize($info),
168 [ipcm_default_protocol(), $guard->serializer, $guard->route],
169 "Got a useful info string"
170 );
171 4 note("Info: $info");
172  
173 4 my $con1 = ipcm_connect('con1' => $info);
174 4 my $con2 = ipcm_connect('con2' => $info);
175 4 note("Con: $con1");
176  
177 4 isa_ok($con1, ['IPC::Manager::Client'], "Got a connection (con1)");
178 4 isa_ok($con2, ['IPC::Manager::Client'], "Got a connection (con2)");
179  
180 4 like([$con1->get_messages], [], "No messages");
181 4 like([$con2->get_messages], [], "No messages");
182  
183 4 $con1->send_message(con2 => {hi => 'there'});
184 4 $con2->send_message(con1 => {ahoy => 'matey'});
185  
186 4 like(
187 [$con1->get_messages],
188 [{
189 id => T(),
190 stamp => T(),
191 from => 'con2',
192 to => 'con1',
193 content => {ahoy => 'matey'},
194 }],
195 "Got message sent from con2 to con1"
196 );
197  
198 4 like(
199 [$con2->get_messages],
200 [{
201 id => T(),
202 stamp => T(),
203 from => 'con1',
204 to => 'con2',
205 content => {hi => 'there'},
206 }],
207 "Got message sent from con1 to con2"
208 );
209  
210 4 like([$con1->get_messages], [], "No messages");
211 4 like([$con2->get_messages], [], "No messages");
212  
213 4 $con1->send_message(con2 => "string message!");
214 4 like(
215 [$con2->get_messages],
216 [{
217 id => T(),
218 stamp => T(),
219 from => 'con1',
220 to => 'con2',
221 content => "string message!",
222 }],
223 "Got message sent from con1 to con2"
224 );
225  
226 4 my $con3 = ipcm_connect('con3' => $info);
227  
228 4 $con3->broadcast({mass => 'message'});
229  
230 4 like(
231 [$con1->get_messages],
232 [{
233 id => T(),
234 stamp => T(),
235 from => 'con3',
236 to => 'con1',
237 content => {mass => 'message'},
238 }],
239 "Got broadcast (3 -> 1)"
240 );
241  
242 4 like(
243 [$con2->get_messages],
244 [{
245 id => T(),
246 stamp => T(),
247 from => 'con3',
248 to => 'con2',
249 content => {mass => 'message'},
250 }],
251 "Got broadcast (3 -> 2)"
252 );
253  
254 4 like(
255 [$con3->get_messages],
256 [],
257 "No broadcast (3 -> 3)"
258 );
259  
260 4 $con3->broadcast({mass => 'message2'});
261 4 $con3->broadcast({mass => 'message3'});
262  
263 # Non-blocking sockets may not deliver all datagrams instantly,
264 # so poll until we have the expected count.
265 4 my (@con1_msgs, @con2_msgs);
266 4 my $deadline = Time::HiRes::time() + 5;
267 4 ▶ until (@con1_msgs >= 2 && @con2_msgs >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@con1_msgs >= 2 and @con2_msgs >= 2) 4 4 8
Condition66%2/3
@con1_msgs >= 2 and @con2_msgs >= 2
!ll&&!rl&&r
404
MC/DC50%1/2
@con1_msgs >= 2 and @con2_msgs >= 2
@con1_msgs >= 2 @con2_msgs >= 2
Truth table66%2/3
A and B
A= @con1_msgs >= 2B= @con2_msgs >= 2
ABresult
0X0
100
111
268 4 push @con1_msgs => $con1->get_messages;
269 4 push @con2_msgs => $con2->get_messages;
270 4 ▶ last if Time::HiRes::time() > $deadline;
Branch: 1 branch
BranchTrueFalseTotal
if &Time::HiRes::time() > $deadline 0 4 4
271 4 ▶ Time::HiRes::sleep(0.05) unless @con1_msgs >= 2 && @con2_msgs >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @con1_msgs >= 2 and @con2_msgs >= 2 0 4 4
Condition33%1/3
@con1_msgs >= 2 and @con2_msgs >= 2
!ll&&!rl&&r
004
MC/DC0%0/2
@con1_msgs >= 2 and @con2_msgs >= 2
@con1_msgs >= 2 @con2_msgs >= 2
Truth table33%1/3
A and B
A= @con1_msgs >= 2B= @con2_msgs >= 2
ABresult
0X0
100
111
272 }
273 4 is(\@con1_msgs, [T(), T()], "Got 2 more");
274 4 is(\@con2_msgs, [T(), T()], "Got 2 more");
275  
276 4 $con1->send_message(con2 => 'woosh, I am invisible');
277  
278 4 my $stats = {};
279 4 for my $con ($con1, $con2, $con3) {
280 12 $con->write_stats;
281 12 $stats->{$con->id} = $con->read_stats;
282 }
283  
284 is(
285 4 $stats,
286 {
287 'con1' => {
288 'read' => {'con2' => 1, 'con3' => 3},
289 'sent' => {'con2' => 3},
290 },
291 'con2' => {
292 'read' => {'con1' => 2, 'con3' => 3},
293 'sent' => {'con1' => 1},
294 },
295 'con3' => {
296 'read' => {},
297 'sent' => {'con1' => 3, 'con2' => 3},
298 }
299 },
300 "Got expected stats"
301 );
302  
303 is(
304 4 ▶ warnings { $guard = undef },
Subroutine __ANON__: called CC=1 SCAR=0.0
305 [
306 4 match qr/Messages waiting at disconnect for con2/,
307 match qr/Messages sent vs received mismatch:.*1 con1 -> con2/s,
308 ],
309 "Got warnings"
310 );
311 4 ok(!-e $info, "Info does not exist on the filesystem");
312 }
313  
314 sub test_nested_services {
315 12 ▶ my $parent_pid = $$;
Subroutine test_nested_services: called CC=1 SCAR=0.0
Pod documented
316  
317 my $outer_handle = ipcm_service(
318 'outer_svc',
319 class => 'IPC::Manager::Service',
320 on_start => sub {
321 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
322  
323 # Start a nested inner service in void context (same ipcm bus).
324 # ipcm_service detects we are inside a service and reuses the
325 # existing ipcm_info; the peer is retrieved later via $self->peer.
326 ipcm_service 'inner_svc' => sub {
327 11 ▶ my ($inner_self, $activity) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
328  
329 11 ▶ return if $activity->{interval} && keys(%$activity) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $activity->{"interval"} and keys %$activity == 1 3 8 11
Condition100%3/3
$activity->{"interval"} and keys %$activity == 1
!ll&&!rl&&r
533
MC/DC100%2/2
$activity->{"interval"} and keys %$activity == 1
$activity->{"interval"} keys %$activity == 1
Truth table100%3/3
A and B
A= $activity->{"interval"}B= keys %$activity == 1
ABresult
0X0
100
111
330  
331 8 ▶ if (my $msgs = $activity->{messages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msgs = $activity->{"messages"}) 8 0 8
332 8 for my $msg (@$msgs) {
333 8 my $c = $msg->content;
334 8 ▶ if (ref($c) eq 'HASH' && $c->{ipcm_request_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c eq "HASH" and $c->{"ipcm_request_id"}) 4 4 8
Condition66%2/3
ref $c eq "HASH" and $c->{"ipcm_request_id"}
!ll&&!rl&&r
044
MC/DC50%1/2
ref $c eq "HASH" and $c->{"ipcm_request_id"}
ref $c eq "HASH" $c->{"ipcm_request_id"}
Truth table66%2/3
A and B
A= ref $c eq "HASH"B= $c->{"ipcm_request_id"}
ABresult
0X0
100
111
335 $inner_self->send_response(
336 $msg->from,
337 $c->{ipcm_request_id},
338 4 "inner: $c->{request}",
339 );
340 }
341 }
342 }
343 8 };
344  
345 # Void-context ipcm_service does not wait for the service to be
346 # ready, so block here before returning from on_start. Without
347 # this, a request from the test process could arrive before
348 # inner_svc has connected to the bus, causing a stall.
349 4 $self->peer('inner_svc')->ready(0);
350 },
351  
352 # Receive a request from the test process, forward it to the nested
353 # inner service, and defer the response until the inner service replies.
354 handle_request => sub {
355 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
356  
357 4 my $orig_id = $req->{ipcm_request_id};
358 4 my $orig_from = $msg->from;
359  
360 $self->peer('inner_svc')->send_request(
361 $req->{request},
362 sub {
363 4 ▶ my ($resp, $resp_msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
364 4 $self->send_response($orig_from, $orig_id, "outer: $resp->{response}");
365 }
366 4 );
367  
368 4 return (); # Deferred - will respond via callback above
369 },
370  
371 # IPC::Manager::Service's handle_response calls the user-supplied
372 # callback instead of the role's response-tracking machinery, so we
373 # have to dispatch _RESPONSE_HANDLER callbacks ourselves.
374 handle_response => sub {
375 4 ▶ my ($self, $resp, $msg) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
376 4 my $id = $resp->{ipcm_response_id};
377 4 ▶ if (my $entry = delete $self->{_RESPONSE_HANDLER}->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = delete $self->{"_RESPONSE_HANDLER"}{$id}) 4 0 4
378 4 $entry->{cb}->($resp, $msg);
379 }
380 },
381 12 );
382  
383 4 my $outer_svc_pid = $outer_handle->service_pid;
384 4 isnt($outer_svc_pid, $parent_pid, "Outer service runs in a different process");
385  
386 4 my $got_resp = 0;
387 $outer_handle->send_request(
388 outer_svc => "hello",
389 sub {
390 4 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
391 4 $got_resp = 1;
392 4 is($resp->{response}, "outer: inner: hello", "Got chained response through nested services");
393 }
394 4 );
395  
396 4 $outer_handle->await_all_responses;
397  
398 4 ok($got_resp, "Got response from nested service chain");
399  
400 4 $outer_handle = undef;
401 }
402  
403 sub test_exec_service {
404 8 ▶ my $parent_pid = $$;
Subroutine test_exec_service: called CC=1 SCAR=0.0
Pod documented
405  
406 # Build -I flags so the exec'd process can find our libs
407 8 my @inc_flags = map { "-I$_" } grep { !ref($_) } @INC;
408  
409 8 my $got_resp = 0;
410 8 my $handle = ipcm_service(
411 'exec_svc',
412 class => 'IPC::Manager::Service::Echo',
413 exec => {cmd => \@inc_flags},
414 );
415  
416 4 my $service_pid = $handle->service_pid;
417 4 isnt($service_pid, $parent_pid, "Exec service runs in a different process");
418  
419 $handle->send_request(
420 exec_svc => "ping",
421 sub {
422 4 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
423 4 $got_resp = 1;
424 4 is($resp->{response}, "echo: ping", "Got expected response from exec'd service");
425 },
426 4 );
427  
428 4 $handle->await_all_responses;
429  
430 4 ok($got_resp, "Got response from exec'd service");
431  
432 4 $handle = undef;
433 }
434  
435 sub test_workers {
436 12 ▶ my $parent_pid = $$;
Subroutine test_workers: called CC=8 SCAR=23.2
Pod documented
437  
438 12 my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
439 12 my $marker_file = File::Spec->catfile($marker_dir, 'worker_ran');
440 12 my $longpid_file = File::Spec->catfile($marker_dir, 'long_worker_pid');
441 12 my $killed_file = File::Spec->catfile($marker_dir, 'long_worker_killed');
442  
443 my $handle = ipcm_service(
444 'worker_svc',
445 class => 'IPC::Manager::Service',
446 on_start => sub {
447 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.4
448  
449 # Worker that does a short task and exits
450 ipcm_worker short_worker => sub {
451 0 ▶ open my $fh, '>', $marker_file or die "open: $!";
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $marker_file or die "open: $!" 0 0 0
452 0 print $fh "$$\n";
453 0 close $fh;
454 0 return 0;
455 8 };
456  
457 # Worker that runs until killed
458 ipcm_worker long_worker => sub {
459 4 ▶ my $kfile = $killed_file;
Subroutine __ANON__: called CC=3 SCAR=11.3
460 $SIG{TERM} = sub {
461 4 ▶ open my $kfh, '>', $kfile or die "open: $!";
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
open my $kfh, ">", $kfile or die "open: $!" 0 4 4
462 4 print $kfh "$$\n";
463 4 close $kfh;
464 4 exit 0;
465 4 };
466  
467 # Write pid atomically: the parent polls for file existence,
468 # so we cannot let it observe an empty file before close()
469 # has flushed the pid.
470 4 my $tmp = "$longpid_file.tmp";
471 4 ▶ open my $fh, '>', $tmp or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $tmp or die "open: $!" 0 4 4
472 4 print $fh "$$\n";
473 4 close $fh;
474 4 ▶ rename($tmp, $longpid_file) or die "rename: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $longpid_file or die "rename: $!" 0 4 4
475  
476 4 sleep 1 while 1;
477 0 return 0;
478 8 };
479 },
480  
481 handle_request => sub {
482 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=3 SCAR=12.0
483  
484 4 ▶ if ($req->{request} eq 'worker_count') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"request"} eq "worker_count") 4 0 4
485 4 ▶ my $workers = $self->workers // {};
Condition50%1/2
$self->workers // {}
l!l
40
MC/DC0%0/1
$self->workers // {}
$self->workers
Truth table50%1/2
A
A= $self->workers
Aresult
11
00
486 4 return scalar keys %$workers;
487 }
488  
489 0 return undef;
490 },
491 12 );
492  
493 4 my $service_pid = $handle->service_pid;
494 4 isnt($service_pid, $parent_pid, "Worker service runs in a different process");
495  
496 # Wait for the short worker to finish and write its marker
497 4 my $waited = 0;
498 4 ▶ until (-e $marker_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $marker_file or $waited > 5) 0 4 4
Condition33%1/3
-e $marker_file or $waited > 5
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $marker_file or $waited > 5
-e $marker_file $waited > 5
Truth table33%1/3
A or B
A= -e $marker_fileB= $waited > 5
ABresult
1X1
011
000
499 0 Time::HiRes::sleep(0.1);
500 0 $waited += 0.1;
501 }
502 4 ok(-e $marker_file, "Short worker ran and wrote marker file");
503  
504 # Wait for the long worker to write its pid
505 4 $waited = 0;
506 4 ▶ until (-e $longpid_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $longpid_file or $waited > 5) 0 4 4
Condition33%1/3
-e $longpid_file or $waited > 5
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $longpid_file or $waited > 5
-e $longpid_file $waited > 5
Truth table33%1/3
A or B
A= -e $longpid_fileB= $waited > 5
ABresult
1X1
011
000
507 0 Time::HiRes::sleep(0.1);
508 0 $waited += 0.1;
509 }
510 4 ok(-e $longpid_file, "Long worker wrote pid file");
511  
512 4 ▶ open(my $fh, '<', $longpid_file) or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $longpid_file or die "open: $!" 0 4 4
513 4 chomp(my $long_worker_pid = <$fh>);
514 4 close($fh);
515 4 ok($long_worker_pid, "Got long worker pid");
516  
517 # Verify the long worker is still running
518 4 ok(pid_is_running($long_worker_pid), "Long worker is running before shutdown");
519  
520 # Ask the service how many workers are registered (long_worker should
521 # still be there; short_worker may or may not have been reaped yet)
522 4 my $got_count = 0;
523 $handle->send_request(
524 worker_svc => 'worker_count',
525 sub {
526 4 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
527 4 $got_count = $resp->{response};
528 },
529 4 );
530 4 $handle->await_all_responses;
531 4 ok($got_count >= 1, "Service has at least 1 registered worker (got $got_count)");
532  
533 # Drop the handle — triggers shutdown which sends terminate, then the
534 # service's DESTROY calls terminate_workers to kill lingering workers.
535 4 $handle = undef;
536  
537 # Wait for the killed marker file to appear
538 4 my $tries = 0;
539 4 ▶ until (-e $killed_file || $tries >= 50) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $killed_file or $tries >= 50) 0 4 4
Condition33%1/3
-e $killed_file or $tries >= 50
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $killed_file or $tries >= 50
-e $killed_file $tries >= 50
Truth table33%1/3
A or B
A= -e $killed_fileB= $tries >= 50
ABresult
1X1
011
000
540 0 Time::HiRes::sleep(0.1);
541 0 $tries++;
542 }
543 4 ok(-e $killed_file, "Long worker received TERM signal during shutdown");
544 }
545  
546 sub test_suspend_and_reconnect {
547 4 ▶ my $proto = ipcm_default_protocol();
Subroutine test_suspend_and_reconnect: called CC=2 SCAR=6.9
Pod documented
548  
549 4 ▶ return skip_all "suspend/reconnect not supported by $proto"
Branch: 1 branch
BranchTrueFalseTotal
unless $proto->suspend_supported 2 2 4
550 unless $proto->suspend_supported;
551  
552 2 my $guard = ipcm_spawn(guard => 0);
553 2 my $info = "$guard";
554  
555 2 my $con1 = ipcm_connect('sr1' => $info);
556 2 my $con2 = ipcm_connect('sr2' => $info);
557  
558 # Send a message, then suspend con2
559 2 $con1->send_message(sr2 => {before => 'suspend'});
560 2 $con2->suspend;
561  
562 # con2 is now suspended — send another message while it's away
563 2 $con1->send_message(sr2 => {during => 'suspend'});
564  
565 # Reconnect
566 2 my $con2b = ipcm_reconnect('sr2' => $info);
567 2 ok($con2b, "Reconnected as sr2");
568  
569 # Should receive both messages
570 2 my @msgs = $con2b->get_messages;
571 2 is(scalar @msgs, 2, "Got both messages after reconnect");
572 2 my @contents = map { $_->content } @msgs;
573 2 like(\@contents, [{before => 'suspend'}, {during => 'suspend'}], "Messages preserved across suspend");
574  
575 2 $con1->disconnect;
576 2 $con2b->disconnect;
577 2 $guard->unspawn;
578 }
579  
580 sub test_sync_request {
581 my $handle = ipcm_service(
582 'sync_svc',
583 class => 'IPC::Manager::Service',
584 handle_request => sub {
585 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
586 4 return "sync_echo: $req->{request}";
587 },
588 8 ▶ );
Subroutine test_sync_request: called CC=1 SCAR=0.0
Pod documented
589  
590 4 my $resp = $handle->sync_request(sync_svc => "hello_sync");
591 4 is($resp->{response}, "sync_echo: hello_sync", "sync_request returned correct response");
592  
593 4 $handle = undef;
594 }
595  
596 sub test_sync_request_peer_exits_after_response {
597 # Regression: a service that responds and then exits used to race
598 # with the client's await_response loop. The response could already
599 # be in the parent's inbox when peer-active first reported the peer
600 # gone, and await_response would croak with "peer ... went away"
601 # instead of returning the response that had already been delivered.
602 #
603 # Reproduce the race deterministically: send the request via the
604 # async API, sleep long enough for the service to respond and fully
605 # tear down (so its registration is gone from the bus), then call
606 # await_response. The response is sitting in the parent's inbox;
607 # peer_active reports gone. Without the in-transit drain in
608 # await_response, this throws "peer ... went away".
609 24 ▶ my $iters = 5;
Subroutine test_sync_request_peer_exits_after_response: called CC=12 SCAR=30.4
Pod undocumented
610 24 my @errors;
611  
612 24 for my $i (1 .. $iters) {
613 80 my $done = 0;
614 80 my $name = "exit_after_resp_$i";
615  
616 my $handle = ipcm_service(
617 $name,
618 class => 'IPC::Manager::Service',
619 handle_request => sub {
620 20 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
621 20 $done = 1;
622 20 return {ok => 1, n => $req->{request}->{n}};
623 },
624 20 ▶ should_end => sub { $done },
Subroutine __ANON__: called CC=1 SCAR=0.0
625 80 );
626  
627 60 my $req_id = $handle->send_request($name, {n => $i});
628  
629 # Wait for the service process to die and the bus to deregister it.
630 # waitpid the child the spawn started, so peer_active() definitively
631 # reports gone by the time await_response runs. Prefer blocking
632 # waitpid for direct children; fall back to a short poll otherwise.
633 60 my $svc_pid = $handle->child_pid;
634 60 ▶ if (defined $svc_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $svc_pid) { } 60 0 60
635 60 my $r = waitpid($svc_pid, 0);
636 60 ▶ if ($r == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r == -1) 0 60 60
637 0 for (1 .. 50) {
638 0 ▶ last unless pid_is_running($svc_pid);
Branch: 1 branch
BranchTrueFalseTotal
unless pid_is_running($svc_pid) 0 0 0
639 0 Time::HiRes::sleep(0.05);
640 }
641 }
642 }
643 else {
644 0 Time::HiRes::sleep(1);
645 }
646  
647 60 my $resp;
648 60 my $ok = eval { $resp = $handle->await_response($req_id); 1 };
649 60 my $err = $@;
650 60 ▶ if (!$ok) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $ok) { } 0 60 60
elsif (not ref $resp or ($resp->{'response'}{'n'} // -1) != $i) { } 0 60 60
651 0 push @errors => "iter $i: $err";
652 }
653 ▶ elsif (!ref($resp) || ($resp->{response}{n} // -1) != $i) {
Condition40%2/5
not ref $resp or ($resp->{"response"}{"n"} // -1) != $i
l!l&&r!l&&!r
0060
$resp->{"response"}{"n"} // -1
l!l
600
MC/DC0%0/2
not ref $resp or ($resp->{"response"}{"n"} // -1) != $i
not ref $resp $resp->{"response"}{"n"}
Truth table33%1/3
not(A) or B
A= not ref $respB= $resp->{"response"}{"n"}
ABresult
1X1
011
000
654 0 ▶ push @errors => "iter $i: bad response: " . (ref($resp) ? $resp->{response}{n} // 'undef' : $resp);
Branch: 1 branch
BranchTrueFalseTotal
ref $resp ? : 0 0 0
Condition0%0/2
$resp->{"response"}{"n"} // "undef"
l!l
00
MC/DC0%0/1
$resp->{"response"}{"n"} // "undef"
$resp->{"response"}{"n"}
Truth table0%0/2
A
A= $resp->{"response"}{"n"}
Aresult
11
00
655 }
656  
657 60 $handle = undef;
658 }
659  
660 4 is(\@errors, [], "no peer-gone exceptions across $iters exits-after-response iterations");
661 }
662  
663 sub test_multiple_requests {
664 my $handle = ipcm_service(
665 'multi_svc',
666 class => 'IPC::Manager::Service',
667 handle_request => sub {
668 20 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
669 20 return "reply_$req->{request}";
670 },
671 8 ▶ );
Subroutine test_multiple_requests: called CC=2 SCAR=6.9
Pod documented
672  
673 4 my @got;
674 4 for my $i (1 .. 5) {
675 $handle->send_request(
676 multi_svc => "req_$i",
677 sub {
678 20 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
679 20 push @got, $resp->{response};
680 },
681 20 );
682 }
683  
684 4 $handle->await_all_responses;
685  
686 4 is(scalar @got, 5, "Got all 5 responses");
687 4 is([sort @got], [sort map { "reply_req_$_" } 1..5], "All responses correct");
688  
689 4 $handle = undef;
690 }
691  
692 sub test_handle_messages_buffer {
693 my $handle = ipcm_service(
694 'buf_svc',
695 class => 'IPC::Manager::Service',
696 handle_request => sub {
697 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
698  
699 # Send a non-response message back alongside the response
700 4 $self->client->send_message(
701 $msg->from,
702 {notification => "event_$req->{request}"},
703 );
704  
705 4 return "ack";
706 },
707 8 ▶ );
Subroutine test_handle_messages_buffer: called CC=1 SCAR=0.0
Pod documented
708  
709 4 my $resp = $handle->sync_request(buf_svc => "ping");
710 4 is($resp->{response}, "ack", "Got response");
711  
712 # The notification should be in the handle's message buffer
713 4 my @buffered = $handle->messages;
714 4 is(scalar @buffered, 1, "One buffered non-response message");
715 4 is($buffered[0]->content->{notification}, "event_ping", "Notification content correct");
716  
717 # Buffer is drained
718 4 my @again = $handle->messages;
719 4 is(scalar @again, 0, "Buffer empty after drain");
720  
721 4 $handle = undef;
722 }
723  
724 sub test_service_callbacks {
725 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_service_callbacks: called CC=8 SCAR=21.0
Pod documented
726 8 my $interval_file = File::Spec->catfile($marker_dir, 'interval_ran');
727 8 my $peer_file = File::Spec->catfile($marker_dir, 'peer_delta');
728 8 my $cleanup_file = File::Spec->catfile($marker_dir, 'cleanup_ran');
729 8 my $should_end_req = File::Spec->catfile($marker_dir, 'should_end');
730  
731 my $handle = ipcm_service(
732 'cb_svc',
733 class => 'IPC::Manager::Service',
734 interval => 0.1,
735  
736 on_interval => sub {
737 11 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
738 11 ▶ unless (-e $interval_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $interval_file) 4 7 11
739 4 ▶ open my $fh, '>', $interval_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $interval_file or die "open: $!" 0 4 4
740 4 print $fh "$$\n";
741 4 close $fh;
742 }
743 },
744  
745 on_peer_delta => sub {
746 7 ▶ my ($self, $delta) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
747 7 ▶ open my $fh, '>>', $peer_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $peer_file or die "open: $!" 0 7 7
748 7 for my $peer (sort keys %$delta) {
749 7 print $fh "$peer:$delta->{$peer}\n";
750 }
751 7 close $fh;
752 },
753  
754 on_cleanup => sub {
755 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
756 4 ▶ open my $fh, '>', $cleanup_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cleanup_file or die "open: $!" 0 4 4
757 4 print $fh "$$\n";
758 4 close $fh;
759 },
760  
761 should_end => sub {
762 12 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
763 12 ▶ return -e $should_end_req ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
-e $should_end_req ? : 4 8 12
764 },
765  
766 handle_request => sub {
767 0 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
768 0 return "ok";
769 },
770 8 );
771  
772 # Wait for on_interval to fire
773 4 my $waited = 0;
774 4 ▶ until (-e $interval_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $interval_file or $waited > 5) 9 4 13
Condition66%2/3
-e $interval_file or $waited > 5
l!l&&r!l&&!r
409
MC/DC50%1/2
-e $interval_file or $waited > 5
-e $interval_file $waited > 5
Truth table66%2/3
A or B
A= -e $interval_fileB= $waited > 5
ABresult
1X1
011
000
775 9 Time::HiRes::sleep(0.1);
776 9 $waited += 0.1;
777 }
778 4 ok(-e $interval_file, "on_interval callback fired");
779  
780 # Trigger peer_delta by connecting a new client to the same bus.
781 # The service polls at its interval (0.1s above), but slow CI machines
782 # can take much longer to schedule the service's poll, so wait for the
783 # callback to actually fire instead of relying on a fixed sleep.
784 4 my $extra = ipcm_connect('extra_peer' => $handle->ipcm_info);
785 4 $waited = 0;
786 4 ▶ until (-e $peer_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $peer_file or $waited > 10) 6 4 10
Condition66%2/3
-e $peer_file or $waited > 10
l!l&&r!l&&!r
406
MC/DC50%1/2
-e $peer_file or $waited > 10
-e $peer_file $waited > 10
Truth table66%2/3
A or B
A= -e $peer_fileB= $waited > 10
ABresult
1X1
011
000
787 6 Time::HiRes::sleep(0.1);
788 6 $waited += 0.1;
789 }
790 4 $extra->disconnect;
791 4 ok(-e $peer_file, "on_peer_delta callback fired");
792  
793 # Trigger should_end
794 4 ▶ open my $fh, '>', $should_end_req or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $should_end_req or die "open: $!" 0 4 4
795 4 close $fh;
796  
797 # Wait for cleanup file (service should end and run cleanup)
798 4 $waited = 0;
799 4 ▶ until (-e $cleanup_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cleanup_file or $waited > 10) 7 4 11
Condition66%2/3
-e $cleanup_file or $waited > 10
l!l&&r!l&&!r
407
MC/DC50%1/2
-e $cleanup_file or $waited > 10
-e $cleanup_file $waited > 10
Truth table66%2/3
A or B
A= -e $cleanup_fileB= $waited > 10
ABresult
1X1
011
000
800 7 Time::HiRes::sleep(0.1);
801 7 $waited += 0.1;
802 }
803 4 ok(-e $cleanup_file, "on_cleanup callback fired after should_end");
804  
805 4 $handle = undef;
806 }
807  
808 sub test_service_on_pid {
809 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_service_on_pid: called CC=6 SCAR=18.2
Pod documented
810 8 my $pid_file = File::Spec->catfile($marker_dir, 'reaped_pid');
811  
812 my $handle = ipcm_service(
813 'pid_svc',
814 class => 'IPC::Manager::Service',
815 on_pid => sub {
816 4 ▶ my ($self, $pid, $exit) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
817 4 ▶ open my $fh, '>>', $pid_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>", $pid_file or die "open: $!" 0 4 4
818 4 print $fh "$pid $exit\n";
819 4 close $fh;
820 },
821 handle_request => sub {
822 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=4 SCAR=15.9
823  
824 4 ▶ if ($req->{request} eq 'fork_child') {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"request"} eq "fork_child") 4 0 4
825 4 ▶ my $kid = fork // die "fork: $!";
Condition50%1/2
fork // die("fork: $!")
l!l
40
MC/DC0%0/1
fork // die("fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
826 4 ▶ if (!$kid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kid) 0 4 4
827 0 POSIX::_exit(5);
828 }
829 4 return $kid;
830 }
831  
832 0 return undef;
833 },
834 8 );
835  
836 4 my $got_pid;
837 $handle->send_request(
838 pid_svc => 'fork_child',
839 sub {
840 4 ▶ my ($resp, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
841 4 $got_pid = $resp->{response};
842 },
843 4 );
844 4 $handle->await_all_responses;
845 4 ok($got_pid, "Got forked non-worker child pid: $got_pid");
846  
847 4 my $waited = 0;
848 4 ▶ until (-s $pid_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $pid_file or $waited > 5) 4 4 8
Condition66%2/3
-s $pid_file or $waited > 5
l!l&&r!l&&!r
404
MC/DC50%1/2
-s $pid_file or $waited > 5
-s $pid_file $waited > 5
Truth table66%2/3
A or B
A= -s $pid_fileB= $waited > 5
ABresult
1X1
011
000
849 4 Time::HiRes::sleep(0.1);
850 4 $waited += 0.1;
851 }
852 4 ok(-s $pid_file, "on_pid handler fired after non-worker child exit");
853  
854 4 ▶ open my $fh, '<', $pid_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pid_file or die "open: $!" 0 4 4
855 4 my @lines = <$fh>;
856 4 close $fh;
857  
858 4 my ($reaped_pid, $reaped_exit);
859 4 for my $line (@lines) {
860 4 chomp $line;
861 4 my ($p, $e) = split / /, $line;
862 4 ▶ if ($p == $got_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == $got_pid) 4 0 4
863 4 $reaped_pid = $p;
864 4 $reaped_exit = $e;
865 4 last;
866 }
867 }
868  
869 4 is($reaped_pid, $got_pid, "on_pid got the forked child's pid");
870 4 is($reaped_exit >> 8, 5, "on_pid got exit value 5");
871  
872 4 $handle = undef;
873 }
874  
875 sub test_service_signal_handling {
876 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_service_signal_handling: called CC=3 SCAR=11.1
Pod documented
877 8 my $sig_file = File::Spec->catfile($marker_dir, 'got_signal');
878  
879 my $handle = ipcm_service(
880 'sig_svc',
881 class => 'IPC::Manager::Service',
882 on_sig => {
883 USR1 => sub {
884 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
885 4 ▶ open my $fh, '>', $sig_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $sig_file or die "open: $!" 0 4 4
886 4 print $fh "$$\n";
887 4 close $fh;
888 },
889 },
890 handle_request => sub {
891 0 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
892 0 return "ok";
893 },
894 8 );
895  
896 4 my $svc_pid = $handle->service_pid;
897 4 ok($svc_pid, "Got service pid");
898  
899 # Send USR1 to the service
900 4 kill('USR1', $svc_pid);
901  
902 4 my $waited = 0;
903 4 ▶ until (-e $sig_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $sig_file or $waited > 5) 4 4 8
Condition66%2/3
-e $sig_file or $waited > 5
l!l&&r!l&&!r
404
MC/DC50%1/2
-e $sig_file or $waited > 5
-e $sig_file $waited > 5
Truth table66%2/3
A or B
A= -e $sig_fileB= $waited > 5
ABresult
1X1
011
000
904 4 Time::HiRes::sleep(0.1);
905 4 $waited += 0.1;
906 }
907 4 ok(-e $sig_file, "Signal handler ran after USR1");
908  
909 4 $handle = undef;
910 }
911  
912 sub test_peer_active {
913 4 ▶ my $guard = ipcm_spawn();
Subroutine test_peer_active: called CC=1 SCAR=0.0
Pod documented
914 4 my $info = "$guard";
915  
916 4 my $con1 = ipcm_connect('pa1' => $info);
917 4 my $con2 = ipcm_connect('pa2' => $info);
918  
919 4 ok($con1->peer_active('pa2'), "pa2 is active");
920 4 ok($con2->peer_active('pa1'), "pa1 is active");
921  
922 4 ok(!$con1->peer_active('nonexistent'), "nonexistent peer is not active");
923  
924 4 $con1->disconnect;
925 4 $con2->disconnect;
926 4 $guard = undef;
927 }
928  
929 sub test_disconnect_with_handler {
930 4 ▶ my $guard = ipcm_spawn();
Subroutine test_disconnect_with_handler: called CC=1 SCAR=0.0
Pod documented
931 4 my $info = "$guard";
932  
933 4 my $con1 = ipcm_connect('dh1' => $info);
934 4 my $con2 = ipcm_connect('dh2' => $info);
935  
936 # Send messages that will be waiting when con2 disconnects
937 4 $con1->send_message(dh2 => {msg => 'waiting_1'});
938 4 $con1->send_message(dh2 => {msg => 'waiting_2'});
939  
940 # Disconnect with a handler callback — messages should be passed to it
941 # instead of producing a warning.
942 4 my @handled;
943 $con2->disconnect(sub {
944 4 ▶ my ($self, $msgs) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
945 4 push @handled, @$msgs;
946 4 });
947  
948 4 is(scalar @handled, 2, "Handler received 2 pending messages");
949 4 my @contents = map { $_->content->{msg} } IPC::Manager::Message::sort_messages(@handled);
950 4 is(\@contents, ['waiting_1', 'waiting_2'], "Handler got correct message contents");
951  
952 4 $con1->disconnect;
953 4 $guard = undef;
954 }
955  
956 sub test_try_message {
957 4 ▶ my $guard = ipcm_spawn();
Subroutine test_try_message: called CC=1 SCAR=0.0
Pod documented
958 4 my $info = "$guard";
959  
960 4 my $con1 = ipcm_connect('tm1' => $info);
961 4 my $con2 = ipcm_connect('tm2' => $info);
962  
963 # Success path — scalar context
964 4 my $ok = $con1->try_message(tm2 => {data => 'hello'});
965 4 ok($ok, "try_message returns true on success (scalar)");
966  
967 # Success path — list context
968 4 my ($ok2, $err) = $con1->try_message(tm2 => {data => 'world'});
969 4 ok($ok2, "try_message returns true on success (list)");
970 4 ok(!$err, "no error on success (list)");
971  
972 # Verify messages arrived
973 4 my @msgs = $con2->get_messages;
974 4 is(scalar @msgs, 2, "Both try_message calls delivered");
975  
976 # Failure path — peer doesn't exist
977 4 my ($ok3, $err3) = $con1->try_message(nonexistent => 'nope');
978 4 ok(!$ok3, "try_message returns false for missing peer");
979 4 ok($err3, "error returned for missing peer");
980  
981 4 $con1->disconnect;
982 4 $con2->disconnect;
983 4 $guard = undef;
984 }
985  
986 sub test_general_messages_to_service {
987 # Sends a plain message (not a request or response) to a service and
988 # verifies run_on_general_message is invoked.
989 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_general_messages_to_service: called CC=5 SCAR=16.5
Pod documented
990 8 my $general_file = File::Spec->catfile($marker_dir, 'general_msg');
991  
992 my $handle = ipcm_service(
993 'gen_svc',
994 class => 'IPC::Manager::Service',
995  
996 on_general_message => sub {
997 8 ▶ my ($self, $msg) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.6
998 8 my $c = $msg->content;
999 8 ▶ return unless ref($c) eq 'HASH' && defined $c->{text};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" and defined $c->{"text"} 4 4 8
Condition66%2/3
ref $c eq "HASH" and defined $c->{"text"}
!ll&&!rl&&r
044
MC/DC50%1/2
ref $c eq "HASH" and defined $c->{"text"}
ref $c eq "HASH" defined $c->{"text"}
Truth table66%2/3
A and B
A= ref $c eq "HASH"B= defined $c->{"text"}
ABresult
0X0
100
111
1000 4 ▶ open my $fh, '>', "$general_file.tmp" or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$general_file.tmp" or die "open: $!" 0 4 4
1001 4 print $fh $c->{text}, "\n";
1002 4 ▶ close $fh or die "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close: $!" 0 4 4
1003 4 ▶ rename "$general_file.tmp", $general_file or die "rename: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename "$general_file.tmp", $general_file or die "rename: $!" 0 4 4
1004 },
1005  
1006 handle_request => sub {
1007 0 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1008 0 return "ok";
1009 },
1010 8 );
1011  
1012 # Send a plain (non-request) message directly via the handle's client
1013 4 $handle->client->send_message(gen_svc => {text => 'plain_hello'});
1014  
1015 4 my $waited = 0;
1016 4 ▶ until (-s $general_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $general_file or $waited > 5) 6 4 10
Condition66%2/3
-s $general_file or $waited > 5
l!l&&r!l&&!r
406
MC/DC50%1/2
-s $general_file or $waited > 5
-s $general_file $waited > 5
Truth table66%2/3
A or B
A= -s $general_fileB= $waited > 5
ABresult
1X1
011
000
1017 6 Time::HiRes::sleep(0.1);
1018 6 $waited += 0.1;
1019 }
1020 4 ok(-s $general_file, "on_general_message callback fired");
1021  
1022 4 ▶ if (-s $general_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $general_file) 4 0 4
1023 4 ▶ open my $fh, '<', $general_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $general_file or die "open: $!" 0 4 4
1024 4 chomp(my $line = <$fh>);
1025 4 close $fh;
1026 4 is($line, 'plain_hello', "General message content correct");
1027 }
1028  
1029 4 $handle = undef;
1030 }
1031  
1032 sub test_intercept_errors {
1033 # Verifies that a service with intercept_errors => 1 catches exceptions
1034 # in multiple callback types instead of dying, and continues processing.
1035 # Also verifies that a crashing handle_request sends an error response.
1036  
1037 8 ▶ local *STDERR = *STDOUT;
Subroutine test_intercept_errors: called CC=3 SCAR=11.0
Pod documented
1038  
1039 8 my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
1040 8 my $interval_survived = File::Spec->catfile($marker_dir, 'interval_ok');
1041 8 my $general_survived = File::Spec->catfile($marker_dir, 'general_ok');
1042 8 my $peer_delta_survived = File::Spec->catfile($marker_dir, 'peer_delta_ok');
1043  
1044 8 my $interval_count = 0;
1045 my $handle = ipcm_service(
1046 'ie_svc',
1047 class => 'IPC::Manager::Service',
1048 intercept_errors => 1,
1049 expose_error_details => 1,
1050 interval => 0.1,
1051  
1052 on_interval => sub {
1053 18 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
1054 18 $interval_count++;
1055 # Throw on the first call; subsequent calls write the marker
1056 18 ▶ if ($interval_count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interval_count == 1) 4 14 18
1057 4 die "interval boom\n";
1058 }
1059 14 ▶ unless (-e $interval_survived) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $interval_survived) 4 10 14
1060 4 ▶ open my $fh, '>', $interval_survived or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $interval_survived or die "open: $!" 0 4 4
1061 4 close $fh;
1062 }
1063 },
1064  
1065 on_general_message => sub {
1066 8 ▶ my ($self, $msg) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.0
1067 8 my $c = $msg->content;
1068 8 ▶ return unless ref($c) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "HASH" 0 8 8
1069 8 ▶ if ($c->{action} && $c->{action} eq 'crash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"action"} and $c->{"action"} eq "crash") 4 4 8
Condition66%2/3
$c->{"action"} and $c->{"action"} eq "crash"
!ll&&!rl&&r
404
MC/DC50%1/2
$c->{"action"} and $c->{"action"} eq "crash"
$c->{"action"} $c->{"action"} eq "crash"
Truth table66%2/3
A and B
A= $c->{"action"}B= $c->{"action"} eq "crash"
ABresult
0X0
100
111
1070 4 die "general_message boom\n";
1071 }
1072 },
1073  
1074 on_peer_delta => sub {
1075 4 ▶ my ($self, $delta) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1076 # Always throw — service should survive
1077 4 die "peer_delta boom\n";
1078 },
1079  
1080 handle_request => sub {
1081 16 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
1082 16 ▶ die "request boom\n" if $req->{request} eq 'crash';
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"request"} eq "crash" 4 12 16
1083 12 return "ok";
1084 },
1085 8 );
1086  
1087 # 1) on_interval: first call throws, second should still fire
1088 4 my $waited = 0;
1089 4 ▶ until (-e $interval_survived || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $interval_survived or $waited > 5) 16 4 20
Condition66%2/3
-e $interval_survived or $waited > 5
l!l&&r!l&&!r
4016
MC/DC50%1/2
-e $interval_survived or $waited > 5
-e $interval_survived $waited > 5
Truth table66%2/3
A or B
A= -e $interval_survivedB= $waited > 5
ABresult
1X1
011
000
1090 16 Time::HiRes::sleep(0.1);
1091 16 $waited += 0.1;
1092 }
1093 4 ok(-e $interval_survived, "on_interval survived exception and kept firing");
1094  
1095 # 2) on_peer_delta: connect a peer — triggers an exception, but service
1096 # should keep running. The follow-up sync_request below is FIFO-
1097 # serialised behind the peer-delta dispatch, so by the time the
1098 # response arrives the peer_delta callback has already fired (and
1099 # been intercepted).
1100 4 my $extra = ipcm_connect('ie_extra' => $handle->ipcm_info);
1101 4 $extra->disconnect;
1102  
1103 4 my $resp = $handle->sync_request(ie_svc => 'ping');
1104 4 is($resp->{response}, 'ok', "Service survived on_peer_delta exception");
1105  
1106 # 3) on_general_message: send a plain message that throws. Same FIFO
1107 # ordering argument: by the time the next sync_request returns, the
1108 # general_message callback has already fired (and been intercepted).
1109 4 $handle->client->send_message(ie_svc => {action => 'crash'});
1110  
1111 4 my $resp2 = $handle->sync_request(ie_svc => 'ping');
1112 4 is($resp2->{response}, 'ok', "Service survived on_general_message exception");
1113  
1114 # 4) handle_request: send a request that throws — should get an error response
1115 4 my $err_resp = $handle->sync_request(ie_svc => 'crash');
1116 4 ok($err_resp->{ipcm_error}, "Got error response from crashing request");
1117 4 like($err_resp->{ipcm_error}, qr/request boom/, "Error response contains exception details");
1118 4 ok(!defined $err_resp->{response}, "Response is undef on error");
1119  
1120 # 5) Service still works after all the exceptions
1121 4 my $resp3 = $handle->sync_request(ie_svc => 'ping');
1122 4 is($resp3->{response}, 'ok', "Service still alive after all intercepted errors");
1123  
1124 4 $handle = undef;
1125 }
1126  
1127 sub test_watch_pids {
1128 # Verifies that a service terminates when a watched PID exits.
1129 # Uses watch_pids => [$$] by watching the parent. The parent forks a
1130 # temporary child to own the IPC bus while the service runs; the parent
1131 # then exits that scope so the service sees the pid die.
1132  
1133 12 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_watch_pids: called CC=10 SCAR=23.8
Pod documented
1134 12 my $cleanup_file = File::Spec->catfile($marker_dir, 'wp_cleanup');
1135 12 my $ready_file = File::Spec->catfile($marker_dir, 'wp_ready');
1136  
1137 # Fork a child that acts as the watched process. It starts the service
1138 # with watch_pids pointing at itself, verifies the service works, then
1139 # exits — causing the service to detect the pid death.
1140 12 ▶ my $child = fork // die "fork: $!";
Condition50%1/2
fork // die("fork: $!")
l!l
120
MC/DC0%0/1
fork // die("fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
1141 12 ▶ if ($child == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child == 0) 8 4 12
1142 my $handle = ipcm_service(
1143 'wp_svc',
1144 class => 'IPC::Manager::Service',
1145 watch_pids => [$$],
1146  
1147 on_cleanup => sub {
1148 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
1149 4 ▶ open my $fh, '>', $cleanup_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $cleanup_file or die "open: $!" 0 4 4
1150 4 print $fh "$$\n";
1151 4 close $fh;
1152 },
1153  
1154 handle_request => sub {
1155 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1156 4 return "ok";
1157 },
1158 8 );
1159  
1160 4 my $resp = $handle->sync_request(wp_svc => 'ping');
1161 # Signal parent we are ready
1162 4 ▶ open my $fh, '>', $ready_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $ready_file or die "open: $!" 0 4 4
1163 4 print $fh "$resp->{response}\n";
1164 4 close $fh;
1165  
1166 # Exit — the service watches our pid and should terminate
1167 4 exit(0);
1168 }
1169  
1170 # Wait for child to signal readiness
1171 4 my $waited = 0;
1172 4 ▶ until (-e $ready_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $ready_file or $waited > 10) 13 4 17
Condition66%2/3
-e $ready_file or $waited > 10
l!l&&r!l&&!r
4013
MC/DC50%1/2
-e $ready_file or $waited > 10
-e $ready_file $waited > 10
Truth table66%2/3
A or B
A= -e $ready_fileB= $waited > 10
ABresult
1X1
011
000
1173 13 Time::HiRes::sleep(0.1);
1174 13 $waited += 0.1;
1175 }
1176 4 ok(-e $ready_file, "Child started service and sent request");
1177  
1178 4 ▶ if (-e $ready_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $ready_file) 4 0 4
1179 4 ▶ open my $fh, '<', $ready_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $ready_file or die "open: $!" 0 4 4
1180 4 chomp(my $resp = <$fh>);
1181 4 close $fh;
1182 4 is($resp, 'ok', "Service responded before watched pid died");
1183 }
1184  
1185 # Reap the child (whose exit triggers the service's watch_pids)
1186 4 waitpid($child, 0);
1187  
1188 # Wait for service to notice and run cleanup
1189 4 $waited = 0;
1190 4 ▶ until (-e $cleanup_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $cleanup_file or $waited > 10) 0 4 4
Condition33%1/3
-e $cleanup_file or $waited > 10
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $cleanup_file or $waited > 10
-e $cleanup_file $waited > 10
Truth table33%1/3
A or B
A= -e $cleanup_fileB= $waited > 10
ABresult
1X1
011
000
1191 0 Time::HiRes::sleep(0.1);
1192 0 $waited += 0.1;
1193 }
1194 4 ok(-e $cleanup_file, "Service exited after watched pid terminated");
1195 }
1196  
1197 sub test_spawn_terminate_with_signal {
1198 # Tests that Spawn::terminate sends the configured signal to service
1199 # processes. The signal is sent before the terminate broadcast so the
1200 # service's on_sig handler has a chance to run before the event loop ends.
1201 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_spawn_terminate_with_signal: called CC=3 SCAR=11.4
Pod documented
1202 8 my $sig_file = File::Spec->catfile($marker_dir, 'got_usr1');
1203  
1204 my $handle = ipcm_service(
1205 'sigterm_svc',
1206 class => 'IPC::Manager::Service',
1207 on_sig => {
1208 USR1 => sub {
1209 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
1210 4 ▶ open my $fh, '>', $sig_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $sig_file or die "open: $!" 0 4 4
1211 4 print $fh "$$\n";
1212 4 close $fh;
1213 },
1214 },
1215 handle_request => sub {
1216 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1217 4 return "ok";
1218 },
1219 8 );
1220  
1221 # Verify service is running
1222 4 my $resp = $handle->sync_request(sigterm_svc => 'ping');
1223 4 is($resp->{response}, 'ok', "Service running before signal test");
1224  
1225 # Set the signal on the spawn so shutdown sends USR1 to all peers
1226 # before the terminate broadcast.
1227 4 $handle->{spawn}->{signal} = 'USR1';
1228  
1229 # Drop the handle — triggers shutdown which sends USR1 then broadcasts terminate
1230 4 $handle = undef;
1231  
1232 4 my $waited = 0;
1233 4 ▶ until (-e $sig_file || $waited > 5) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $sig_file or $waited > 5) 0 4 4
Condition33%1/3
-e $sig_file or $waited > 5
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $sig_file or $waited > 5
-e $sig_file $waited > 5
Truth table33%1/3
A or B
A= -e $sig_fileB= $waited > 5
ABresult
1X1
011
000
1234 0 Time::HiRes::sleep(0.1);
1235 0 $waited += 0.1;
1236 }
1237 4 ok(-e $sig_file, "Service received signal from Spawn::terminate");
1238 }
1239  
1240 sub test_cleave {
1241 4 ▶ my $guard = ipcm_spawn(guard => 0);
Subroutine test_cleave: called CC=2 SCAR=7.0
Pod documented
1242 4 my $info = "$guard";
1243  
1244 4 my $original_pid = $guard->pid;
1245 4 is($original_pid, $$, "Spawn pid is ours before cleave");
1246  
1247 4 my $cleave_result = $guard->cleave;
1248  
1249 4 ▶ if ($cleave_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cleave_result) { } 4 0 4
1250 # Parent — cleave_result is the new owner PID
1251 4 ok($cleave_result != $$, "Cleave returned new PID to parent");
1252 4 isnt($guard->pid, $$, "Spawn ownership transferred");
1253  
1254 # Connect, send a message, verify the bus still works
1255 4 my $con = ipcm_connect('cleave_test' => $info);
1256 4 ok($con, "Can connect to bus after cleave");
1257 4 $con->disconnect;
1258  
1259 # Kill the cleaved process and clean up
1260 4 kill('TERM', $cleave_result);
1261 4 waitpid($cleave_result, 0);
1262  
1263 4 $guard->unspawn;
1264 }
1265 else {
1266 # Child (new owner) — just exit. Don't unspawn; the parent
1267 # still needs the route to verify the bus works.
1268 0 exit(0);
1269 }
1270 }
1271  
1272 sub test_request_error_generic {
1273 my $handle = ipcm_service(
1274 'err_generic_svc',
1275 class => 'IPC::Manager::Service',
1276 handle_request => sub {
1277 12 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
1278 12 ▶ die "secret internal failure\n" if $req->{request} eq 'crash';
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"request"} eq "crash" 4 8 12
1279 8 return "ok";
1280 },
1281 8 ▶ );
Subroutine test_request_error_generic: called CC=1 SCAR=0.0
Pod documented
1282  
1283 # Normal request works
1284 4 my $resp = $handle->sync_request(err_generic_svc => 'hello');
1285 4 is($resp->{response}, 'ok', "Normal request succeeds");
1286 4 ok(!$resp->{ipcm_error}, "No error on normal request");
1287  
1288 # Crashing request returns a generic error (default: expose_error_details off)
1289 4 my $err_resp = $handle->sync_request(err_generic_svc => 'crash');
1290 4 ok($err_resp->{ipcm_error}, "Error response has ipcm_error flag");
1291 4 is($err_resp->{ipcm_error}, 'Internal service error', "Error message is generic");
1292 4 unlike($err_resp->{ipcm_error}, qr/secret/, "Internal details not exposed");
1293 4 ok(!defined $err_resp->{response}, "Response is undef on error");
1294  
1295 # Service is still alive after the error
1296 4 my $resp2 = $handle->sync_request(err_generic_svc => 'hello');
1297 4 is($resp2->{response}, 'ok', "Service still works after error");
1298  
1299 4 $handle = undef;
1300 }
1301  
1302 sub test_request_error_detailed {
1303 my $handle = ipcm_service(
1304 'err_detail_svc',
1305 class => 'IPC::Manager::Service',
1306 expose_error_details => 1,
1307 handle_request => sub {
1308 8 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
1309 8 ▶ die "detailed failure info\n" if $req->{request} eq 'crash';
Branch: 1 branch
BranchTrueFalseTotal
if $req->{"request"} eq "crash" 4 4 8
1310 4 return "ok";
1311 },
1312 8 ▶ );
Subroutine test_request_error_detailed: called CC=1 SCAR=0.0
Pod documented
1313  
1314 # Crashing request returns the actual exception text
1315 4 my $err_resp = $handle->sync_request(err_detail_svc => 'crash');
1316 4 ok($err_resp->{ipcm_error}, "Error response has ipcm_error flag");
1317 4 like($err_resp->{ipcm_error}, qr/detailed failure info/, "Error exposes exception details");
1318 4 ok(!defined $err_resp->{response}, "Response is undef on error");
1319  
1320 # Service is still alive
1321 4 my $resp = $handle->sync_request(err_detail_svc => 'hello');
1322 4 is($resp->{response}, 'ok', "Service still works after detailed error");
1323  
1324 4 $handle = undef;
1325 }
1326  
1327 sub test_post_fork_hook {
1328 # Verifies that the post_fork_hook allows a double-fork pattern where the
1329 # middle process goes off to do its own work and the grandchild becomes
1330 # the service.
1331  
1332 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_post_fork_hook: called CC=8 SCAR=23.0
Pod documented
1333 8 my $middle_file = File::Spec->catfile($marker_dir, 'middle_pid');
1334 8 my $service_file = File::Spec->catfile($marker_dir, 'service_pid');
1335  
1336 my $handle = ipcm_service(
1337 'pfh_svc',
1338 class => 'IPC::Manager::Service',
1339  
1340 post_fork => sub {
1341 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=20.4
1342  
1343 4 ▶ my $pid = fork // die "Could not fork in post_fork: $!";
Condition50%1/2
fork // die("Could not fork in post_fork: $!")
l!l
40
MC/DC0%0/1
fork // die("Could not fork in post_fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
1344  
1345 4 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 4 4
1346 # Middle process: write our PID and exit
1347 0 ▶ open my $fh, '>', $middle_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $middle_file or die "open: $!" 0 0 0
1348 0 print $fh "$$\n";
1349 0 close $fh;
1350 0 POSIX::_exit(0);
1351 }
1352  
1353 # Grandchild: return to become the service
1354 },
1355  
1356 on_start => sub {
1357 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
1358 4 ▶ open my $fh, '>', $service_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $service_file or die "open: $!" 0 4 4
1359 4 print $fh "$$\n";
1360 4 close $fh;
1361 },
1362  
1363 handle_request => sub {
1364 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1365 4 return "pfh_$$";
1366 },
1367 8 );
1368  
1369 # Wait for the middle process marker
1370 4 my $waited = 0;
1371 4 ▶ until (-e $middle_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $middle_file or $waited > 10) 0 4 4
Condition33%1/3
-e $middle_file or $waited > 10
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $middle_file or $waited > 10
-e $middle_file $waited > 10
Truth table33%1/3
A or B
A= -e $middle_fileB= $waited > 10
ABresult
1X1
011
000
1372 0 Time::HiRes::sleep(0.1);
1373 0 $waited += 0.1;
1374 }
1375 4 ok(-e $middle_file, "Middle process ran and wrote marker");
1376  
1377 # The service should be functional
1378 4 my $resp = $handle->sync_request(pfh_svc => 'ping');
1379 4 ok($resp->{response}, "Service responded");
1380  
1381 # Wait for service on_start marker
1382 4 $waited = 0;
1383 4 ▶ until (-e $service_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $service_file or $waited > 10) 0 4 4
Condition33%1/3
-e $service_file or $waited > 10
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $service_file or $waited > 10
-e $service_file $waited > 10
Truth table33%1/3
A or B
A= -e $service_fileB= $waited > 10
ABresult
1X1
011
000
1384 0 Time::HiRes::sleep(0.1);
1385 0 $waited += 0.1;
1386 }
1387 4 ok(-e $service_file, "Service on_start ran");
1388  
1389 # Read the PIDs
1390 4 ▶ open my $mfh, '<', $middle_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $mfh, "<", $middle_file or die "open: $!" 0 4 4
1391 4 chomp(my $middle_pid = <$mfh>);
1392 4 close $mfh;
1393  
1394 4 ▶ open my $sfh, '<', $service_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $sfh, "<", $service_file or die "open: $!" 0 4 4
1395 4 chomp(my $svc_pid = <$sfh>);
1396 4 close $sfh;
1397  
1398 4 my $handle_svc_pid = $handle->service_pid;
1399  
1400 # The service PID should be the grandchild, not the middle process
1401 4 isnt($svc_pid, $middle_pid, "Service PID ($svc_pid) differs from middle process PID ($middle_pid)");
1402 4 is($handle_svc_pid, $svc_pid, "Handle reports correct service PID");
1403  
1404 # Verify the response came from the grandchild
1405 4 like($resp->{response}, qr/^pfh_\d+$/, "Response includes a PID");
1406 4 is($resp->{response}, "pfh_$svc_pid", "Response came from the grandchild service process");
1407  
1408 # Reap the middle process so it doesn't linger as a zombie
1409 4 ▶ waitpid($middle_pid, 0) if $middle_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $middle_pid 4 0 4
1410  
1411 # child_pid on the handle is the first-fork pid from ipcm_service.
1412 # In this daemonize case that is the middle process, which has now
1413 # exited - so it is no longer alive.
1414 4 my $cpid = $handle->child_pid;
1415 4 ok($cpid, "Handle carries a child_pid");
1416 4 is($cpid, $middle_pid, "child_pid == first-fork (middle) pid");
1417 4 ok(!kill(0, $cpid), "First-fork pid no longer alive after daemonize");
1418  
1419 4 $handle = undef;
1420 }
1421  
1422 sub test_child_pid {
1423 # Basic accessor: handle returned by ipcm_service carries the
1424 # first-fork pid, and it is alive while the service is up.
1425  
1426 my $handle = ipcm_service(
1427 'child_pid_svc',
1428 class => 'IPC::Manager::Service',
1429 0 ▶ handle_request => sub { return "ok" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1430 8 ▶ );
Subroutine test_child_pid: called CC=1 SCAR=0.0
Pod documented
1431  
1432 4 my $cpid = $handle->child_pid;
1433 4 ok($cpid, "Handle has child_pid");
1434 4 like($cpid, qr/^\d+$/, "child_pid is a positive integer");
1435 4 ok(kill(0, $cpid), "child_pid is alive while service is up");
1436  
1437 # No post_fork_hook: first-fork pid == service pid
1438 4 is($handle->service_pid, $cpid, "child_pid == service_pid (no post_fork_hook)");
1439  
1440 4 $handle = undef;
1441 }
1442  
1443 sub test_child_pid_nested {
1444 # Nested ipcm_service from inside a running service should also
1445 # stamp child_pid onto the peer it returns.
1446  
1447 my $outer = ipcm_service(
1448 'cp_outer',
1449 class => 'IPC::Manager::Service',
1450 on_start => sub {
1451 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
1452  
1453 my $peer = ipcm_service(
1454 'cp_inner',
1455 class => 'IPC::Manager::Service',
1456 0 ▶ handle_request => sub { return "inner_ok" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1457 8 );
1458  
1459 4 $self->{_inner_cpid} = $peer->child_pid;
1460 },
1461  
1462 handle_request => sub {
1463 4 ▶ my ($self, $req, $msg) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
1464 4 return $self->{_inner_cpid};
1465 },
1466 12 ▶ );
Subroutine test_child_pid_nested: called CC=1 SCAR=0.0
Pod documented
1467  
1468 4 my $resp = $outer->sync_request(cp_outer => 'cpid?');
1469 4 my $inner_cpid = $resp->{response};
1470 4 ok($inner_cpid, "Inner peer has child_pid");
1471 4 like($inner_cpid, qr/^\d+$/, "Inner child_pid is a positive integer");
1472  
1473 4 $outer = undef;
1474 }
1475  
1476 sub test_child_pid_unset {
1477 # Handles/peers constructed outside ipcm_service's spawn path have
1478 # child_pid undef.
1479  
1480 4 ▶ require IPC::Manager::Service::Handle;
Subroutine test_child_pid_unset: called CC=1 SCAR=0.0
Pod documented
1481 4 my $h = IPC::Manager::Service::Handle->new(
1482 service_name => 'nope',
1483 ipcm_info => 'fake',
1484 );
1485 4 is($h->child_pid, undef, "Bare handle has undef child_pid");
1486 }
1487  
1488 sub test_child_pid_interpose {
1489 # post_fork_hook interpose pattern: first-fork pid stays alive as
1490 # the long-lived "wrapper", and a deeper fork runs the service loop.
1491 # child_pid should equal the wrapper (first-fork) pid, and that pid
1492 # should stay alive as long as the service is up.
1493  
1494 8 ▶ my $marker_dir = File::Temp::tempdir(CLEANUP => 1);
Subroutine test_child_pid_interpose: called CC=7 SCAR=22.3
Pod documented
1495 8 my $wrapper_file = File::Spec->catfile($marker_dir, 'wrapper_pid');
1496 8 my $service_file = File::Spec->catfile($marker_dir, 'service_pid');
1497  
1498 my $handle = ipcm_service(
1499 'cp_interpose_svc',
1500 class => 'IPC::Manager::Service',
1501  
1502 post_fork => sub {
1503 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=21.1
1504  
1505 4 ▶ my $pid = fork // die "Could not fork in post_fork: $!";
Condition50%1/2
fork // die("Could not fork in post_fork: $!")
l!l
40
MC/DC0%0/1
fork // die("Could not fork in post_fork: $!")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
1506  
1507 4 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 4 4
1508 # Wrapper: record our pid, stay alive, wait for service to exit.
1509 0 ▶ open my $fh, '>', $wrapper_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $wrapper_file or die "open: $!" 0 0 0
1510 0 print $fh "$$\n";
1511 0 close $fh;
1512 0 waitpid($pid, 0);
1513 0 POSIX::_exit(0);
1514 }
1515  
1516 # Deeper child: return to become the service
1517 },
1518  
1519 on_start => sub {
1520 4 ▶ open my $fh, '>', $service_file or die "open: $!";
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $service_file or die "open: $!" 0 4 4
1521 4 print $fh "$$\n";
1522 4 close $fh;
1523 },
1524  
1525 4 ▶ handle_request => sub { return "ok" },
Subroutine __ANON__: called CC=1 SCAR=0.0
1526 8 );
1527  
1528 4 my $waited = 0;
1529 4 ▶ until (-e $wrapper_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $wrapper_file or $waited > 10) 0 4 4
Condition33%1/3
-e $wrapper_file or $waited > 10
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $wrapper_file or $waited > 10
-e $wrapper_file $waited > 10
Truth table33%1/3
A or B
A= -e $wrapper_fileB= $waited > 10
ABresult
1X1
011
000
1530 0 Time::HiRes::sleep(0.1);
1531 0 $waited += 0.1;
1532 }
1533 4 ok(-e $wrapper_file, "Wrapper process ran and wrote marker");
1534  
1535 # Drive one request so service is definitely up
1536 4 my $resp = $handle->sync_request(cp_interpose_svc => 'ping');
1537 4 is($resp->{response}, 'ok', "Service responded");
1538  
1539 4 $waited = 0;
1540 4 ▶ until (-e $service_file || $waited > 10) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $service_file or $waited > 10) 0 4 4
Condition33%1/3
-e $service_file or $waited > 10
l!l&&r!l&&!r
400
MC/DC0%0/2
-e $service_file or $waited > 10
-e $service_file $waited > 10
Truth table33%1/3
A or B
A= -e $service_fileB= $waited > 10
ABresult
1X1
011
000
1541 0 Time::HiRes::sleep(0.1);
1542 0 $waited += 0.1;
1543 }
1544 4 ok(-e $service_file, "Service on_start ran");
1545  
1546 4 ▶ open my $wfh, '<', $wrapper_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $wfh, "<", $wrapper_file or die "open: $!" 0 4 4
1547 4 chomp(my $wrapper_pid = <$wfh>);
1548 4 close $wfh;
1549  
1550 4 ▶ open my $sfh, '<', $service_file or die "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $sfh, "<", $service_file or die "open: $!" 0 4 4
1551 4 chomp(my $svc_pid = <$sfh>);
1552 4 close $sfh;
1553  
1554 4 my $cpid = $handle->child_pid;
1555 4 is($cpid, $wrapper_pid, "child_pid == wrapper (first-fork) pid");
1556 4 isnt($cpid, $svc_pid, "Wrapper pid differs from service pid");
1557 4 ok(kill(0, $cpid), "Wrapper pid alive while service is up");
1558  
1559 4 $handle = undef;
1560 }
1561  
1562 452 1;
1563  
1564 __END__
« Manager/Client/ConnectionUnix.pm ↑ Summary Manager.pm »