From 61a9559856602b5bf242b9273c2a633f486762fe Mon Sep 17 00:00:00 2001 From: Kajetan Date: Mon, 3 Oct 2022 22:14:16 +0200 Subject: [PATCH] feat: upsert search attributes (#248) * Upsert search attributes * Search attributes improvements * Linter * Fix tests bootstrap Co-authored-by: Sergey Zhuk --- .../Request/UpsertSearchAttributes.php | 20 +++++++++++ src/Internal/Workflow/ScopeContext.php | 11 ++++++ src/Internal/Workflow/WorkflowContext.php | 11 ++++++ src/Workflow.php | 12 +++++++ src/Workflow/WorkflowContextInterface.php | 5 +++ .../UpsertSearchAttributesWorkflow.php | 34 +++++++++++++++++++ ...UpsertSearchAttributesWorkflowTestCase.php | 25 ++++++++++++++ tests/SearchAttributeTestInvoker.php | 32 +++++++++++++++++ tests/bootstrap.php | 5 ++- 9 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/Internal/Transport/Request/UpsertSearchAttributes.php create mode 100644 tests/Fixtures/src/Workflow/UpsertSearchAttributesWorkflow.php create mode 100644 tests/Functional/Client/UpsertSearchAttributesWorkflowTestCase.php create mode 100644 tests/SearchAttributeTestInvoker.php diff --git a/src/Internal/Transport/Request/UpsertSearchAttributes.php b/src/Internal/Transport/Request/UpsertSearchAttributes.php new file mode 100644 index 00000000..768d2aae --- /dev/null +++ b/src/Internal/Transport/Request/UpsertSearchAttributes.php @@ -0,0 +1,20 @@ + $searchAttributes + */ + public function __construct(array $searchAttributes) + { + parent::__construct(self::NAME, ['searchAttributes' => $searchAttributes]); + } +} diff --git a/src/Internal/Workflow/ScopeContext.php b/src/Internal/Workflow/ScopeContext.php index b8b1e415..fde43285 100644 --- a/src/Internal/Workflow/ScopeContext.php +++ b/src/Internal/Workflow/ScopeContext.php @@ -22,6 +22,7 @@ use Temporal\Workflow\CancellationScopeInterface; use Temporal\Workflow\ScopedContextInterface; use Temporal\Workflow\WorkflowContextInterface; +use Temporal\Internal\Transport\Request\UpsertSearchAttributes; class ScopeContext extends WorkflowContext implements ScopedContextInterface { @@ -163,4 +164,14 @@ public function timer($interval): PromiseInterface return $result; } + + /** + * {@inheritDoc} + */ + public function upsertSearchAttributes(array $searchAttributes): void + { + $this->request( + new UpsertSearchAttributes($searchAttributes) + ); + } } diff --git a/src/Internal/Workflow/WorkflowContext.php b/src/Internal/Workflow/WorkflowContext.php index 15888156..454c69cf 100644 --- a/src/Internal/Workflow/WorkflowContext.php +++ b/src/Internal/Workflow/WorkflowContext.php @@ -45,6 +45,7 @@ use Temporal\Workflow\WorkflowContextInterface; use Temporal\Workflow\WorkflowExecution; use Temporal\Workflow\WorkflowInfo; +use Temporal\Internal\Transport\Request\UpsertSearchAttributes; use function React\Promise\reject; use function React\Promise\resolve; @@ -418,6 +419,16 @@ public function getStackTrace(): string return StackRenderer::renderTrace($this->trace); } + /** + * {@inheritDoc} + */ + public function upsertSearchAttributes(array $searchAttributes): void + { + $this->services->client->request( + new UpsertSearchAttributes($searchAttributes) + ); + } + /** * {@inheritDoc} */ diff --git a/src/Workflow.php b/src/Workflow.php index 424d149a..a2497a69 100644 --- a/src/Workflow.php +++ b/src/Workflow.php @@ -928,4 +928,16 @@ public static function getStackTrace(): string return $context->getStackTrace(); } + + /** + * Upsert search attributes + * + * @param array $searchAttributes + */ + public static function upsertSearchAttributes(array $searchAttributes): void + { + /** @var ScopedContextInterface $context */ + $context = self::getCurrentContext(); + $context->upsertSearchAttributes($searchAttributes); + } } diff --git a/src/Workflow/WorkflowContextInterface.php b/src/Workflow/WorkflowContextInterface.php index 6be12d8a..66b41f66 100644 --- a/src/Workflow/WorkflowContextInterface.php +++ b/src/Workflow/WorkflowContextInterface.php @@ -274,4 +274,9 @@ public function awaitWithTimeout($interval, ...$conditions): PromiseInterface; * @return string */ public function getStackTrace(): string; + + /** + * @param array $searchAttributes + */ + public function upsertSearchAttributes(array $searchAttributes): void; } diff --git a/tests/Fixtures/src/Workflow/UpsertSearchAttributesWorkflow.php b/tests/Fixtures/src/Workflow/UpsertSearchAttributesWorkflow.php new file mode 100644 index 00000000..f046ff6e --- /dev/null +++ b/tests/Fixtures/src/Workflow/UpsertSearchAttributesWorkflow.php @@ -0,0 +1,34 @@ + 'attr1-value', + 'attr2' => true, + ] + ); + + return 'done'; + } +} diff --git a/tests/Functional/Client/UpsertSearchAttributesWorkflowTestCase.php b/tests/Functional/Client/UpsertSearchAttributesWorkflowTestCase.php new file mode 100644 index 00000000..0d420455 --- /dev/null +++ b/tests/Functional/Client/UpsertSearchAttributesWorkflowTestCase.php @@ -0,0 +1,25 @@ +createClient(); + $workflow = $client->newUntypedWorkflowStub('UpsertSearchAttributesWorkflow'); + + $e = $client->start($workflow); + + $this->assertNotEmpty($e->getExecution()->getID()); + $this->assertNotEmpty($e->getExecution()->getRunID()); + + $this->assertSame('done', $workflow->getResult()); + } +} diff --git a/tests/SearchAttributeTestInvoker.php b/tests/SearchAttributeTestInvoker.php new file mode 100644 index 00000000..62139870 --- /dev/null +++ b/tests/SearchAttributeTestInvoker.php @@ -0,0 +1,32 @@ + ChannelCredentials::createInsecure()] + ); + $result = $operation->AddSearchAttributes( + new AddSearchAttributesRequest( + [ + 'search_attributes' => [ + 'attr1' => 2, // Keyword + 'attr2' => 5, // Bool + ] + ] + ) + ); + + $result->getMetadata(); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 60d50dfc..36bd7035 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,12 +3,15 @@ declare(strict_types=1); use Temporal\Testing\Environment; +use Temporal\Tests\SearchAttributeTestInvoker; require __DIR__ . '/../vendor/autoload.php'; if (getenv('RUN_TEMPORAL_TEST_SERVER') !== false) { $environment = Environment::create(); - $environment->start('./rr serve -c .rr.silent.yaml -w tests'); + $environment->startTemporalTestServer(); + (new SearchAttributeTestInvoker)(); + $environment->startRoadRunner('./rr serve -c .rr.silent.yaml -w tests'); register_shutdown_function(fn() => $environment->stop()); }