From e9b19f8d223e75bf063a2763e633c63c199b79ae Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Mon, 29 Jan 2024 09:33:54 +0100 Subject: [PATCH 1/3] test: demonstrate race condition when dispatching workflows within transactions --- .../DispatchWorkflowInTransactionTest.php | 55 +++++++++++++++++++ tests/Fixtures/TestSimpleWorkflow.php | 26 +++++++++ 2 files changed, 81 insertions(+) create mode 100644 tests/Feature/DispatchWorkflowInTransactionTest.php create mode 100644 tests/Fixtures/TestSimpleWorkflow.php diff --git a/tests/Feature/DispatchWorkflowInTransactionTest.php b/tests/Feature/DispatchWorkflowInTransactionTest.php new file mode 100644 index 0000000..b448b34 --- /dev/null +++ b/tests/Feature/DispatchWorkflowInTransactionTest.php @@ -0,0 +1,55 @@ + 1, "workflow_id" => $workflow->id()]) + */ + + /** + * dispatch the workflow to the queue + */ + $workflow->start(); + + /** + * pretend that committing the transaction takes a while + */ + sleep(3); + }); + + /** + * the workflow stays in the pending state as the transaction was not committed + * when the worker was told to process the workflow + * + * the exception is silently caught in src/Workflow.php:115 + */ + while ($workflow->running() && $workflow->exceptions()->isEmpty() && now()->diffInSeconds($start) < 5); + + + $this->assertSame(WorkflowCompletedStatus::class, $workflow->status()); + $this->assertEmpty($workflow->exceptions()); + } +} diff --git a/tests/Fixtures/TestSimpleWorkflow.php b/tests/Fixtures/TestSimpleWorkflow.php new file mode 100644 index 0000000..7a98649 --- /dev/null +++ b/tests/Fixtures/TestSimpleWorkflow.php @@ -0,0 +1,26 @@ + Date: Tue, 30 Jan 2024 09:13:05 +0100 Subject: [PATCH 2/3] feat: set $afterCommit = true to prevent race condition refs #157 --- src/Activity.php | 2 ++ src/Workflow.php | 2 ++ tests/Feature/DispatchWorkflowInTransactionTest.php | 6 +----- tests/Fixtures/TestSimpleWorkflow.php | 4 ---- 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Activity.php b/src/Activity.php index 560ee83..dec936e 100644 --- a/src/Activity.php +++ b/src/Activity.php @@ -58,6 +58,8 @@ public function __construct( if (property_exists($this, 'queue')) { $this->onQueue($this->queue); } + + $this->afterCommit = true; } public function backoff() diff --git a/src/Workflow.php b/src/Workflow.php index 67a1e6f..3d7902c 100644 --- a/src/Workflow.php +++ b/src/Workflow.php @@ -65,6 +65,8 @@ public function __construct( if (property_exists($this, 'queue')) { $this->onQueue($this->queue); } + + $this->afterCommit = true; } public function query($method) diff --git a/tests/Feature/DispatchWorkflowInTransactionTest.php b/tests/Feature/DispatchWorkflowInTransactionTest.php index b448b34..68ee655 100644 --- a/tests/Feature/DispatchWorkflowInTransactionTest.php +++ b/tests/Feature/DispatchWorkflowInTransactionTest.php @@ -12,15 +12,12 @@ final class DispatchWorkflowInTransactionTest extends TestCase { - /** - * @TODO: This test is not production ready - */ public function testRaceCondition(): void { $workflow = null; $start = now(); - DB::transaction(function () use (&$workflow) { + DB::transaction(static function () use (&$workflow) { $workflow = WorkflowStub::make(TestSimpleWorkflow::class); /** @@ -48,7 +45,6 @@ public function testRaceCondition(): void */ while ($workflow->running() && $workflow->exceptions()->isEmpty() && now()->diffInSeconds($start) < 5); - $this->assertSame(WorkflowCompletedStatus::class, $workflow->status()); $this->assertEmpty($workflow->exceptions()); } diff --git a/tests/Fixtures/TestSimpleWorkflow.php b/tests/Fixtures/TestSimpleWorkflow.php index 7a98649..400a509 100644 --- a/tests/Fixtures/TestSimpleWorkflow.php +++ b/tests/Fixtures/TestSimpleWorkflow.php @@ -4,12 +4,8 @@ namespace Tests\Fixtures; -use Illuminate\Contracts\Foundation\Application; use Workflow\ActivityStub; -use Workflow\QueryMethod; -use Workflow\SignalMethod; use Workflow\Workflow; -use Workflow\WorkflowStub; class TestSimpleWorkflow extends Workflow { From d9f2448ee3a492c0dac3d6ac760cc726b49a3cf9 Mon Sep 17 00:00:00 2001 From: Erik Anders <5753604+Naugrimm@users.noreply.github.com> Date: Tue, 30 Jan 2024 10:06:45 +0100 Subject: [PATCH 3/3] fix: try to fix tests on the container refs #157 --- tests/Feature/DispatchWorkflowInTransactionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/DispatchWorkflowInTransactionTest.php b/tests/Feature/DispatchWorkflowInTransactionTest.php index 68ee655..5038c4c 100644 --- a/tests/Feature/DispatchWorkflowInTransactionTest.php +++ b/tests/Feature/DispatchWorkflowInTransactionTest.php @@ -43,7 +43,7 @@ public function testRaceCondition(): void * * the exception is silently caught in src/Workflow.php:115 */ - while ($workflow->running() && $workflow->exceptions()->isEmpty() && now()->diffInSeconds($start) < 5); + while ($workflow->running() && $workflow->exceptions()->isEmpty() && now()->diffInSeconds($start) < 15); $this->assertSame(WorkflowCompletedStatus::class, $workflow->status()); $this->assertEmpty($workflow->exceptions()); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy