Skip to content

Commit

Permalink
Add more initMethod tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 20, 2025
1 parent 70d1244 commit 26e308c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
44 changes: 44 additions & 0 deletions tests/Acceptance/App/Attribute/RetryOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\Acceptance\App\Attribute;

use Temporal\Common\RetryOptions as CommonOptions;

/**
* @see \Temporal\Tests\Acceptance\App\Feature\WorkflowStubInjector
* @see \Temporal\Common\RetryOptions
* @internal
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class RetryOptions
{
/**
* @param int<0, max> $maximumAttempts
*/
public function __construct(
public ?string $initialInterval = CommonOptions::DEFAULT_INITIAL_INTERVAL,
public float $backoffCoefficient = CommonOptions::DEFAULT_BACKOFF_COEFFICIENT,
public ?string $maximumInterval = CommonOptions::DEFAULT_MAXIMUM_INTERVAL,
public int $maximumAttempts = CommonOptions::DEFAULT_MAXIMUM_ATTEMPTS,
public array $nonRetryableExceptions = CommonOptions::DEFAULT_NON_RETRYABLE_EXCEPTIONS,
) {}

public function toRetryOptions(): CommonOptions
{
return CommonOptions::new()
->withMaximumInterval($this->maximumInterval)
->withInitialInterval($this->initialInterval)
->withBackoffCoefficient($this->backoffCoefficient)
->withMaximumAttempts($this->maximumAttempts)
->withNonRetryableExceptions($this->nonRetryableExceptions);
}
}
6 changes: 6 additions & 0 deletions tests/Acceptance/App/Attribute/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Temporal\Tests\Acceptance\App\Attribute;

use Temporal\Common\RetryOptions as CommonOptions;

/**
* An attribute to configure workflow stub.
*
Expand All @@ -12,6 +14,8 @@
#[\Attribute(\Attribute::TARGET_PARAMETER)]
final class Stub
{
public CommonOptions $retryOptions;

/**
* @param non-empty-string $type Workflow type.
* @param non-empty-string|null $workflowId
Expand All @@ -24,6 +28,8 @@ public function __construct(
public ?string $executionTimeout = null,
public array $args = [],
public array $memo = [],
RetryOptions $retryOptions = new RetryOptions(),
) {
$this->retryOptions = $retryOptions->toRetryOptions();
}
}
1 change: 1 addition & 0 deletions tests/Acceptance/App/Feature/WorkflowStubInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function createInjection(
$options = WorkflowOptions::new()
->withWorkflowExecutionTimeout($attribute->executionTimeout ?? '1 minute')
->withTaskQueue($feature->taskQueue)
->withRetryOptions($attribute->retryOptions)
->withEagerStart($attribute->eagerStart);

$attribute->workflowId === null or $options = $options
Expand Down
75 changes: 71 additions & 4 deletions tests/Acceptance/Extra/Workflow/InitMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\Test;
use Temporal\Client\WorkflowStubInterface;
use Temporal\Exception\Client\WorkflowFailedException;
use Temporal\Exception\Failure\TimeoutFailure;
use Temporal\Tests\Acceptance\App\Attribute\RetryOptions;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
use Temporal\Workflow\InitMethod;
use Temporal\Workflow\WorkflowInterface;
use Temporal\Workflow\WorkflowMethod;
Expand All @@ -18,13 +20,41 @@
class InitMethodTest extends TestCase
{
#[Test]
public function updateHandlersWithOneCall(
public function simpleCase(
#[Stub(
type: 'Extra_Workflow_InitMethod',
args: [new Input('John Doe', 30)],
)] WorkflowStubInterface $stub,
): void {
$this->assertTrue($stub->getResult(timeout: 5));
$this->assertTrue($stub->getResult());
}

#[Test]
public function emptyConstructor(
#[Stub(
type: 'Extra_Workflow_InitMethod__empty_constructor',
args: [new Input('John Doe', 30)],
)] WorkflowStubInterface $stub,
): void {
$this->assertTrue($stub->getResult());
}

#[Test]
public function differentConstructorParams(
#[Stub(
type: 'Extra_Workflow_InitMethod__different_constructor_params',
executionTimeout: '2 seconds',
args: [new Input('John Doe', 30)],
retryOptions: new RetryOptions(
maximumAttempts: 1,
),
)] WorkflowStubInterface $stub,
): void {
try {
$stub->getResult();
} catch (WorkflowFailedException $failure) {
self:self::assertInstanceOf(TimeoutFailure::class, $failure->getPrevious());
}
}
}

Expand All @@ -34,7 +64,8 @@ class TestWorkflow
private array $initInput;

#[InitMethod]
public function __construct(Input $input) {
public function __construct(Input $input)
{
$this->initInput = \func_get_args();
}

Expand All @@ -45,6 +76,42 @@ public function handle(Input $input)
}
}

#[WorkflowInterface]
class TestWorkflowEmptyConstructor
{
private array $initInput;

#[InitMethod]
public function __construct()
{
$this->initInput = \func_get_args();
}

#[WorkflowMethod(name: "Extra_Workflow_InitMethod__empty_constructor")]
public function handle(Input $input)
{
return $this->initInput === \func_get_args();
}
}

#[WorkflowInterface]
class TestWorkflowDifferentConstructorParams
{
private array $initInput;

#[InitMethod]
public function __construct(\stdClass $input)
{
$this->initInput = \func_get_args();
}

#[WorkflowMethod(name: "Extra_Workflow_InitMethod__different_constructor_params")]
public function handle(Input $input)
{
return $this->initInput === \func_get_args();
}
}

class Input
{
public function __construct(
Expand Down

0 comments on commit 26e308c

Please sign in to comment.