Skip to content

Commit fc67190

Browse files
authored
Merge pull request #551: activate Acceptance/Extra tests, fix bugs
2 parents 34e49ca + 609fad4 commit fc67190

File tree

13 files changed

+125
-45
lines changed

13 files changed

+125
-45
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
>
1919
<testsuites>
2020
<testsuite name="Acceptance">
21+
<directory suffix="Test.php">tests/Acceptance/Extra</directory>
2122
<directory suffix="Test.php">tests/Acceptance/Harness</directory>
2223
</testsuite>
2324
<testsuite name="Arch">

src/Internal/Declaration/Dispatcher/Dispatcher.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ private function createExecutorFromFunction(\ReflectionFunction $fun): \Closure
120120
throw new \BadMethodCallException($message, $type, $error);
121121
});
122122

123-
return $closure->call($ctx, ...$arguments);
123+
124+
return $fun->isStatic()
125+
? $closure->bindTo(null, $ctx::class)?->__invoke(...$arguments) ?? $closure(...$arguments)
126+
: $closure->call($ctx, ...$arguments);
124127
} finally {
125128
\restore_error_handler();
126129
}

src/Internal/Workflow/Process/DeferredGenerator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function send(mixed $value): mixed
8989
*/
9090
public function getReturn(): mixed
9191
{
92-
// $this->start();
92+
$this->finished or throw new \LogicException('Cannot get return value of a generator that was not finished.');
9393
try {
9494
return $this->generator->getReturn();
9595
} catch (\Throwable $e) {
@@ -149,7 +149,8 @@ public function valid(): bool
149149
{
150150
$this->start();
151151
try {
152-
return $this->generator->valid();
152+
$result = $this->generator->valid() or $this->finished = true;
153+
return $result;
153154
} catch (\Throwable $e) {
154155
$this->handleException($e);
155156
}
@@ -218,9 +219,9 @@ private function handleException(\Throwable $e): never
218219
{
219220
$this->finished and throw $e;
220221
$this->finished = true;
221-
foreach ($this->catchers as $catch) {
222+
foreach ($this->catchers as $catcher) {
222223
try {
223-
$catch($e);
224+
$catcher($e);
224225
} catch (\Throwable) {
225226
// Do nothing.
226227
}

src/Internal/Workflow/Process/Scope.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ public function startSignal(callable $handler, ValuesInterface $values, string $
186186
*/
187187
public function attach(\Generator $generator): self
188188
{
189-
$this->coroutine = DeferredGenerator::fromGenerator($generator);
189+
$this->coroutine = DeferredGenerator::fromGenerator($generator)
190+
->catch($this->onException(...));
190191

191192
$this->next();
192193
return $this;
@@ -386,13 +387,13 @@ protected function next(): void
386387
begin:
387388
$this->context->resolveConditions();
388389

389-
if (!$this->coroutine->valid()) {
390-
try {
390+
try {
391+
if (!$this->coroutine->valid()) {
391392
$this->onResult($this->coroutine->getReturn());
392-
} catch (\Throwable) {
393-
$this->onResult(null);
393+
return;
394394
}
395-
395+
} catch (\Throwable) {
396+
$this->onResult(null);
396397
return;
397398
}
398399

@@ -418,11 +419,7 @@ protected function next(): void
418419
break;
419420

420421
case $current instanceof \Generator:
421-
try {
422-
$this->nextPromise($this->createScope(false)->attach($current));
423-
} catch (\Throwable $e) {
424-
$this->coroutine->throw($e);
425-
}
422+
$this->nextPromise($this->createScope(false)->attach($current));
426423
break;
427424

428425
default:

src/Workflow.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Temporal\Client\WorkflowStubInterface;
1919
use Temporal\DataConverter\Type;
2020
use Temporal\DataConverter\ValuesInterface;
21+
use Temporal\Exception\Failure\CanceledFailure;
2122
use Temporal\Exception\OutOfContextException;
2223
use Temporal\Internal\Support\Facade;
2324
use Temporal\Internal\Workflow\ScopeContext;
@@ -958,6 +959,9 @@ public static function uuid7(?\DateTimeInterface $dateTime = null): PromiseInter
958959
* Run a function when the mutex is released.
959960
* The mutex is locked for the duration of the function.
960961
*
962+
* Note that calling the method creates a non-detached asynchronous context {@see Workflow::async()}.
963+
* Closing the context using the `cancel()` method will reject the returned promise with a {@see CanceledFailure}.
964+
*
961965
* @template T
962966
* @param Mutex $mutex Mutex name or instance.
963967
* @param callable(): T $callable Function to run.

tests/Acceptance/Extra/Schedule/ScheduleClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public function listSchedulesWithQuery(
4040
);
4141
}
4242

43+
// Wait for schedules to be created
44+
$deadline = \microtime(true) + 5;
45+
check:
46+
$paginator = $client->listSchedules(
47+
pageSize: 10,
48+
query: 'bar = 4242'
49+
);
50+
if (\count($paginator->getPageItems()) < 6 && \microtime(true) < $deadline) {
51+
goto check;
52+
}
53+
4354
try {
4455
$paginator = $client->listSchedules(
4556
pageSize: 5,

tests/Acceptance/Extra/Update/UntypedStubTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Temporal\Client\WorkflowClientInterface;
1010
use Temporal\Client\WorkflowStubInterface;
1111
use Temporal\Exception\Client\TimeoutException;
12-
use Temporal\Exception\Client\UntypedStubException;
12+
use Temporal\Exception\Client\WorkflowUpdateException;
1313
use Temporal\Internal\Support\DateInterval;
1414
use Temporal\Tests\Acceptance\App\Attribute\Stub;
1515
use Temporal\Tests\Acceptance\App\TestCase;
@@ -37,7 +37,11 @@ public function fetchResolvedResultAfterWorkflowCompleted(
3737

3838
$this->assertSame(['key' => 'resolved'], (array)$result, 'Workflow result contains resolved value');
3939
$this->assertFalse($handle->hasResult());
40-
$this->assertFalse($resolver->hasResult(), 'Resolver should not have result because of wait policy');
40+
41+
// Since Temporal CLI 1.2.0, the result is available immediately after the operation
42+
$this->assertTrue($resolver->hasResult());
43+
$this->assertSame('resolved', $resolver->getResult());
44+
4145
// Fetch result
4246
$this->assertSame('resolved', $handle->getResult());
4347
$this->assertTrue($handle->hasResult());
@@ -88,7 +92,7 @@ public function handleUnknownUpdate(
8892
try {
8993
$stub->startUpdate('unknownUpdateMethod', '42');
9094
$this->fail('Should throw exception');
91-
} catch (UntypedStubException $e) {
95+
} catch (WorkflowUpdateException $e) {
9296
$this->assertStringContainsString(
9397
'unknown update method unknownUpdateMethod',
9498
$e->getPrevious()->getMessage(),

tests/Acceptance/Extra/Update/UpdateWithStartTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function failWithBadUpdateName(
6060
try {
6161
$stub->getResult(timeout: 1);
6262
$this->fail('Workflow must fail');
63-
} catch (WorkflowFailedException $e) {
63+
} catch (WorkflowFailedException) {
6464
$this->assertTrue(true);
6565
}
6666
}

tests/Acceptance/Extra/Workflow/AllHandlersFinishedTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public function updateHandlersWithOneCall(
3333

3434
$this->assertSame(['key' => 'resolved'], (array) $result, 'Workflow result contains resolved value');
3535
$this->assertFalse($handle->hasResult());
36-
$this->assertFalse($resolver->hasResult(), 'Resolver should not have result because of wait policy');
36+
37+
// Since Temporal CLI 1.2.0, the result is available immediately after the operation
38+
$this->assertTrue($resolver->hasResult());
39+
$this->assertSame('resolved', $resolver->getResult());
40+
3741
// Fetch signal's result
3842
$this->assertSame('resolved', $handle->getResult());
3943
$this->assertTrue($handle->hasResult());

tests/Acceptance/Extra/Workflow/MutexRunLockedTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use React\Promise\PromiseInterface;
99
use Temporal\Client\WorkflowStubInterface;
1010
use Temporal\DataConverter\Type;
11+
use Temporal\Exception\Failure\CanceledFailure;
1112
use Temporal\Tests\Acceptance\App\Attribute\Stub;
1213
use Temporal\Tests\Acceptance\App\TestCase;
1314
use Temporal\Workflow;
@@ -28,6 +29,7 @@ public function runLockedWithGeneratorAndAwait(
2829
$this->assertTrue($result[0], 'Mutex must be unlocked after runLocked is finished');
2930
$this->assertTrue($result[1], 'The function inside runLocked mist wait for signal');
3031
$this->assertTrue($result[2], 'Mutex must be locked during runLocked');
32+
$this->assertNull($result[3], 'No exception must be thrown');
3133
}
3234

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

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

@@ -64,7 +67,12 @@ public function __construct()
6467
#[Workflow\ReturnType(Type::TYPE_ARRAY)]
6568
public function handle(): \Generator
6669
{
67-
$result = yield $this->promise = Workflow::runLocked($this->mutex, $this->runLocked(...));
70+
$exception = null;
71+
try {
72+
$result = yield $this->promise = Workflow::runLocked($this->mutex, $this->runLocked(...));
73+
} catch (\Throwable $e) {
74+
$exception = $e::class;
75+
}
6876

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

81-
return [$this->unlocked, $this->unblock, $result];
89+
return [$this->unlocked, $this->unblock, $result, $exception];
8290
}
8391

8492
#[Workflow\SignalMethod]

0 commit comments

Comments
 (0)