Skip to content

Commit

Permalink
Add test with single promise inside Workflow::await
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 16, 2022
1 parent cb78de2 commit 9ee9beb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Internal/Workflow/WorkflowContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ public function await(...$conditions): PromiseInterface
}
}

// if (\count($result) === 1) {
// return $result[0];
// }
if (\count($result) === 1) {
return $result[0];
}

return Promise::any($result)->then(
function ($result) use ($conditionGroupId) {
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/src/Workflow/AwaitWithSingleTimeoutWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* This file is part of Temporal package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Temporal\Tests\Workflow;

use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;

#[Workflow\WorkflowInterface]
class AwaitWithSingleTimeoutWorkflow
{
#[WorkflowMethod()]
public function handler()
{
yield Workflow::await(Workflow::timer(5000));

return 'ok';
}
}
48 changes: 42 additions & 6 deletions tests/Functional/WorkflowTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ public function testBatchedSignal_Combined()

/**
* Destroy workflow with a started awaitWithTimeout promise inside.
* @see \Temporal\Tests\Workflow\AwaitWithTimeoutWorkflow
*/
public function testAwaitWithTimeout()
public function testAwaitWithTimeout(): void
{
$worker = WorkerMock::createMock();

Expand All @@ -250,8 +251,9 @@ public function testAwaitWithTimeout()
* Destroy 100 workflows with a started awaitWithTimeout promise inside.
* The promise will be annihilated on the workflow destroy.
* There mustn't be any leaks.
* @see \Temporal\Tests\Workflow\AwaitWithTimeoutWorkflow
*/
public function testAwaitWithTimeout_Leaks()
public function testAwaitWithTimeout_Leaks(): void
{
$worker = WorkerMock::createMock();

Expand All @@ -261,7 +263,9 @@ public function testAwaitWithTimeout_Leaks()
$uuid2 = Uuid::v4();
$log = <<<LOG
[0m [{"id":1,"command":"StartWorkflow","options":{"info":{"WorkflowExecution":{"ID":"$uuid1","RunID":"$uuid2"},"WorkflowType":{"Name":"AwaitWithTimeoutWorkflow"},"TaskQueueName":"default","WorkflowExecutionTimeout":315360000000000000,"WorkflowRunTimeout":315360000000000000,"WorkflowTaskTimeout":0,"Namespace":"default","Attempt":1,"CronSchedule":"","ContinuedExecutionRunID":"","ParentWorkflowNamespace":"","ParentWorkflowExecution":null,"Memo":null,"SearchAttributes":null,"BinaryChecksum":"4301710877bf4b107429ee12de0922be"}},"payloads":"CicKFgoIZW5jb2RpbmcSCmpzb24vcGxhaW4SDSJIZWxsbyBXb3JsZCI="}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:52.2672785Z"}
# Run a timers
[0m [{"id":$id,"command":"NewTimer","options":{"ms":999000},"payloads":""},{"id":1,"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"receive": true}
# Destroy workflow
[0m [{"id":2,"command":"DestroyWorkflow","options":{"runId":"$uuid2"}}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:53.3838443Z","replay":true}
[0m [{"id":2,"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"receive": true}
LOG;
Expand All @@ -278,16 +282,17 @@ public function testAwaitWithTimeout_Leaks()
* Destroy 100 workflows with started few awaitWithTimeout promises inside.
* The promises will be annihilated on the workflow destroy.
* There mustn't be any leaks.
* @see \Temporal\Tests\Workflow\AwaitWithTimeoutWorkflow
*/
public function testAwaitWithFewParallelTimeouts_Leaks()
public function testAwaitWithFewParallelTimeouts_Leaks(): void
{
$worker = WorkerMock::createMock();

// Run the workflow $i times
for ($id = 9001, $i = 0; $i < 100; ++$i, ++$id) {
for ($id = 9000, $i = 0; $i < 100; ++$i) {
$uuid1 = Uuid::v4();
$uuid2 = Uuid::v4();
$id1 = $id;
$id1 = ++$id;
$id2 = ++$id;
$id3 = ++$id;
$id4 = ++$id;
Expand All @@ -297,7 +302,38 @@ public function testAwaitWithFewParallelTimeouts_Leaks()
[0m [{"id":$id1}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:53.3204026Z"}
# Run three async timers
[0m [{"id":{$id2},"command":"NewTimer","options":{"ms":500000},"payloads":""},{"id":$id3,"command":"NewTimer","options":{"ms":120000},"payloads":""},{"id":$id4,"command":"NewTimer","options":{"ms":20000},"payloads":""}] {"receive": true}
# Destroy worker
# Destroy workflow
[0m [{"id":2,"command":"DestroyWorkflow","options":{"runId":"$uuid2"}}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:53.3838443Z","replay":true}
[0m [{"id":2,"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"receive": true}
LOG;

$worker->run($this, Splitter::createFromString($log)->getQueue());
$before ??= \memory_get_usage();
}
$after = \memory_get_usage();

$this->assertSame(0, $after - $before);
}

/**
* Destroy 100 workflows with single promise inside Workflow::await.
* That case mustn't leak.
* @see \Temporal\Tests\Workflow\AwaitWithSingleTimeoutWorkflow
*/
public function testAwaitWithOneTimer_Leaks()
{
$worker = WorkerMock::createMock();

// Run the workflow $i times
for ($id = 9000, $i = 0; $i < 100; ++$i) {
$uuid1 = Uuid::v4();
$uuid2 = Uuid::v4();
$id1 = ++$id;
$log = <<<LOG
[0m [{"id":1,"command":"StartWorkflow","options":{"info":{"WorkflowExecution":{"ID":"$uuid1","RunID":"$uuid2"},"WorkflowType":{"Name":"AwaitWithSingleTimeoutWorkflow"},"TaskQueueName":"default","WorkflowExecutionTimeout":315360000000000000,"WorkflowRunTimeout":315360000000000000,"WorkflowTaskTimeout":0,"Namespace":"default","Attempt":1,"CronSchedule":"","ContinuedExecutionRunID":"","ParentWorkflowNamespace":"","ParentWorkflowExecution":null,"Memo":null,"SearchAttributes":null,"BinaryChecksum":"4301710877bf4b107429ee12de0922be"}},"payloads":"CicKFgoIZW5jb2RpbmcSCmpzb24vcGxhaW4SDSJIZWxsbyBXb3JsZCI="}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:52.2672785Z"}
# Run a timer
[0m [{"id":$id1,"command":"NewTimer","options":{"ms":5000000},"payloads":""},{"id":1,"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"receive": true}
# Destroy workflow
[0m [{"id":2,"command":"DestroyWorkflow","options":{"runId":"$uuid2"}}] {"taskQueue":"default","tickTime":"2021-01-12T15:21:53.3838443Z","replay":true}
[0m [{"id":2,"payloads":"ChkKFwoIZW5jb2RpbmcSC2JpbmFyeS9udWxs"}] {"receive": true}
LOG;
Expand Down

0 comments on commit 9ee9beb

Please sign in to comment.