-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathTimeoutTest.php
More file actions
53 lines (46 loc) · 1.39 KB
/
TimeoutTest.php
File metadata and controls
53 lines (46 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Runtime\Bref\Tests;
use PHPUnit\Framework\TestCase;
use Runtime\Bref\Timeout\LambdaTimeoutException;
use Runtime\Bref\Timeout\Timeout;
class TimeoutTest extends TestCase
{
public static function setUpBeforeClass(): void
{
if (!function_exists('pcntl_async_signals')) {
self::markTestSkipped('PCNTL extension is not enabled.');
}
}
protected function setUp(): void
{
parent::setUp();
unset($_SERVER['LAMBDA_INVOCATION_CONTEXT']);
}
protected function tearDown(): void
{
Timeout::reset();
parent::tearDown();
}
public function testEnable()
{
Timeout::enable(3000);
$timeout = pcntl_alarm(0);
// 1 second (2 seconds shorter than the 3s remaining time)
$this->assertSame(1, $timeout);
}
public function testTimeoutsAreInterruptedInTime()
{
$start = microtime(true);
Timeout::enable(3000);
try {
sleep(4);
$this->fail('We expect a LambdaTimeout before we reach this line');
} catch (LambdaTimeoutException $e) {
$time = 1000 * (microtime(true) - $start);
$this->assertEqualsWithDelta(1000, $time, 200, 'We must wait about 1 second');
Timeout::reset();
} catch (\Throwable $e) {
$this->fail('It must throw a LambdaTimeout.');
}
}
}