Skip to content

Commit

Permalink
Add TypedSA tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 14, 2025
1 parent 770b41d commit 37247c5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tests/Acceptance/App/Runtime/TemporalStarter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
80 changes: 77 additions & 3 deletions tests/Acceptance/Extra/Workflow/TypedSearchAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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);
Expand Down

0 comments on commit 37247c5

Please sign in to comment.