examples/06-lifespan-state/app.pl

filter: Statementstmt 97.3% 37 / 38 Branchbran 66.6% 8 / 12 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 83.0% 54 / 65 CC 10 SCAR 13.0
CC
10
Cov
86%
handle_http
5.3
handle_lifespan
4.0
app
3.1
CRAP 3.7
« examples/05-sse-broadcaster/app.pl ↑ Summary examples/03-request-body/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 1 ▶async sub handle_lifespan {
Subroutine handle_lifespan: called CC=4 SCAR=13.9
6 1 my ($scope, $receive, $send) = @_;
7  
8 1 ▶ my $state = $scope->{state} //= {};
Condition50%1/2
$scope->{"state"} //= {}
l!l
10
MC/DC0%0/1
$scope->{"state"} //= {}
$scope->{"state"}
Truth table50%1/2
A
A= $scope->{"state"}
Aresult
11
00
9  
10 1 while (1) {
11 2 my $event = await $receive->();
12 2 ▶ if ($event->{type} eq 'lifespan.startup') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'lifespan.startup') { } 1 1 2
elsif ($event->{'type'} eq 'lifespan.shutdown') { } 1 0 1
13 1 $state->{greeting} = 'Hello from lifespan';
14 1 await $send->({ type => 'lifespan.startup.complete' });
15 }
16 elsif ($event->{type} eq 'lifespan.shutdown') {
17 1 await $send->({ type => 'lifespan.shutdown.complete' });
18 1 last;
19 }
20 }
21 1 }
22  
23 1 ▶async sub handle_http {
Subroutine handle_http: called CC=5 SCAR=16.6
24 1 my ($scope, $receive, $send) = @_;
25  
26 1 ▶ my $state = $scope->{state} // {};
Condition50%1/2
$scope->{"state"} // {}
l!l
10
MC/DC0%0/1
$scope->{"state"} // {}
$scope->{"state"}
Truth table50%1/2
A
A= $scope->{"state"}
Aresult
11
00
27 1 ▶ my $greeting = $state->{greeting} // 'Hello';
Condition50%1/2
$state->{"greeting"} // "Hello"
l!l
10
MC/DC0%0/1
$state->{"greeting"} // "Hello"
$state->{"greeting"}
Truth table50%1/2
A
A= $state->{"greeting"}
Aresult
11
00
28  
29 # Drain the request body (if any)
30 1 while (1) {
31 1 my $event = await $receive->();
32 1 ▶ last if $event->{type} ne 'http.request';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"type"} ne "http.request" 0 1 1
33 1 ▶ last unless $event->{more};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"more"} 1 0 1
34 }
35  
36 await $send->({
37 type => 'http.response.start',
38 status => 200,
39 headers => [ [ 'content-type', 'text/plain' ] ],
40 1 });
41  
42 1 await $send->({ type => 'http.response.body', body => "$greeting via shared state", more => 0 });
43 1 }
44  
45 2 ▶async sub app {
Subroutine app: called CC=3 SCAR=11.3
46 2 my ($scope, $receive, $send) = @_;
47  
48 2 ▶ return await handle_lifespan($scope, $receive, $send) if $scope->{type} eq 'lifespan';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} eq "lifespan" 1 1 2
49 1 ▶ return await handle_http($scope, $receive, $send) if $scope->{type} eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $scope->{"type"} eq "http" 1 0 1
50 0 die "Unsupported scope type: $scope->{type}";
51 1 }
52  
53 1 \&app;
« examples/05-sse-broadcaster/app.pl ↑ Summary examples/03-request-body/app.pl »