examples/05-sse-broadcaster/app.pl

filter: Statementstmt 100% 33 / 33 Branchbran 60.0% 6 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 85.1% 46 / 54 CC 8 SCAR 13.2
CC
8
Cov
87%
app
6.3
maybe_sleep
2.1
watch_sse_disconnect
2.0
CRAP 3.8
« bin/pagi-server ↑ Summary examples/06-lifespan-state/app.pl »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Try to load Future::IO for loop-agnostic sleep, fall back to immediate if not available
6 1 my $HAS_FUTURE_IO = eval { require Future::IO; 1 };
7  
8 3 ▶sub maybe_sleep {
Subroutine maybe_sleep: called CC=2 SCAR=7.2
9 3 my ($seconds) = @_;
10 3 ▶ return $HAS_FUTURE_IO ? Future::IO->sleep($seconds) : Future->done;
Branch: 1 branch
BranchTrueFalseTotal
$HAS_FUTURE_IO ? : 3 0 3
11 }
12  
13 1 ▶async sub watch_sse_disconnect {
Subroutine watch_sse_disconnect: called CC=2 SCAR=7.0
14 1 my ($receive) = @_;
15  
16 1 while (1) {
17 2 my $event = await $receive->();
18 1 ▶ return $event if $event->{type} eq 'sse.disconnect';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"type"} eq "sse.disconnect" 0 1 1
19 }
20 1 }
21  
22 2 ▶async sub app {
Subroutine app: called CC=6 SCAR=18.4
23 2 my ($scope, $receive, $send) = @_;
24  
25 2 ▶ die "Unsupported scope type: $scope->{type}" if $scope->{type} ne 'sse';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} ne "sse" 1 1 2
26  
27 1 await $send->({
28 type => 'sse.start',
29 status => 200,
30 headers => [ [ 'content-type', 'text/event-stream' ] ],
31 });
32  
33 1 my $disconnect = watch_sse_disconnect($receive);
34 1 my @events = (
35 { event => 'tick', data => '1' },
36 { event => 'tick', data => '2' },
37 { event => 'done', data => 'finished' },
38 );
39  
40 1 for my $msg (@events) {
41 3 ▶ last if $disconnect->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect->is_ready 0 3 3
42 3 await maybe_sleep(1);
43 3 await $send->({ type => 'sse.send', %$msg });
44 }
45  
46 1 ▶ $disconnect->cancel if $disconnect->can('cancel') && !$disconnect->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $disconnect->can("cancel") and not $disconnect->is_ready 1 0 1
Condition33%1/3
$disconnect->can("cancel") and not $disconnect->is_ready
!ll&&!rl&&r
001
MC/DC0%0/2
$disconnect->can("cancel") and not $disconnect->is_ready
$disconnect->can("cancel") not $disconnect->is_ready
Truth table33%1/3
A and not(B)
A= $disconnect->can("cancel")B= not $disconnect->is_ready
ABresult
0X0
100
111
47 1 }
48  
49 1 \&app; # Return coderef when loaded via do
« bin/pagi-server ↑ Summary examples/06-lifespan-state/app.pl »