8
8
use GuzzleHttp \Server \Server ;
9
9
use PHPUnit \Framework \TestCase ;
10
10
use Runtime \Bref \Lambda \LambdaClient ;
11
+ use Runtime \Bref \Timeout \LambdaTimeoutException ;
12
+ use Runtime \Bref \Timeout \Timeout ;
11
13
12
14
/**
13
15
* Tests the communication between `LambdaClient` and the Lambda Runtime HTTP API.
@@ -189,9 +191,9 @@ public function handle($event, Context $context)
189
191
190
192
// Check the lambda result contains the error message
191
193
$ error = json_decode ((string ) $ eventFailureLog ->getBody (), true );
192
- $ this ->assertSame ('Error while calling the Lambda runtime API: The requested URL returned error: 400 Bad Request ' , $ error ['errorMessage ' ]);
194
+ $ this ->assertStringContainsString ('Error while calling the Lambda runtime API: The requested URL returned error: 400 ' , $ error ['errorMessage ' ]);
193
195
194
- $ this ->assertErrorInLogs ('Exception ' , 'Error while calling the Lambda runtime API: The requested URL returned error: 400 Bad Request ' );
196
+ $ this ->assertErrorInLogs ('Exception ' , 'Error while calling the Lambda runtime API: The requested URL returned error: 400 ' );
195
197
}
196
198
197
199
public function test function results that cannot be encoded are reported as invocation errors ()
@@ -235,6 +237,28 @@ public function handle($event, Context $context)
235
237
$ this ->assertInvocationResult (['foo ' => 'bar ' ]);
236
238
}
237
239
240
+ public function testLambdaTimeoutsCanBeAnticipated ()
241
+ {
242
+ $ this ->givenAnEvent ([]);
243
+
244
+ $ start = microtime (true );
245
+ $ this ->lambda ->processNextEvent (new class implements Handler {
246
+ public function handle ($ event , Context $ context )
247
+ {
248
+ // This 10s sleep should be interrupted
249
+ sleep (10 );
250
+ }
251
+ });
252
+
253
+ $ elapsedTime = microtime (true ) - $ start ;
254
+ // The Lambda timeout was 2 seconds, we expect the Bref timeout to trigger 1 second before that: 1 second
255
+ $ this ->assertEqualsWithDelta (1 , $ elapsedTime , 0.2 );
256
+ Timeout::reset ();
257
+
258
+ $ this ->assertInvocationErrorResult (LambdaTimeoutException::class, 'Maximum AWS Lambda execution time reached ' );
259
+ $ this ->assertErrorInLogs (LambdaTimeoutException::class, 'Maximum AWS Lambda execution time reached ' );
260
+ }
261
+
238
262
/**
239
263
* @param mixed $event
240
264
*/
@@ -246,6 +270,8 @@ private function givenAnEvent($event): void
246
270
[
247
271
'lambda-runtime-aws-request-id ' => '1 ' ,
248
272
'lambda-runtime-invoked-function-arn ' => 'test-function-name ' ,
273
+ // now + 2 seconds
274
+ 'lambda-runtime-deadline-ms ' => intval ((microtime (true ) + 2 ) * 1000 ),
249
275
],
250
276
json_encode ($ event )
251
277
),
@@ -312,7 +338,7 @@ private function assertErrorInLogs(string $errorClass, string $errorMessage): vo
312
338
'stack ' ,
313
339
], array_keys ($ invocationResult ));
314
340
$ this ->assertEquals ($ errorClass , $ invocationResult ['errorType ' ]);
315
- $ this ->assertEquals ($ errorMessage , $ invocationResult ['errorMessage ' ]);
341
+ $ this ->assertStringContainsString ($ errorMessage , $ invocationResult ['errorMessage ' ]);
316
342
$ this ->assertIsArray ($ invocationResult ['stack ' ]);
317
343
}
318
344
0 commit comments