From 9d1079b123f7a2c83a7822abf2e1dbbe77b225c8 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 3 Jun 2024 19:54:53 +0400 Subject: [PATCH 1/2] Client: fix connection timeout that was converted into static deadline for all requests --- src/Client/GRPC/Context.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Client/GRPC/Context.php b/src/Client/GRPC/Context.php index c2dff46f..3ba0279f 100644 --- a/src/Client/GRPC/Context.php +++ b/src/Client/GRPC/Context.php @@ -20,6 +20,7 @@ final class Context implements ContextInterface { private ?\DateTimeInterface $deadline = null; + private ?\DateInterval $timeout = null; private array $options = []; private array $metadata; private RetryOptions $retryOptions; @@ -41,7 +42,8 @@ public function withTimeout($timeout, string $format = DateInterval::FORMAT_SECO $internal = DateInterval::parse($timeout, $format); $ctx = clone $this; - $ctx->deadline = (new \DateTimeImmutable())->add($internal); + $ctx->timeout = $internal; + $ctx->deadline =null; return $ctx; } @@ -50,6 +52,7 @@ public function withDeadline(\DateTimeInterface $deadline): self { $ctx = clone $this; $ctx->deadline = $deadline; + $ctx->timeout = null; return $ctx; } @@ -90,7 +93,11 @@ public function getMetadata(): array public function getDeadline(): ?\DateTimeInterface { - return $this->deadline; + return match (true) { + $this->deadline !== null => $this->deadline, + $this->timeout !== null => (new \DateTime())->add($this->timeout), + default => null, + }; } public function getRetryOptions(): RetryOptions From 468ac78ac8e7344620a96f6d306f18ad11b11da4 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Mon, 3 Jun 2024 20:05:21 +0400 Subject: [PATCH 2/2] Add tests --- src/Client/GRPC/Context.php | 2 +- tests/Unit/Client/GRPC/BaseClientTestCase.php | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Client/GRPC/Context.php b/src/Client/GRPC/Context.php index 3ba0279f..12b9034d 100644 --- a/src/Client/GRPC/Context.php +++ b/src/Client/GRPC/Context.php @@ -43,7 +43,7 @@ public function withTimeout($timeout, string $format = DateInterval::FORMAT_SECO $ctx = clone $this; $ctx->timeout = $internal; - $ctx->deadline =null; + $ctx->deadline = null; return $ctx; } diff --git a/tests/Unit/Client/GRPC/BaseClientTestCase.php b/tests/Unit/Client/GRPC/BaseClientTestCase.php index 36adcfe7..66d6222f 100644 --- a/tests/Unit/Client/GRPC/BaseClientTestCase.php +++ b/tests/Unit/Client/GRPC/BaseClientTestCase.php @@ -16,7 +16,6 @@ use Temporal\Client\GRPC\ContextInterface; use Temporal\Client\GRPC\ServiceClient; use Temporal\Client\GRPC\StatusCode; -use Temporal\Common\RetryOptions; use Temporal\Exception\Client\ServiceClientException; use Temporal\Exception\Client\TimeoutException; use Temporal\Internal\Interceptor\Pipeline; @@ -87,6 +86,30 @@ public function testWithContext(): void $this->assertNotSame($client, $client2); } + public function testWithTimeoutDynamicDeadline(): void + { + $client = $this->createClientMock(); + $context = $client->getContext()->withTimeout(1.234); + + $this->assertNotSame($context->getDeadline(), $context->getDeadline()); + } + + public function testContextGetDeadlineWithoutDeadline(): void + { + $client = $this->createClientMock(); + $context = $client->getContext(); + + $this->assertNull($context->getDeadline()); + } + + public function testContextGetDeadlineWithStaticDeadline(): void + { + $client = $this->createClientMock(); + $context = $client->getContext()->withDeadline(new DateTimeImmutable('+1 second')); + + $this->assertSame($context->getDeadline(), $context->getDeadline()); + } + public function testWithAuthKey(): void { $client = $this->createClientMock();