Skip to content

Commit a73b1fb

Browse files
authored
Merge pull request #445: Client: fix connection timeout
2 parents 90d2a77 + 468ac78 commit a73b1fb

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/Client/GRPC/Context.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
final class Context implements ContextInterface
2121
{
2222
private ?\DateTimeInterface $deadline = null;
23+
private ?\DateInterval $timeout = null;
2324
private array $options = [];
2425
private array $metadata;
2526
private RetryOptions $retryOptions;
@@ -41,7 +42,8 @@ public function withTimeout($timeout, string $format = DateInterval::FORMAT_SECO
4142
$internal = DateInterval::parse($timeout, $format);
4243

4344
$ctx = clone $this;
44-
$ctx->deadline = (new \DateTimeImmutable())->add($internal);
45+
$ctx->timeout = $internal;
46+
$ctx->deadline = null;
4547

4648
return $ctx;
4749
}
@@ -50,6 +52,7 @@ public function withDeadline(\DateTimeInterface $deadline): self
5052
{
5153
$ctx = clone $this;
5254
$ctx->deadline = $deadline;
55+
$ctx->timeout = null;
5356

5457
return $ctx;
5558
}
@@ -90,7 +93,11 @@ public function getMetadata(): array
9093

9194
public function getDeadline(): ?\DateTimeInterface
9295
{
93-
return $this->deadline;
96+
return match (true) {
97+
$this->deadline !== null => $this->deadline,
98+
$this->timeout !== null => (new \DateTime())->add($this->timeout),
99+
default => null,
100+
};
94101
}
95102

96103
public function getRetryOptions(): RetryOptions

tests/Unit/Client/GRPC/BaseClientTestCase.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Temporal\Client\GRPC\ContextInterface;
1717
use Temporal\Client\GRPC\ServiceClient;
1818
use Temporal\Client\GRPC\StatusCode;
19-
use Temporal\Common\RetryOptions;
2019
use Temporal\Exception\Client\ServiceClientException;
2120
use Temporal\Exception\Client\TimeoutException;
2221
use Temporal\Internal\Interceptor\Pipeline;
@@ -87,6 +86,30 @@ public function testWithContext(): void
8786
$this->assertNotSame($client, $client2);
8887
}
8988

89+
public function testWithTimeoutDynamicDeadline(): void
90+
{
91+
$client = $this->createClientMock();
92+
$context = $client->getContext()->withTimeout(1.234);
93+
94+
$this->assertNotSame($context->getDeadline(), $context->getDeadline());
95+
}
96+
97+
public function testContextGetDeadlineWithoutDeadline(): void
98+
{
99+
$client = $this->createClientMock();
100+
$context = $client->getContext();
101+
102+
$this->assertNull($context->getDeadline());
103+
}
104+
105+
public function testContextGetDeadlineWithStaticDeadline(): void
106+
{
107+
$client = $this->createClientMock();
108+
$context = $client->getContext()->withDeadline(new DateTimeImmutable('+1 second'));
109+
110+
$this->assertSame($context->getDeadline(), $context->getDeadline());
111+
}
112+
90113
public function testWithAuthKey(): void
91114
{
92115
$client = $this->createClientMock();

0 commit comments

Comments
 (0)