Lambda/Bootstrap.pm

filter: Statementstmt 86.0% 136 / 158 Branchbran 58.8% 20 / 34 Conditioncond 48.5% 17 / 35 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 87.1% 27 / 31 Podpod 0.0% 0 / 8 totaltotal 71.3% 202 / 283 CC 32 SCAR 21.0
CC
32
Cov
76%
new
27.3
__ANON__
6.0
handle_events
5.8
CRAP 8.2
« Lambda/PSGI.pm ↑ Summary Lambda/ResponseWriter.pm »
1 package AWS::Lambda::Bootstrap;
2 18 ▶use 5.026000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use JSON::XS qw/decode_json encode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use AWS::Lambda;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use AWS::Lambda::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use AWS::Lambda::ResponseWriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 our @EXPORT = ('bootstrap');
16  
17 0 ▶sub bootstrap {
Subroutine bootstrap: not called CC=1 SCAR=6.9
18 0 my $handler = shift;
19 0 my $bootstrap = AWS::Lambda::Bootstrap->new(
20 handler => $handler,
21 );
22 0 $bootstrap->handle_events;
23 }
24  
25 11 ▶sub new {
Subroutine new: called CC=14 SCAR=33.1
Pod undocumented
26 11 my $proto = shift;
27 11 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
28 11 my %args;
29 11 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 11 11
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
30 0 %args = %{$_[0]};
31 } else {
32 11 %args = @_;
33 }
34  
35 11 my $api_version = '2018-06-01';
36 11 ▶ my $env_handler = $args{handler} // $ENV{'_HANDLER'} // die '$_HANDLER is not found';
Condition40%2/5
($args{"handler"} // $ENV{"_HANDLER"}) // die("\$_HANDLER is not found")
l!l
110
$args{"handler"} // $ENV{"_HANDLER"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
($args{"handler"} // $ENV{"_HANDLER"}) // die("\$_HANDLER is not found")
$args{"handler"} $ENV{"_HANDLER"}
Truth table33%1/3
A // B
A= $args{"handler"}B= $ENV{"_HANDLER"}
ABresult
1X1
011
000
37 11 my ($handler, $function) = split(/[.]/, $env_handler, 2);
38 11 ▶ my $runtime_api = $args{runtime_api} // $ENV{'AWS_LAMBDA_RUNTIME_API'} // die '$AWS_LAMBDA_RUNTIME_API is not found';
Condition40%2/5
($args{"runtime_api"} // $ENV{"AWS_LAMBDA_RUNTIME_API"}) // die("\$AWS_LAMBDA_RUNTIME_API is not found")
l!l
110
$args{"runtime_api"} // $ENV{"AWS_LAMBDA_RUNTIME_API"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
($args{"runtime_api"} // $ENV{"AWS_LAMBDA_RUNTIME_API"}) // die("\$AWS_LAMBDA_RUNTIME_API is not found")
$args{"runtime_api"} $ENV{"AWS_LAMBDA_RUNTIME_API"}
Truth table33%1/3
A // B
A= $args{"runtime_api"}B= $ENV{"AWS_LAMBDA_RUNTIME_API"}
ABresult
1X1
011
000
39 11 ▶ my $task_root = $args{task_root} // $ENV{'LAMBDA_TASK_ROOT'} // die '$LAMBDA_TASK_ROOT is not found';
Condition40%2/5
($args{"task_root"} // $ENV{"LAMBDA_TASK_ROOT"}) // die("\$LAMBDA_TASK_ROOT is not found")
l!l
110
$args{"task_root"} // $ENV{"LAMBDA_TASK_ROOT"}
l!l&&r!l&&!r
1100
MC/DC0%0/2
($args{"task_root"} // $ENV{"LAMBDA_TASK_ROOT"}) // die("\$LAMBDA_TASK_ROOT is not found")
$args{"task_root"} $ENV{"LAMBDA_TASK_ROOT"}
Truth table33%1/3
A // B
A= $args{"task_root"}B= $ENV{"LAMBDA_TASK_ROOT"}
ABresult
1X1
011
000
40 11 ▶ my $max_workers = $args{max_workers} // $ENV{'AWS_LAMBDA_MAX_CONCURRENCY'} // 0;
Condition80%4/5
($args{"max_workers"} // $ENV{"AWS_LAMBDA_MAX_CONCURRENCY"}) // 0
l!l
110
$args{"max_workers"} // $ENV{"AWS_LAMBDA_MAX_CONCURRENCY"}
l!l&&r!l&&!r
1010
MC/DC50%1/2
($args{"max_workers"} // $ENV{"AWS_LAMBDA_MAX_CONCURRENCY"}) // 0
$args{"max_workers"} $ENV{"AWS_LAMBDA_MAX_CONCURRENCY"}
Truth table66%2/3
A // B
A= $args{"max_workers"}B= $ENV{"AWS_LAMBDA_MAX_CONCURRENCY"}
ABresult
1X1
011
000
41 11 ▶ die "max_workers must be a non-negative integer, got: $max_workers"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_workers =~ /^\d+$/u and $max_workers >= 0 0 11 11
Condition33%1/3
$max_workers =~ /^\d+$/u and $max_workers >= 0
!ll&&!rl&&r
0011
MC/DC0%0/2
$max_workers =~ /^\d+$/u and $max_workers >= 0
$max_workers =~ /^\d+$/u $max_workers >= 0
Truth table33%1/3
A and B
A= $max_workers =~ /^\d+$/uB= $max_workers >= 0
ABresult
0X0
100
111
42 unless $max_workers =~ /^\d+$/ && $max_workers >= 0;
43 11 my $self = bless +{
44 task_root => $task_root,
45 handler => $handler,
46 function_name => $function,
47 runtime_api => $runtime_api,
48 api_version => $api_version,
49 next_event_url => "http://${runtime_api}/${api_version}/runtime/invocation/next",
50 max_workers => $max_workers,
51 http => HTTP::Tiny->new(
52 # XXX: I want to disable timeout, but it seems HTTP::Tiny does not support it.
53 # So, I set a long timeout.
54 timeout => 365*24*60*60, # 365 days
55 ),
56 }, $class;
57 11 return $self;
58 }
59  
60 1 ▶sub handle_events {
Subroutine handle_events: called CC=5 SCAR=17.6
Pod undocumented
61 1 my $self = shift;
62 1 ▶ $self->_init or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init or return 1 0 1
63  
64 1 ▶ if ($self->{max_workers} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'max_workers'} > 0) { } 1 0 1
65 1 require Parallel::Prefork;
66 my $pm = Parallel::Prefork->new({
67 max_workers => $self->{max_workers},
68 1 trap_signals => {
69 TERM => 'TERM',
70 HUP => 'TERM',
71 },
72 });
73 1 ▶ while ($pm->signal_received ne 'TERM') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pm->signal_received ne "TERM") 1 1 2
74 1 ▶ $pm->start and next;
Branch: 1 branch
BranchTrueFalseTotal
$pm->start and next 1 0 1
75 0 $self->_handle_events;
76 0 $pm->finish;
77 }
78 1 $pm->wait_all_children;
79 } else {
80 0 $self->_handle_events
81 }
82 }
83  
84 0 ▶sub _handle_events {
Subroutine _handle_events: not called CC=1 SCAR=6.9
85 0 my $self = shift;
86 0 while(1) {
87 0 $self->handle_event;
88 }
89 }
90  
91 6 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
92 6 my $self = shift;
93 6 ▶ if (my $func = $self->{function}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = $self->{"function"}) 0 6 6
94 0 return $func;
95 }
96  
97 6 my $task_root = $self->{task_root};
98 6 my $handler = $self->{handler};
99 6 my $name = $self->{function_name};
100 6 ▶ return try {
Subroutine __ANON__: called CC=2 SCAR=6.9
101 package main;
102 6 require "${task_root}/${handler}.pl";
103 5 ▶ my $f = main->can($name) // die "handler $name is not found";
Condition100%2/2
"main"->can($name) // die("handler $name is not found")
l!l
41
MC/DC100%1/1
"main"->can($name) // die("handler $name is not found")
"main"->can($name)
Truth table100%2/2
A
A= "main"->can($name)
Aresult
11
00
104 4 $self->{function} = $f;
105 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
106 2 $self->lambda_init_error($_);
107 2 ▶ $self->{function} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
108 2 undef;
109 6 };
110 }
111  
112 5 ▶sub handle_event {
Subroutine handle_event: called CC=4 SCAR=13.9
Pod undocumented
113 5 my $self = shift;
114 5 ▶ $self->_init or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init or return 3 2 5
115 3 my ($payload, $context) = $self->lambda_next;
116 3 ▶ my $response = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 3 local $AWS::Lambda::context = $context;
118 3 local $ENV{_X_AMZN_TRACE_ID} = $context->{trace_id};
119 3 $self->{function}->($payload, $context);
120 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
121 1 my $err = $_;
122 1 print STDERR "$err";
123 1 $self->lambda_error($err, $context);
124 1 bless {}, 'AWS::Lambda::ErrorSentinel';
125 3 };
126 3 my $ref = ref($response);
127 3 ▶ if ($ref eq 'AWS::Lambda::ErrorSentinel') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "AWS::Lambda::ErrorSentinel") 1 2 3
128 1 return;
129 }
130 2 ▶ if ($ref eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'CODE') { } 1 1 2
131 1 $self->lambda_response_streaming($response, $context);
132 } else {
133 1 $self->lambda_response($response, $context);
134 }
135 2 return 1;
136 }
137  
138 1 ▶sub lambda_next {
Subroutine lambda_next: called CC=2 SCAR=7.1
Pod undocumented
139 1 my $self = shift;
140 1 my $resp = $self->{http}->get($self->{next_event_url});
141 1 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 1 1
142 0 die "failed to retrieve the next event: $resp->{status} $resp->{reason}";
143 }
144 1 my $h = $resp->{headers};
145 1 my $payload = decode_json($resp->{content});
146 return $payload, AWS::Lambda::Context->new(
147 deadline_ms => $h->{'lambda-runtime-deadline-ms'},
148 aws_request_id => $h->{'lambda-runtime-aws-request-id'},
149 invoked_function_arn => $h->{'lambda-runtime-invoked-function-arn'},
150 trace_id => $h->{'lambda-runtime-trace-id'},
151 1 tenant_id => $h->{'lambda-runtime-aws-tenant-id'},
152 );
153 }
154  
155 1 ▶sub lambda_response {
Subroutine lambda_response: called CC=2 SCAR=7.1
Pod undocumented
156 1 my $self = shift;
157 1 my ($response, $context) = @_;
158 1 my $runtime_api = $self->{runtime_api};
159 1 my $api_version = $self->{api_version};
160 1 my $request_id = $context->aws_request_id;
161 1 my $url = "http://${runtime_api}/${api_version}/runtime/invocation/${request_id}/response";
162 1 my $resp = $self->{http}->post($url, {
163 content => encode_json($response),
164 });
165 1 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp->{"success"}) 0 1 1
166 0 die "failed to response of execution: $resp->{status} $resp->{reason}";
167 }
168 }
169  
170 1 ▶sub lambda_response_streaming {
Subroutine lambda_response_streaming: called CC=3 SCAR=12.3
Pod undocumented
171 1 my $self = shift;
172 1 my ($response, $context) = @_;
173 1 my $runtime_api = $self->{runtime_api};
174 1 my $api_version = $self->{api_version};
175 1 my $request_id = $context->aws_request_id;
176 1 my $url = "http://${runtime_api}/${api_version}/runtime/invocation/${request_id}/response";
177 1 my $writer = undef;
178 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
179 1 ▶ $response->(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 1 my $content_type = shift;
181 $writer = AWS::Lambda::ResponseWriter->new(
182 response_url => $url,
183 http => $self->{http},
184 1 );
185 1 $writer->_request($content_type);
186 1 return $writer;
187 1 });
188 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
189 0 my $err = $_;
190 0 print STDERR "$err";
191 0 ▶ if ($writer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writer) { } 0 0 0
192 0 $writer->_close_with_error($err);
193 } else {
194 0 $self->lambda_error($err, $context);
195 }
196 1 };
197 1 ▶ if ($writer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($writer) 1 0 1
198 1 my $response = $writer->_handle_response;
199 1 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->{"success"}) 0 1 1
200 0 die "failed to response of execution: $response->{status} $response->{reason}";
201 }
202 }
203 }
204  
205 1 ▶sub lambda_error {
Subroutine lambda_error: called CC=3 SCAR=11.3
Pod undocumented
206 1 my $self = shift;
207 1 my ($error, $context) = @_;
208 1 my $runtime_api = $self->{runtime_api};
209 1 my $api_version = $self->{api_version};
210 1 my $request_id = $context->aws_request_id;
211 1 my $url = "http://${runtime_api}/${api_version}/runtime/invocation/${request_id}/error";
212 1 ▶ my $type = blessed($error) // "Error";
Condition50%1/2
builtin::blessed($error) // "Error"
l!l
01
MC/DC0%0/1
builtin::blessed($error) // "Error"
builtin::blessed($error)
Truth table50%1/2
A
A= builtin::blessed($error)
Aresult
11
00
213 1 my $resp = $self->{http}->post($url, {
214 content => encode_json({
215 errorMessage => "$error",
216 errorType => "$type",
217 }),
218 });
219 1 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp->{"success"}) 0 1 1
220 0 die "failed to send error of execution: $resp->{status} $resp->{reason}";
221 }
222 }
223  
224 1 ▶sub lambda_init_error {
Subroutine lambda_init_error: called CC=3 SCAR=11.3
Pod undocumented
225 1 my $self = shift;
226 1 my $error = shift;
227 1 my $runtime_api = $self->{runtime_api};
228 1 my $api_version = $self->{api_version};
229 1 my $url = "http://${runtime_api}/${api_version}/runtime/init/error";
230 1 ▶ my $type = blessed($error) // "Error";
Condition50%1/2
builtin::blessed($error) // "Error"
l!l
01
MC/DC0%0/1
builtin::blessed($error) // "Error"
builtin::blessed($error)
Truth table50%1/2
A
A= builtin::blessed($error)
Aresult
11
00
231 1 my $resp = $self->{http}->post($url, {
232 content => encode_json({
233 errorMessage => "$error",
234 errorType => "$type",
235 }),
236 });
237 1 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $resp->{"success"}) 0 1 1
238 0 die "failed to send error of execution: $resp->{status} $resp->{reason}";
239 }
240 }
241  
242 18 1;
243 __END__
« Lambda/PSGI.pm ↑ Summary Lambda/ResponseWriter.pm »