Skip to content

Commit 4148e90

Browse files
authored
Fix timer replay (#336)
1 parent a8f78e8 commit 4148e90

5 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/Traits/AwaitWithTimeouts.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use function React\Promise\resolve;
1111
use Workflow\Serializers\Serializer;
1212
use Workflow\Signal;
13+
use Workflow\Timer;
1314

1415
trait AwaitWithTimeouts
1516
{
@@ -19,7 +20,14 @@ public static function awaitWithTimeout(int|string|CarbonInterval $seconds, $con
1920

2021
if ($log) {
2122
++self::$context->index;
22-
return resolve(Serializer::unserialize($log->result));
23+
24+
$result = Serializer::unserialize($log->result);
25+
26+
if ($log->class === Timer::class) {
27+
return resolve(! $result);
28+
}
29+
30+
return resolve($result);
2331
}
2432

2533
$result = $condition();

tests/Feature/AwaitWithTimeoutWorkflowTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tests\Feature;
66

7+
use Tests\Fixtures\TestAwaitWithTimeoutReplayWorkflow;
78
use Tests\Fixtures\TestAwaitWithTimeoutWorkflow;
89
use Tests\TestCase;
910
use Workflow\States\WorkflowCompletedStatus;
@@ -40,4 +41,16 @@ public function testTimedout(): void
4041
$this->assertSame(WorkflowCompletedStatus::class, $workflow->status());
4142
$this->assertSame('workflow_timed_out', $workflow->output());
4243
}
44+
45+
public function testTimedoutResultStaysFalseAfterReplay(): void
46+
{
47+
$workflow = WorkflowStub::make(TestAwaitWithTimeoutReplayWorkflow::class);
48+
49+
$workflow->start();
50+
51+
while ($workflow->running());
52+
53+
$this->assertSame(WorkflowCompletedStatus::class, $workflow->status());
54+
$this->assertFalse($workflow->output());
55+
}
4356
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Fixtures;
6+
7+
use function Workflow\activity;
8+
use function Workflow\awaitWithTimeout;
9+
use Workflow\Workflow;
10+
11+
final class TestAwaitWithTimeoutReplayWorkflow extends Workflow
12+
{
13+
public function execute()
14+
{
15+
$result = yield awaitWithTimeout(1, static fn (): bool => false);
16+
17+
yield activity(TestCountActivity::class, $result ? 1 : 0);
18+
19+
return $result;
20+
}
21+
}

tests/Unit/Traits/AwaitWithTimeoutsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Workflow\Serializers\Serializer;
1212
use Workflow\Signal;
1313
use Workflow\States\WorkflowPendingStatus;
14+
use Workflow\Timer;
1415
use Workflow\WorkflowStub;
1516

1617
final class AwaitWithTimeoutsTest extends TestCase
@@ -87,6 +88,27 @@ public function testLoadsStoredResult(): void
8788
$this->assertTrue(Serializer::unserialize($workflow->logs()->firstWhere('index', 0)->result));
8889
}
8990

91+
public function testLoadsStoredTimerResultAsTimedOut(): void
92+
{
93+
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
94+
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
95+
$storedWorkflow->logs()
96+
->create([
97+
'index' => 0,
98+
'now' => WorkflowStub::now(),
99+
'class' => Timer::class,
100+
'result' => Serializer::serialize(true),
101+
]);
102+
103+
WorkflowStub::awaitWithTimeout('1 minute', static fn () => true)
104+
->then(static function ($value) use (&$result) {
105+
$result = $value;
106+
});
107+
108+
$this->assertSame(false, $result);
109+
$this->assertSame(1, $workflow->logs()->count());
110+
}
111+
90112
public function testResolvesConflictingResult(): void
91113
{
92114
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());

tests/Unit/Traits/TimersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testLoadsStoredResult(): void
135135
$result = $value;
136136
});
137137

138-
$this->assertSame(true, $result);
138+
$this->assertSame(false, $result);
139139
$this->assertSame(1, $workflow->logs()->count());
140140
$this->assertDatabaseHas('workflow_logs', [
141141
'stored_workflow_id' => $workflow->id(),

0 commit comments

Comments
 (0)