diff --git a/src/Workflow/Mutex.php b/src/Workflow/Mutex.php index 639d4b6c..0b20f857 100644 --- a/src/Workflow/Mutex.php +++ b/src/Workflow/Mutex.php @@ -34,7 +34,7 @@ final class Mutex * yield $this->mutex->lock(); * ``` * - * @return PromiseInterface A promise that resolves when the lock is acquired. + * @return PromiseInterface A promise that resolves when the lock is acquired. */ public function lock(): PromiseInterface { diff --git a/tests/Unit/Workflow/MutexTestCase.php b/tests/Unit/Workflow/MutexTestCase.php new file mode 100644 index 00000000..dbd1428f --- /dev/null +++ b/tests/Unit/Workflow/MutexTestCase.php @@ -0,0 +1,60 @@ +assertFalse($mutex->isLocked()); + $mutex->lock(); + $this->assertTrue($mutex->isLocked()); + $mutex->unlock(); + $this->assertFalse($mutex->isLocked()); + } + + public function testTryLock(): void + { + $mutex = new Mutex(); + + $this->assertTrue($mutex->tryLock()); + $this->assertFalse($mutex->tryLock()); + $mutex->unlock(); + $this->assertTrue($mutex->tryLock()); + } + + public function testLock(): void + { + $result = [false, false, false]; + + $mutex = new Mutex(); + $this->assertTrue($mutex->tryLock()); + + $mutex->lock()->then(function (Mutex $mutex) use (&$result) { + $result[0] = true; + $mutex->unlock(); + }); + $mutex->lock()->then(function () use (&$result) { + $result[1] = true; + }); + $mutex->lock()->then(function () use (&$result) { + $result[2] = true; + }); + + + $this->assertSame([false, false, false], $result); + + $mutex->unlock(); + $this->assertSame([true, true, false], $result); + + $mutex->unlock(); + $this->assertSame([true, true, true], $result); + } +}