Skip to content

Commit

Permalink
Fix runLocked cancellation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 13, 2025
1 parent 410ea72 commit 316bb04
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/Internal/Workflow/Process/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function start(\Closure $handler, ?ValuesInterface $values, bool $deferre
{
// Create a coroutine generator
$this->coroutine = DeferredGenerator::fromHandler($handler, $values ?? EncodedValues::empty())
// ->catch(tr(...));
->catch($this->onException(...));

$deferred
Expand Down
4 changes: 4 additions & 0 deletions src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Temporal\Client\WorkflowStubInterface;
use Temporal\DataConverter\Type;
use Temporal\DataConverter\ValuesInterface;
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Exception\OutOfContextException;
use Temporal\Internal\Support\Facade;
use Temporal\Internal\Workflow\ScopeContext;
Expand Down Expand Up @@ -958,6 +959,9 @@ public static function uuid7(?\DateTimeInterface $dateTime = null): PromiseInter
* Run a function when the mutex is released.
* The mutex is locked for the duration of the function.
*
* Note that calling the method creates a non-detached asynchronous context {@see Workflow::async()}.
* Closing the context using the `cancel()` method will reject the returned promise with a {@see CanceledFailure}.
*
* @template T
* @param Mutex $mutex Mutex name or instance.
* @param callable(): T $callable Function to run.
Expand Down
12 changes: 10 additions & 2 deletions tests/Acceptance/Extra/Workflow/MutexRunLockedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use React\Promise\PromiseInterface;
use Temporal\Client\WorkflowStubInterface;
use Temporal\DataConverter\Type;
use Temporal\Exception\Failure\CanceledFailure;
use Temporal\Tests\Acceptance\App\Attribute\Stub;
use Temporal\Tests\Acceptance\App\TestCase;
use Temporal\Workflow;
Expand All @@ -28,6 +29,7 @@ public function runLockedWithGeneratorAndAwait(
$this->assertTrue($result[0], 'Mutex must be unlocked after runLocked is finished');
$this->assertTrue($result[1], 'The function inside runLocked mist wait for signal');
$this->assertTrue($result[2], 'Mutex must be locked during runLocked');
$this->assertNull($result[3], 'No exception must be thrown');
}

#[Test]
Expand All @@ -41,6 +43,7 @@ public function runLockedAndCancel(

$this->assertTrue($result[0], 'Mutex must be unlocked after runLocked is cancelled');
$this->assertNull($result[2], 'Mutex must be locked during runLocked');
$this->assertSame(CanceledFailure::class, $result[3], 'CanceledFailure must be thrown');
}
}

Expand All @@ -64,7 +67,12 @@ public function __construct()
#[Workflow\ReturnType(Type::TYPE_ARRAY)]
public function handle(): \Generator
{
$result = yield $this->promise = Workflow::runLocked($this->mutex, $this->runLocked(...));
$exception = null;
try {
$result = yield $this->promise = Workflow::runLocked($this->mutex, $this->runLocked(...));
} catch (\Throwable $e) {
$exception = $e::class;
}

$trailed = false;
yield Workflow::await(
Expand All @@ -78,7 +86,7 @@ public function handle(): \Generator
// that was created inside the first runLocked
$trailed and throw new \Exception('The trailed runLocked must not be executed.');

return [$this->unlocked, $this->unblock, $result];
return [$this->unlocked, $this->unblock, $result, $exception];
}

#[Workflow\SignalMethod]
Expand Down

0 comments on commit 316bb04

Please sign in to comment.