From 37247c5b3d5d69d3ffd303b28c51a5269d5c6994 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Wed, 15 Jan 2025 00:08:56 +0400 Subject: [PATCH] Add TypedSA tests --- .../App/Runtime/TemporalStarter.php | 3 + .../Workflow/TypedSearchAttributesTest.php | 80 ++++++++++++++++++- 2 files changed, 80 insertions(+), 3 deletions(-) diff --git a/tests/Acceptance/App/Runtime/TemporalStarter.php b/tests/Acceptance/App/Runtime/TemporalStarter.php index 4514dcda..8ce4749f 100644 --- a/tests/Acceptance/App/Runtime/TemporalStarter.php +++ b/tests/Acceptance/App/Runtime/TemporalStarter.php @@ -26,7 +26,10 @@ public function start(): void $this->environment->startTemporalServer(parameters: [ '--search-attribute', 'foo=text', '--search-attribute', 'bar=int', + '--search-attribute', 'testInt=int', '--search-attribute', 'testFloat=double', + '--search-attribute', 'testString=text', + '--search-attribute', 'testKeyword=keyword', ]); $this->started = true; } diff --git a/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php b/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php index 000f2c80..9ea677a7 100644 --- a/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php +++ b/tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php @@ -19,6 +19,75 @@ #[CoversFunction('Temporal\Internal\Workflow\Process\Process::logRunningHandlers')] class TypedSearchAttributesTest extends TestCase { + #[Test] + public function testStartWithTypedSearchAttributes( + WorkflowClientInterface $client, + Feature $feature, + ): void { + $stub = $client->newUntypedWorkflowStub( + 'Extra_Workflow_TypedSearchAttributes', + WorkflowOptions::new() + ->withTaskQueue($feature->taskQueue) + ->withTypedSearchAttributes( + TypedSearchAttributes::empty() + ->withValue(SearchAttributeKey::forFloat('testFloat'), 1.1) + ), + ); + + /** @see TestWorkflow::handle() */ + $client->start($stub); + + // Complete workflow + /** @see TestWorkflow::exit */ + $stub->signal('exit'); + $result = $stub->getResult(); + + $this->assertSame(['testFloat' => 1.1], (array)$result); + } + + #[Test] + public function testUpsertTypedSearchAttributes( + WorkflowClientInterface $client, + Feature $feature, + ): void { + $stub = $client->newUntypedWorkflowStub( + 'Extra_Workflow_TypedSearchAttributes', + WorkflowOptions::new() + ->withTaskQueue($feature->taskQueue) + ->withSearchAttributes(['testFloat' => 1.1]) + ); + + $toSend = [ + 'testFloat' => 3.25, + ]; + + /** @see TestWorkflow::handle() */ + $client->start($stub); + try { + // Send an empty list of TSA + $stub->signal('setAttributes', []); + + $stub->update('setAttributes', $toSend); + + // Get Search Attributes using Client API + $clientSA = \array_intersect_key( + $toSend, + $stub->describe()->info->searchAttributes->getValues(), + ); + + // Complete workflow + /** @see TestWorkflow::exit */ + $stub->signal('exit'); + } catch (\Throwable $e) { + $stub->terminate('test failed'); + throw $e; + } + + // Get Search Attributes as a Workflow result + $result = $stub->getResult(); + + $this->assertSame($toSend, $clientSA); + } } #[WorkflowInterface] @@ -36,12 +105,17 @@ public function handle() return Workflow::getInfo()->searchAttributes; } - #[Workflow\SignalMethod] + #[Workflow\UpdateMethod] public function setAttributes(array $searchAttributes): void { $updates = []; - foreach ($searchAttributes as $name => $value) { - $updates[] = Workflow::getInfo()->typedSearchAttributes->getByName($name)->valueSet($value); + /** @var SearchAttributeKey $key */ + foreach (Workflow::getInfo()->typedSearchAttributes as $key => $value) { + if (!\array_key_exists($key->getName(), $searchAttributes)) { + continue; + } + + $updates[] = $key->valueSet($searchAttributes[$key->getName()]); } Workflow::upsertTypedSearchAttributes(...$updates);