Skip to content

Commit

Permalink
Merge pull request #359: Add tests
Browse files Browse the repository at this point in the history
Add test case where workflow fails with react/promise v3
  • Loading branch information
roxblnfk authored Oct 4, 2023
2 parents 3320412 + 20b0212 commit e5b375f
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/Fixtures/src/Workflow/CancelSignaledChildWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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 React\Promise\Deferred;
use Temporal\Workflow;
use Temporal\Workflow\WorkflowMethod;

#[Workflow\WorkflowInterface]
class CancelSignaledChildWorkflow
{
private array $status = [];

#[Workflow\QueryMethod(name: 'getStatus')]
public function getStatus(): array
{
return $this->status;
}

#[WorkflowMethod(name: 'CancelSignaledChildWorkflow')]
public function handler()
{
// typed stub
$simple = Workflow::newChildWorkflowStub(SimpleSignaledWorkflow::class);

$waitSignaled = new Deferred();

$this->status[] = 'start';

// start execution
$scope = Workflow::async(
function () use ($simple, $waitSignaled) {
$call = $simple->handler();
$this->status[] = 'child started';

yield $simple->add(8);
$this->status[] = 'child signaled';
$waitSignaled->resolve();

return yield $call;
}
);

// only cancel scope when signal dispatched
yield $waitSignaled;
$scope->cancel();
$this->status[] = 'scope canceled';

try {
return yield $scope;
} catch (\Throwable $e) {
$this->status[] = 'process done';

return 'canceled ok';
}
}
}
37 changes: 37 additions & 0 deletions tests/Fixtures/src/Workflow/SimpleSignaledWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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 SimpleSignaledWorkflow
{
private $counter = 0;

#[Workflow\SignalMethod(name: "add")]
public function add(
int $value
) {
$this->counter += $value;
}

#[WorkflowMethod(name: 'SimpleSignaledWorkflow')]
public function handler(): iterable
{
// collect signals during one second
yield Workflow::timer(1);

return $this->counter;
}
}
21 changes: 21 additions & 0 deletions tests/Functional/Client/TypedStubTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Temporal\Tests\Functional\Client;

use Temporal\Exception\Client\WorkflowQueryException;
use Temporal\Exception\InvalidArgumentException;
use Temporal\Tests\DTO\Message;
use Temporal\Tests\DTO\User;
Expand Down Expand Up @@ -90,6 +91,26 @@ public function testQueryWorkflow()
$this->assertSame(88, $e->getResult());
}

public function testQueryNotExistingMethod()
{
$client = $this->createClient();
$simple = $client->newUntypedWorkflowStub('QueryWorkflow');


$e = $client->start($simple, 'Hello World');
$this->assertNotEmpty($e->getExecution()->getID());
$this->assertNotEmpty($e->getExecution()->getRunID());

try {
$simple->query('error', -1);
} catch (WorkflowQueryException $e) {
$this->assertStringContainsString('KnownQueryTypes=[get]', $e->getPrevious()->getMessage());
return;
}

$this->fail('Expected exception to be thrown');
}

public function testGetDTOResult()
{
$w = $this->createClient();
Expand Down
39 changes: 39 additions & 0 deletions tests/Functional/SimpleWorkflowTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Temporal\Tests\Functional;

use Temporal\Api\Enums\V1\EventType;
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\WorkflowClient;
use Temporal\Client\WorkflowOptions;
use Temporal\Testing\ActivityMocker;
use Temporal\Tests\TestCase;
use Temporal\Tests\Workflow\SimpleWorkflow;
use Temporal\Workflow\WorkflowExecution;

final class SimpleWorkflowTestCase extends TestCase
{
Expand Down Expand Up @@ -47,4 +49,41 @@ public function testEagerStartDoesntFail(): void
$run = $this->workflowClient->start($workflow, 'hello');
$this->assertSame('HELLO', $run->getResult('string'));
}

public function testCancelSignaledChildWorkflow(): void
{
$workflow = $this->workflowClient
->newWorkflowStub(\Temporal\Tests\Workflow\CancelSignaledChildWorkflow::class);
$run = $this->workflowClient->start($workflow);

$this->assertSame('canceled ok', $run->getResult('string', 10));

$status = $workflow->getStatus();
$this->assertSame([
"start",
"child started",
"child signaled",
"scope canceled",
"process done",
], $status);

$this->assertContainsEvent(
$run->getExecution(),
EventType::EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED,
);
}

private function assertContainsEvent(WorkflowExecution $execution, int $event): void
{
$history = $this->workflowClient->getWorkflowHistory(
$execution,
);
foreach ($history as $item) {
if ($item->getEventType() === $event) {
return;
}
}

throw new \Exception(\sprintf('Event %s not found', EventType::value($event)));
}
}

0 comments on commit e5b375f

Please sign in to comment.