Skip to content

Commit d581416

Browse files
committed
feat: set command timeout based on remaining time
1 parent 4206629 commit d581416

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

src/Lambda/Handler/ConsoleCommandLambdaEventHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function handle(InvocationEventInterface $event): ResponseInterface
5858
}
5959

6060
$process = Process::fromShellCommandline("{$event->getCommand()} 2>&1");
61-
$process->setTimeout(null);
61+
$process->setTimeout(max(1, $event->getContext()->getRemainingTimeInMs() / 1000 - 1));
6262
$process->run(function ($type, $output): void {
6363
$this->logger->info($output);
6464
});

tests/Unit/Lambda/Handler/ConsoleCommandLambdaEventHandlerTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@
1717
use Ymir\Runtime\Lambda\Handler\ConsoleCommandLambdaEventHandler;
1818
use Ymir\Runtime\Lambda\Response\ProcessResponse;
1919
use Ymir\Runtime\Tests\Mock\ConsoleCommandEventMockTrait;
20+
use Ymir\Runtime\Tests\Mock\ContextMockTrait;
2021
use Ymir\Runtime\Tests\Mock\InvocationEventInterfaceMockTrait;
2122
use Ymir\Runtime\Tests\Mock\LoggerMockTrait;
2223

2324
class ConsoleCommandLambdaEventHandlerTest extends TestCase
2425
{
2526
use ConsoleCommandEventMockTrait;
27+
use ContextMockTrait;
2628
use InvocationEventInterfaceMockTrait;
2729
use LoggerMockTrait;
2830

31+
public static function provideTimeouts(): \Iterator
32+
{
33+
yield [60000, 59.0];
34+
yield [10000, 9.0];
35+
yield [2000, 1.0];
36+
yield [1000, 1.0];
37+
yield [500, 1.0];
38+
}
39+
2940
public function testCanHandlePingEventType(): void
3041
{
3142
$this->assertTrue((new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->canHandle($this->getConsoleCommandEventMock()));
@@ -36,15 +47,56 @@ public function testCanHandleWrongEventType(): void
3647
$this->assertFalse((new ConsoleCommandLambdaEventHandler($this->getLoggerMock()))->canHandle($this->getInvocationEventInterfaceMock()));
3748
}
3849

50+
/**
51+
* @dataProvider provideTimeouts
52+
*/
53+
public function testHandleSetsCorrectTimeout(int $remainingTimeInMs, float $expectedTimeout): void
54+
{
55+
$context = $this->getContextMock();
56+
$event = $this->getConsoleCommandEventMock();
57+
$logger = $this->getLoggerMock();
58+
59+
$event->expects($this->once())
60+
->method('getCommand')
61+
->willReturn('ls -la');
62+
63+
$event->expects($this->once())
64+
->method('getContext')
65+
->willReturn($context);
66+
67+
$context->expects($this->once())
68+
->method('getRemainingTimeInMs')
69+
->willReturn($remainingTimeInMs);
70+
71+
$response = (new ConsoleCommandLambdaEventHandler($logger))->handle($event);
72+
73+
$reflection = new \ReflectionClass(ProcessResponse::class);
74+
$property = $reflection->getProperty('process');
75+
$property->setAccessible(true);
76+
77+
$process = $property->getValue($response);
78+
79+
$this->assertEquals($expectedTimeout, $process->getTimeout());
80+
}
81+
3982
public function testHandleWithSuccessfulCommand(): void
4083
{
84+
$context = $this->getContextMock();
4185
$event = $this->getConsoleCommandEventMock();
4286
$logger = $this->getLoggerMock();
4387

4488
$event->expects($this->once())
4589
->method('getCommand')
4690
->willReturn('ls -la');
4791

92+
$event->expects($this->once())
93+
->method('getContext')
94+
->willReturn($context);
95+
96+
$context->expects($this->once())
97+
->method('getRemainingTimeInMs')
98+
->willReturn(60000);
99+
48100
$logger->expects($this->once())
49101
->method('info');
50102

@@ -58,13 +110,22 @@ public function testHandleWithSuccessfulCommand(): void
58110

59111
public function testHandleWithUnsuccessfulCommand(): void
60112
{
113+
$context = $this->getContextMock();
61114
$event = $this->getConsoleCommandEventMock();
62115
$logger = $this->getLoggerMock();
63116

64117
$event->expects($this->once())
65118
->method('getCommand')
66119
->willReturn('foo');
67120

121+
$event->expects($this->once())
122+
->method('getContext')
123+
->willReturn($context);
124+
125+
$context->expects($this->once())
126+
->method('getRemainingTimeInMs')
127+
->willReturn(60000);
128+
68129
$logger->expects($this->once())
69130
->method('info');
70131

0 commit comments

Comments
 (0)