Skip to content

Commit

Permalink
Merge pull request #445: Client: fix connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Jun 3, 2024
2 parents 90d2a77 + 468ac78 commit a73b1fb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/Client/GRPC/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -50,6 +52,7 @@ public function withDeadline(\DateTimeInterface $deadline): self
{
$ctx = clone $this;
$ctx->deadline = $deadline;
$ctx->timeout = null;

return $ctx;
}
Expand Down Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion tests/Unit/Client/GRPC/BaseClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a73b1fb

Please sign in to comment.