Skip to content

Commit 3fb450b

Browse files
committed
More tests
1 parent e82da4c commit 3fb450b

File tree

3 files changed

+73
-25
lines changed

3 files changed

+73
-25
lines changed

src/Internal/Workflow/Process/DeferredGenerator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function send(mixed $value): mixed
8989
*/
9090
public function getReturn(): mixed
9191
{
92+
// $this->start();
9293
try {
9394
return $this->generator->getReturn();
9495
} catch (\Throwable $e) {
@@ -170,6 +171,20 @@ public function catch(callable $handler): self
170171
return $this;
171172
}
172173

174+
private static function getDummyGenerator(): \Generator
175+
{
176+
static $generator;
177+
178+
if ($generator === null) {
179+
$generator = (static function (): \Generator {
180+
yield;
181+
})();
182+
$generator->current();
183+
}
184+
185+
return $generator;
186+
}
187+
173188
private function start(): void
174189
{
175190
if ($this->started) {
@@ -185,12 +200,14 @@ private function start(): void
185200
return;
186201
}
187202

188-
$this->generator = (static function (mixed $result) {
203+
/** @psalm-suppress all */
204+
$this->generator = (static function (mixed $result): \Generator {
189205
return $result;
190206
yield;
191207
})($result);
192208
$this->finished = true;
193209
} catch (\Throwable $e) {
210+
$this->generator = self::getDummyGenerator();
194211
$this->handleException($e);
195212
} finally {
196213
unset($this->handler, $this->values);

src/Internal/Workflow/Process/Scope.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ public function getContext(): WorkflowContext
131131
public function start(\Closure $handler, ?ValuesInterface $values, bool $deferred): void
132132
{
133133
// Create a coroutine generator
134-
$this->coroutine = $this->call($handler, $values ?? EncodedValues::empty());
134+
$this->coroutine = DeferredGenerator::fromHandler($handler, $values ?? EncodedValues::empty())
135+
->catch($this->onException(...));
135136

136137
$deferred
137138
? $this->services->loop->once($this->layer, $this->next(...))
@@ -330,19 +331,6 @@ function () use ($cancelID): void {
330331
return $scope;
331332
}
332333

333-
/**
334-
* @param \Closure(ValuesInterface): mixed $handler
335-
*/
336-
protected function call(\Closure $handler, ValuesInterface $values): DeferredGenerator
337-
{
338-
$generator = DeferredGenerator::fromHandler($handler, $values)
339-
->catch(tr(...))
340-
->catch($this->onException(...));
341-
// Run lazy generator
342-
$generator->current();
343-
return $generator;
344-
}
345-
346334
/**
347335
* Call a Signal or Update method. In this case deserialization errors are skipped.
348336
*
@@ -441,7 +429,11 @@ protected function next(): void
441429
break;
442430

443431
default:
444-
$this->coroutine->send($current);
432+
try {
433+
$this->coroutine->send($current);
434+
} catch (\Throwable) {
435+
// Ignore
436+
}
445437
goto begin;
446438
}
447439
}

tests/Unit/Workflow/DeferredGeneratorTestCase.php

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function testSimple(): void
3232
);
3333
}
3434

35-
public function testSendingValues(): void
35+
public function testCompareSendingValues(): void
3636
{
3737
$this->compare(
3838
fn() => (function () {
@@ -50,7 +50,7 @@ public function testSendingValues(): void
5050
);
5151
}
5252

53-
public function testThrowingExceptions(): void
53+
public function testCompareThrowingExceptions(): void
5454
{
5555
$this->compare(
5656
fn() => (function () {
@@ -71,7 +71,7 @@ public function testThrowingExceptions(): void
7171
);
7272
}
7373

74-
public function testReturn(): void
74+
public function testCompareReturn(): void
7575
{
7676
$this->compare(
7777
fn() => (function () {
@@ -86,7 +86,7 @@ public function testReturn(): void
8686
);
8787
}
8888

89-
public function testEmpty(): void
89+
public function testCompareEmpty(): void
9090
{
9191
$this->compare(
9292
fn() => (function () {
@@ -100,7 +100,7 @@ public function testEmpty(): void
100100
);
101101
}
102102

103-
public function testEmptyReturn(): void
103+
public function testCompareEmptyReturn(): void
104104
{
105105
$this->compare(
106106
fn() => (function () {
@@ -115,7 +115,7 @@ public function testEmptyReturn(): void
115115
);
116116
}
117117

118-
public function testEmptyThrow(): void
118+
public function testCompareEmptyThrow(): void
119119
{
120120
$this->compare(
121121
fn() => (function () {
@@ -126,7 +126,7 @@ public function testEmptyThrow(): void
126126
);
127127
}
128128

129-
public function testEmptyThrowValid(): void
129+
public function testCompareEmptyThrowValid(): void
130130
{
131131
$this->compare(
132132
fn() => (function () {
@@ -137,7 +137,7 @@ public function testEmptyThrowValid(): void
137137
);
138138
}
139139

140-
public function testEmptyThrowGetReturn(): void
140+
public function testCompareEmptyThrowGetReturn(): void
141141
{
142142
$this->compare(
143143
fn() => (function () {
@@ -148,7 +148,7 @@ public function testEmptyThrowGetReturn(): void
148148
);
149149
}
150150

151-
public function testEmptyThrowGetKey(): void
151+
public function testCompareEmptyThrowGetKey(): void
152152
{
153153
$this->compare(
154154
fn() => (function () {
@@ -159,6 +159,45 @@ public function testEmptyThrowGetKey(): void
159159
);
160160
}
161161

162+
public function testLazyNotGeneratorValidGetReturn(): void
163+
{
164+
$lazy = DeferredGenerator::fromHandler(fn() => 42, EncodedValues::empty());
165+
166+
$this->assertFalse($lazy->valid());
167+
$this->assertSame(42, $lazy->getReturn());
168+
}
169+
170+
public function testLazyNotGeneratorCurrent(): void
171+
{
172+
$lazy = DeferredGenerator::fromHandler(fn() => 42, EncodedValues::empty());
173+
174+
$this->assertNull($lazy->current());
175+
}
176+
177+
public function testLazyNotGeneratorWithException(): void
178+
{
179+
$lazy = DeferredGenerator::fromHandler(fn() => throw new \Exception('foo'), EncodedValues::empty());
180+
181+
$this->expectException(\Exception::class);
182+
$this->expectExceptionMessage('foo');
183+
184+
$lazy->current();
185+
}
186+
187+
188+
public function testLazyNotGeneratorWithException2(): void
189+
{
190+
$lazy = DeferredGenerator::fromHandler(fn() => throw new \Exception('foo'), EncodedValues::empty());
191+
192+
try {
193+
$lazy->current();
194+
} catch (\Exception) {
195+
// ignore
196+
}
197+
198+
$this->assertNull($lazy->current());
199+
}
200+
162201
/**
163202
* @param callable(): \Generator $generatorFactory
164203
* @param iterable<Action|int, array{Action, mixed}> $actions

0 commit comments

Comments
 (0)