File Coverage

examples/06-lifespan-state/app.pl
Criterion Covered Total %
statement 33 34 97.0
branch 8 12 66.6
condition 3 6 50.0
subroutine 6 6 100.0
pod n/a
total 50 58 86.2


line stmt bran cond sub pod time code
1 1     1   203962 use strict;
  1         2  
  1         32  
2 1     1   4 use warnings;
  1         2  
  1         36  
3 1     1   4 use Future::AsyncAwait;
  1         1  
  1         10  
4              
5 1     1   1 async sub handle_lifespan {
6 1         1 my ($scope, $receive, $send) = @_;
7              
8 1   50     24 my $state = $scope->{state} //= {};
9              
10 1         2 while (1) {
11 2         29 my $event = await $receive->();
12 2 100       135 if ($event->{type} eq 'lifespan.startup') {
    50          
13 1         2 $state->{greeting} = 'Hello from lifespan';
14 1         3 await $send->({ type => 'lifespan.startup.complete' });
15             }
16             elsif ($event->{type} eq 'lifespan.shutdown') {
17 1         4 await $send->({ type => 'lifespan.shutdown.complete' });
18 1         36 last;
19             }
20             }
21             }
22              
23 1     1   2 async sub handle_http {
24 1         3 my ($scope, $receive, $send) = @_;
25              
26 1   50     4 my $state = $scope->{state} // {};
27 1   50     15 my $greeting = $state->{greeting} // 'Hello';
28              
29             # Drain the request body (if any)
30 1         3 while (1) {
31 1         5 my $event = await $receive->();
32 1 50       28 last if $event->{type} ne 'http.request';
33 1 50       7 last unless $event->{more};
34             }
35              
36             await $send->({
37             type => 'http.response.start',
38             status => 200,
39             headers => [ [ 'content-type', 'text/plain' ] ],
40 1         26 });
41              
42 1         64 await $send->({ type => 'http.response.body', body => "$greeting via shared state", more => 0 });
43             }
44              
45 2     2   4 async sub app {
46 2         34 my ($scope, $receive, $send) = @_;
47              
48 2 100       10 return await handle_lifespan($scope, $receive, $send) if $scope->{type} eq 'lifespan';
49 1 50       8 return await handle_http($scope, $receive, $send) if $scope->{type} eq 'http';
50 0           die "Unsupported scope type: $scope->{type}";
51             }
52              
53             \&app;