Skip to content

Commit b515972

Browse files
authored
Avoid redundant wake-ups for child workflows (#364)
1 parent a8f4e97 commit b515972

2 files changed

Lines changed: 14 additions & 35 deletions

File tree

src/ChildWorkflowStub.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use function React\Promise\resolve;
1111
use RuntimeException;
1212
use Throwable;
13-
use Workflow\Exceptions\TransitionNotFound;
1413
use Workflow\Serializers\Serializer;
1514

1615
final class ChildWorkflowStub
@@ -86,13 +85,9 @@ public static function make($workflow, ...$arguments): PromiseInterface
8685
}
8786
}
8887

89-
if ($childWorkflow->running() && ! $childWorkflow->created()) {
90-
try {
91-
$childWorkflow->resume();
92-
} catch (TransitionNotFound) {
93-
// already running
94-
}
95-
} elseif (! $childWorkflow->completed()) {
88+
$runningStartedChildWorkflow = $childWorkflow->running() && ! $childWorkflow->created();
89+
90+
if (! $runningStartedChildWorkflow && ! $childWorkflow->completed()) {
9691
$childWorkflow->startAsChild($context->storedWorkflow, $context->index, $context->now, ...$arguments);
9792
}
9893
}

tests/Unit/ChildWorkflowStubTest.php

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Tests\Fixtures\TestParentWorkflow;
1010
use Tests\TestCase;
1111
use Workflow\ChildWorkflowStub;
12-
use Workflow\Exceptions\TransitionNotFound;
1312
use Workflow\Models\StoredWorkflow;
1413
use Workflow\Serializers\Serializer;
1514
use Workflow\States\WorkflowPendingStatus;
@@ -92,33 +91,18 @@ public function testLoadsChildWorkflow(): void
9291
$this->assertNull($result);
9392
}
9493

95-
public function testIgnoresTransitionNotFoundWhenChildResumeThrows(): void
94+
public function testDoesNotResumeRunningStartedChildWorkflow(): void
9695
{
97-
$childWorkflow = new class() {
98-
public function running(): bool
99-
{
100-
return true;
101-
}
102-
103-
public function created(): bool
104-
{
105-
return false;
106-
}
107-
108-
public function resume(): void
109-
{
110-
throw TransitionNotFound::make('running', 'pending', StoredWorkflow::class);
111-
}
112-
113-
public function completed(): bool
114-
{
115-
return false;
116-
}
117-
118-
public function startAsChild(...$arguments): void
119-
{
120-
}
121-
};
96+
$childWorkflow = Mockery::mock();
97+
$childWorkflow->shouldReceive('running')
98+
->once()
99+
->andReturn(true);
100+
$childWorkflow->shouldReceive('created')
101+
->once()
102+
->andReturn(false);
103+
$childWorkflow->shouldNotReceive('completed');
104+
$childWorkflow->shouldNotReceive('resume');
105+
$childWorkflow->shouldNotReceive('startAsChild');
122106

123107
$storedChildWorkflow = Mockery::mock();
124108
$storedChildWorkflow->status = new \stdClass();

0 commit comments

Comments
 (0)