Skip to content

Commit

Permalink
Apply grpcContext from config; add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Aug 30, 2024
1 parent 8bb069f commit 22401af
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Spiral\Core\FactoryInterface;
use Spiral\RoadRunnerBridge\Bootloader\RoadRunnerBootloader;
use Spiral\TemporalBridge\Commands;
use Spiral\TemporalBridge\Config\ClientConfig;
use Spiral\TemporalBridge\Config\ConnectionConfig;
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\DeclarationLocator;
Expand Down Expand Up @@ -155,18 +156,19 @@ protected function initConfig(EnvironmentInterface $env): void
$this->config->setDefaults(
TemporalConfig::CONFIG,
[
'connection' => $env->get('TEMPORAL_CONNECTION', 'default'),
'connections' => [
'default' => ConnectionConfig::new(
address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
'client' => $env->get('TEMPORAL_CONNECTION', 'default'),
'clients' => [
'default' => ClientConfig::new(
ConnectionConfig::new(
address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
),
),
],
'defaultWorker' => (string)$env->get(
'TEMPORAL_TASK_QUEUE',
TemporalWorkerFactoryInterface::DEFAULT_TASK_QUEUE,
),
'workers' => [],
'clientOptions' => null,
],
);
}
Expand All @@ -176,7 +178,7 @@ protected function initServiceClient(TemporalConfig $config): ServiceClientInter
$client = $config->getClientConfig($config->getDefaultClient());
$connection = $client->connection;

return $connection->isSecure()
$result = $connection->isSecure()
? ServiceClient::createSSL(
address: $connection->address,
crt: $connection->tlsConfig->rootCerts,
Expand All @@ -185,6 +187,8 @@ protected function initServiceClient(TemporalConfig $config): ServiceClientInter
overrideServerName: $connection->tlsConfig->serverName,
)
: ServiceClient::create(address: $connection->address);

return $result->withContext($client->context);
}

protected function initPipelineProvider(TemporalConfig $config, FactoryInterface $factory): PipelineProvider
Expand Down
7 changes: 6 additions & 1 deletion tests/app/config/temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Spiral\TemporalBridge\Config\ClientConfig;
use Spiral\TemporalBridge\Config\ConnectionConfig;
use Temporal\Client\ClientOptions;
use Temporal\Client\GRPC\Context;

return [
'client' => env('TEMPORAL_CONNECTION', 'default'),
Expand All @@ -15,7 +17,10 @@
rootCerts: '/path/to/crt',
privateKey: '/path/to/clientKey',
certChain: '/path/to/clientPem',
)
),
options: (new ClientOptions())
->withNamespace('foo-bar'),
context: Context::default()->withMetadata(['foo' => ['bar']]),
),
],
];
23 changes: 23 additions & 0 deletions tests/src/Bootloader/TemporalBridgeBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Spiral\TemporalBridge\WorkersRegistryInterface;
use Spiral\Testing\Attribute\Env;
use Temporal\Api\Workflowservice\V1\WorkflowServiceClient;
use Temporal\Client\ClientOptions;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\GRPC\ServiceClientInterface;
use Temporal\Client\ScheduleClient;
Expand Down Expand Up @@ -137,6 +138,28 @@ public function testSecureConnection(): void
$this->assertStringContainsString('ssl:7233', $workflowService->getTarget());
}

#[Env('TEMPORAL_CONNECTION', 'ssl')]
public function testContext(): void
{
$client = $this->getContainer()->get(ServiceClientInterface::class);
\assert($client instanceof ServiceClientInterface);
$context = $client->getContext();

$this->assertSame(['foo' => ['bar']], $context->getMetadata());
}

#[Env('TEMPORAL_CONNECTION', 'ssl')]
public function testClientOptions(): void
{
$client = $this->getContainer()->get(WorkflowClientInterface::class);
\assert($client instanceof WorkflowClientInterface);

$clientOptions = (fn() => $client->clientOptions)->call($client);
\assert($clientOptions instanceof ClientOptions);

$this->assertSame('foo-bar', $clientOptions->namespace);
}

#[Env('TEMPORAL_CONNECTION', 'test')]
public function testNonExistsConnection(): void
{
Expand Down

0 comments on commit 22401af

Please sign in to comment.