From 89a1b0b7e57e3c65d7013bbc850f7c416f477be0 Mon Sep 17 00:00:00 2001 From: Aleksei Gagarin Date: Mon, 15 Jan 2024 23:12:45 +0400 Subject: [PATCH] Don't set Workflow Start Delay if it wasn't specified (#388) --- .github/workflows/code-style.yml | 2 +- src/Internal/Client/WorkflowStarter.php | 6 +- .../Client/WorkflowStarterTestCase.php | 111 ++++++++++++++++++ 3 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/Internal/Client/WorkflowStarterTestCase.php diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index b1600b38..94972a61 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -10,7 +10,7 @@ jobs: strategy: fail-fast: false matrix: - php: [8.2] + php: [8.3] os: [ubuntu-latest] steps: - name: Set up PHP ${{ matrix.php }} diff --git a/src/Internal/Client/WorkflowStarter.php b/src/Internal/Client/WorkflowStarter.php index a5da4a58..99232558 100644 --- a/src/Internal/Client/WorkflowStarter.php +++ b/src/Internal/Client/WorkflowStarter.php @@ -191,7 +191,6 @@ private function configureExecutionRequest( ->setTaskQueue(new TaskQueue(['name' => $options->taskQueue])) ->setWorkflowType(new WorkflowType(['name' => $input->workflowType])) ->setWorkflowId($input->workflowId) - ->setWorkflowStartDelay(DateInterval::toDuration($options->workflowStartDelay)) ->setCronSchedule($options->cronSchedule ?? '') ->setRetryPolicy($options->retryOptions ? $options->retryOptions->toWorkflowRetryPolicy() : null) ->setWorkflowIdReusePolicy($options->workflowIdReusePolicy) @@ -202,6 +201,11 @@ private function configureExecutionRequest( ->setSearchAttributes($options->toSearchAttributes($this->converter)) ->setHeader($header->toHeader()); + $delay = DateInterval::toDuration($options->workflowStartDelay); + if ($delay !== null && ($delay->getSeconds() > 0 || $delay->getNanos() > 0)) { + $req->setWorkflowStartDelay($delay); + } + if ($req instanceof StartWorkflowExecutionRequest) { $req->setRequestEagerExecution($options->eagerStart); } diff --git a/tests/Unit/Internal/Client/WorkflowStarterTestCase.php b/tests/Unit/Internal/Client/WorkflowStarterTestCase.php new file mode 100644 index 00000000..734bdcd9 --- /dev/null +++ b/tests/Unit/Internal/Client/WorkflowStarterTestCase.php @@ -0,0 +1,111 @@ +startRequest('test-workflow', $options); + + self::assertNull($request->getWorkflowStartDelay()); + } + + public function testDelayIsNullIfEmpty(): void + { + $options = (new WorkflowOptions())->withWorkflowStartDelay(0); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNull($request->getWorkflowStartDelay()); + } + + public function testDelayIfSpecifiedSeconds(): void + { + $options = (new WorkflowOptions())->withWorkflowStartDelay(10); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNotNull($request->getWorkflowStartDelay()); + self::assertSame(10, $request->getWorkflowStartDelay()->getSeconds()); + self::assertSame(0, $request->getWorkflowStartDelay()->getNanos()); + } + + public function testDelayIfSpecifiedNanos(): void + { + $options = (new WorkflowOptions()) + ->withWorkflowStartDelay(DateInterval::parse(42, DateInterval::FORMAT_MICROSECONDS)); + + $request = $this->startRequest('test-workflow', $options); + + self::assertNotNull($request->getWorkflowStartDelay()); + self::assertSame(0, $request->getWorkflowStartDelay()->getSeconds()); + self::assertSame(42000, $request->getWorkflowStartDelay()->getNanos()); + } + + private function startRequest( + string $workflowType, + WorkflowOptions $options, + array $args = [], + ): StartWorkflowExecutionRequest { + $clientOptions = (new \Temporal\Client\ClientOptions()) + ->withNamespace(self::NAMESPACE) + ->withIdentity(self::IDENTITY); + + $result = null; + $clientMock = $this->createMock(ServiceClientInterface::class); + $clientMock + ->expects($this->once()) + ->method('StartWorkflowExecution') + ->with( + $this->callback(static function (StartWorkflowExecutionRequest $request) use (&$result) { + $result = $request; + return true; + }) + )->willReturn( + (new StartWorkflowExecutionResponse()) + ->setRunId(self::WORKFLOW_RUN_ID) + ); + + $starter = new WorkflowStarter( + serviceClient: $clientMock, + converter: DataConverter::createDefault(), + clientOptions: $clientOptions, + interceptors: Pipeline::prepare([]), + ); + + $execution = $starter->start($workflowType, $options, $args); + + if (self::WORKFLOW_RUN_ID !== $execution->getRunID()) { + $this->fail('Unexpected workflow run ID.'); + } + + return $result; + } +}