|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Timeout; |
| 4 | + |
| 5 | +use Bref\Timeout\LambdaTimeout; |
| 6 | +use Bref\Timeout\Timeout; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +class TimeoutTest extends TestCase |
| 10 | +{ |
| 11 | + public static function setUpBeforeClass(): void |
| 12 | + { |
| 13 | + if (! function_exists('pcntl_async_signals')) { |
| 14 | + self::markTestSkipped('PCNTL extension is not enabled.'); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + protected function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + unset($_SERVER['LAMBDA_INVOCATION_CONTEXT']); |
| 22 | + unset($_SERVER['BREF_TIMEOUT']); |
| 23 | + } |
| 24 | + |
| 25 | + protected function tearDown(): void |
| 26 | + { |
| 27 | + Timeout::reset(); |
| 28 | + parent::tearDown(); |
| 29 | + } |
| 30 | + |
| 31 | + public function testEnableWithoutContext() |
| 32 | + { |
| 33 | + $this->expectException(\LogicException::class); |
| 34 | + Timeout::enable(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testEnableWithBrefTimeoutInactive() |
| 38 | + { |
| 39 | + $_SERVER['BREF_TIMEOUT'] = -1; |
| 40 | + $_SERVER['LAMBDA_INVOCATION_CONTEXT'] = json_encode(['deadlineMs' => (time() + 30) * 1000]); |
| 41 | + |
| 42 | + Timeout::enable(); |
| 43 | + $timeout = pcntl_alarm(0); |
| 44 | + $this->assertSame(0, $timeout, 'Timeout should not be active when BREF_TIMEOUT=-1'); |
| 45 | + } |
| 46 | + |
| 47 | + public function testEnableWithBrefTimeout() |
| 48 | + { |
| 49 | + $_SERVER['BREF_TIMEOUT'] = 10; |
| 50 | + |
| 51 | + Timeout::enable(); |
| 52 | + $timeout = pcntl_alarm(0); |
| 53 | + $this->assertSame(10, $timeout, 'BREF_TIMEOUT=10 should have effect'); |
| 54 | + } |
| 55 | + |
| 56 | + public function testEnableWithBrefTimeoutAndContext() |
| 57 | + { |
| 58 | + $_SERVER['BREF_TIMEOUT'] = 10; |
| 59 | + $_SERVER['LAMBDA_INVOCATION_CONTEXT'] = json_encode(['deadlineMs' => (time() + 30) * 1000]); |
| 60 | + |
| 61 | + Timeout::enable(); |
| 62 | + $timeout = pcntl_alarm(0); |
| 63 | + $this->assertSame(10, $timeout, 'BREF_TIMEOUT=10 should have effect over context'); |
| 64 | + } |
| 65 | + |
| 66 | + public function testEnableWithBrefTimeoutZeroAndContext() |
| 67 | + { |
| 68 | + $_SERVER['BREF_TIMEOUT'] = 0; |
| 69 | + $_SERVER['LAMBDA_INVOCATION_CONTEXT'] = json_encode(['deadlineMs' => (time() + 30) * 1000]); |
| 70 | + |
| 71 | + Timeout::enable(); |
| 72 | + $timeout = pcntl_alarm(0); |
| 73 | + $this->assertEqualsWithDelta(30, $timeout, 1, 'BREF_TIMEOUT=0 should fallback to context'); |
| 74 | + } |
| 75 | + |
| 76 | + public function testEnableWithContext() |
| 77 | + { |
| 78 | + $_SERVER['LAMBDA_INVOCATION_CONTEXT'] = json_encode(['deadlineMs' => (time() + 30) * 1000]); |
| 79 | + |
| 80 | + Timeout::enable(); |
| 81 | + $timeout = pcntl_alarm(0); |
| 82 | + $this->assertEqualsWithDelta(30, $timeout, 1); |
| 83 | + } |
| 84 | + |
| 85 | + public function testTimeoutAfter() |
| 86 | + { |
| 87 | + $start = microtime(true); |
| 88 | + Timeout::timeoutAfter(2); |
| 89 | + try { |
| 90 | + sleep(4); |
| 91 | + $this->fail('We expect a LambdaTimeout before we reach this line'); |
| 92 | + } catch (LambdaTimeout $e) { |
| 93 | + $time = 1000 * (microtime(true) - $start); |
| 94 | + $this->assertEqualsWithDelta(2000, $time, 200, 'We must wait about 2 seconds'); |
| 95 | + } catch (\Throwable $e) { |
| 96 | + $this->fail('It must throw a LambdaTimeout.'); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments