Skip to content

Commit 3c4675a

Browse files
committed
Add Temporal-Namespace metadata into client calls
1 parent b1ca5dd commit 3c4675a

File tree

4 files changed

+58
-53
lines changed

4 files changed

+58
-53
lines changed

src/Client/GRPC/BaseClient.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ private function call(string $method, object $arg, ContextInterface $ctx): objec
276276
$options['timeout'] = CarbonInterval::instance($diff)->totalMicroseconds;
277277
}
278278

279-
tr(['method' => $method, 'arg' => $arg, 'metadata' => $ctx->getMetadata(), 'options' => $options]);
280-
281279
/** @var UnaryCall $call */
282280
$call = $this->connection->getWorkflowService()->{$method}($arg, $ctx->getMetadata(), $options);
283281
[$result, $status] = $call->wait();

src/Client/ScheduleClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,20 @@ public function __construct(
5151
?ClientOptions $options = null,
5252
?DataConverterInterface $converter = null,
5353
) {
54-
$this->client = $serviceClient;
5554
$this->clientOptions = $options ?? new ClientOptions();
5655
$this->converter = $converter ?? DataConverter::createDefault();
5756
$this->marshaller = new Marshaller(
5857
new AttributeMapperFactory(new AttributeReader()),
5958
);
6059
$this->protoConverter = new ProtoToArrayConverter($this->converter);
60+
61+
// Set Temporal-Namespace metadata
62+
$context = $serviceClient->getContext();
63+
$this->client = $serviceClient->withContext(
64+
$context->withMetadata(
65+
['Temporal-Namespace' => [$this->clientOptions->namespace]] + $context->getMetadata(),
66+
),
67+
);
6168
}
6269

6370
public static function create(

src/Client/WorkflowClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,19 @@ public function __construct(
7474
?DataConverterInterface $converter = null,
7575
?PipelineProvider $interceptorProvider = null,
7676
) {
77-
$this->client = $serviceClient;
7877
$this->interceptorPipeline = ($interceptorProvider ?? new SimplePipelineProvider())
7978
->getPipeline(WorkflowClientCallsInterceptor::class);
8079
$this->clientOptions = $options ?? new ClientOptions();
8180
$this->converter = $converter ?? DataConverter::createDefault();
8281
$this->reader = new WorkflowReader($this->createReader());
82+
83+
// Set Temporal-Namespace metadata
84+
$context = $serviceClient->getContext();
85+
$this->client = $serviceClient->withContext(
86+
$context->withMetadata(
87+
['Temporal-Namespace' => [$this->clientOptions->namespace]] + $context->getMetadata(),
88+
),
89+
);
8390
}
8491

8592
/**

tests/Unit/Client/GRPC/BaseClientTestCase.php

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Temporal\Tests\Unit\Client\GRPC;
66

7-
use DateTimeImmutable;
87
use PHPUnit\Framework\TestCase;
98
use Temporal\Api\Workflowservice\V1\GetSystemInfoRequest;
109
use Temporal\Api\Workflowservice\V1\GetSystemInfoResponse;
@@ -48,18 +47,14 @@ public function testGetCapabilitiesClearsCache(): void
4847
public function testClose(): void
4948
{
5049
$client = $this->createClientMock(static fn() => new class extends WorkflowServiceClient {
51-
public function __construct()
52-
{
53-
}
50+
public function __construct() {}
5451

5552
public function getConnectivityState($try_to_connect = false): int
5653
{
5754
return ConnectionState::TransientFailure->value;
5855
}
5956

60-
public function close(): void
61-
{
62-
}
57+
public function close(): void {}
6358
});
6459
$client->close();
6560

@@ -105,7 +100,7 @@ public function testContextGetDeadlineWithoutDeadline(): void
105100
public function testContextGetDeadlineWithStaticDeadline(): void
106101
{
107102
$client = $this->createClientMock();
108-
$context = $client->getContext()->withDeadline(new DateTimeImmutable('+1 second'));
103+
$context = $client->getContext()->withDeadline(new \DateTimeImmutable('+1 second'));
109104

110105
$this->assertSame($context->getDeadline(), $context->getDeadline());
111106
}
@@ -160,22 +155,21 @@ public function __toString(): string
160155

161156
public function testServiceClientCallDeadlineReached(): void
162157
{
163-
$client = $this->createClientMock(fn() => new class() extends WorkflowServiceClient {
158+
$client = $this->createClientMock(static fn() => new class extends WorkflowServiceClient {
164159
public function __construct() {}
165-
public function testCall()
166-
{
167-
throw new class((object)['code' => StatusCode::UNKNOWN, 'metadata' => []])
168-
extends ServiceClientException {
169-
};
170-
}
171-
public function close(): void
160+
161+
public function testCall(): void
172162
{
163+
throw new class((object) ['code' => StatusCode::UNKNOWN, 'metadata' => []]) extends ServiceClientException {};
173164
}
165+
166+
public function close(): void {}
174167
})->withInterceptorPipeline(null);
175168

176-
$client = $client->withContext($client->getContext()
177-
->withDeadline(new DateTimeImmutable('-1 second'))
178-
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(2)) // stop if deadline doesn't work
169+
$client = $client->withContext(
170+
$client->getContext()
171+
->withDeadline(new \DateTimeImmutable('-1 second'))
172+
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(2)), // stop if deadline doesn't work
179173
);
180174

181175
self::expectException(TimeoutException::class);
@@ -185,20 +179,21 @@ public function close(): void
185179

186180
public function testServiceClientCallCustomException(): void
187181
{
188-
$client = $this->createClientMock(fn() => new class() extends WorkflowServiceClient {
182+
$client = $this->createClientMock(static fn() => new class extends WorkflowServiceClient {
189183
public function __construct() {}
190-
public function testCall()
184+
185+
public function testCall(): void
191186
{
192187
throw new \RuntimeException('foo');
193188
}
194-
public function close(): void
195-
{
196-
}
189+
190+
public function close(): void {}
197191
})->withInterceptorPipeline(null);
198192

199-
$client = $client->withContext($client->getContext()
200-
->withDeadline(new DateTimeImmutable('-1 second'))
201-
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(2)) // stop if deadline doesn't work
193+
$client = $client->withContext(
194+
$client->getContext()
195+
->withDeadline(new \DateTimeImmutable('-1 second'))
196+
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(2)), // stop if deadline doesn't work
202197
);
203198

204199
self::expectException(\RuntimeException::class);
@@ -212,31 +207,32 @@ public function close(): void
212207
*/
213208
public function testServiceClientCallMaximumAttemptsReached(): void
214209
{
215-
$client = $this->createClientMock(fn() => new class() extends WorkflowServiceClient {
210+
$client = $this->createClientMock(fn() => new class extends WorkflowServiceClient {
216211
public function __construct() {}
217-
public function testCall()
212+
213+
public function testCall(): void
218214
{
219215
static $counter = 0;
220-
throw new class(++$counter)
221-
extends ServiceClientException {
216+
throw new class(++$counter) extends ServiceClientException {
222217
public function __construct(public int $attempt)
223218
{
224-
parent::__construct((object)['code' => StatusCode::UNKNOWN, 'metadata' => []]);
219+
parent::__construct((object) ['code' => StatusCode::UNKNOWN, 'metadata' => []]);
225220
}
221+
226222
public function isTestError(): bool
227223
{
228224
return true;
229225
}
230226
};
231227
}
232-
public function close(): void
233-
{
234-
}
228+
229+
public function close(): void {}
235230
})->withInterceptorPipeline(null);
236231

237-
$client = $client->withContext($client->getContext()
238-
->withDeadline(new DateTimeImmutable('+2 seconds')) // stop if attempts don't work
239-
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(3)->withBackoffCoefficient(1))
232+
$client = $client->withContext(
233+
$client->getContext()
234+
->withDeadline(new \DateTimeImmutable('+2 seconds')) // stop if attempts don't work
235+
->withRetryOptions(RpcRetryOptions::new()->withMaximumAttempts(3)->withBackoffCoefficient(1)),
240236
);
241237

242238
try {
@@ -251,41 +247,38 @@ public function close(): void
251247
private function createClientMock(?callable $serviceClientFactory = null): BaseClient
252248
{
253249
return (new class($serviceClientFactory ?? static fn() => new class extends WorkflowServiceClient {
254-
public function __construct()
255-
{
256-
}
250+
public function __construct() {}
257251

258252
public function getConnectivityState($try_to_connect = false): int
259253
{
260254
return ConnectionState::Ready->value;
261255
}
262256

263-
public function close(): void
264-
{
265-
}
257+
public function close(): void {}
266258
}) extends ServiceClient {
259+
267260
public function getSystemInfo(
268261
GetSystemInfoRequest $arg,
269-
ContextInterface $ctx = null,
262+
?ContextInterface $ctx = null,
270263
): GetSystemInfoResponse {
271264
return (new GetSystemInfoResponse())
272-
->setCapabilities((new Capabilities)->setSupportsSchedules(true))
265+
->setCapabilities((new Capabilities())->setSupportsSchedules(true))
273266
->setServerVersion('1.2.3');
274267
}
275268

276269
public function testCall(): mixed
277270
{
278-
return $this->invoke("testCall", (object)[], null);
271+
return $this->invoke("testCall", (object) [], null);
279272
}
280273
})->withInterceptorPipeline(
281274
Pipeline::prepare([new class implements \Temporal\Interceptor\GrpcClientInterceptor {
282275
public function interceptCall(
283276
string $method,
284277
object $arg,
285278
ContextInterface $ctx,
286-
callable $next
279+
callable $next,
287280
): object {
288-
return (object)['method' => $method, 'arg' => $arg, 'ctx' => $ctx, 'next' => $next];
281+
return (object) ['method' => $method, 'arg' => $arg, 'ctx' => $ctx, 'next' => $next];
289282
}
290283
}]),
291284
);

0 commit comments

Comments
 (0)