Skip to content

Commit

Permalink
Add Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Oct 8, 2024
1 parent ac04718 commit 3c181ec
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Workflow/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class Mutex
* yield $this->mutex->lock();
* ```
*
* @return PromiseInterface A promise that resolves when the lock is acquired.
* @return PromiseInterface<self> A promise that resolves when the lock is acquired.

Check failure on line 37 in src/Workflow/Mutex.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

TooManyTemplateParams

src/Workflow/Mutex.php:37:16: TooManyTemplateParams: React\Promise\PromiseInterface<Temporal\Workflow\Mutex> has too many template params, expecting 0 (see https://psalm.dev/184)

Check failure on line 37 in src/Workflow/Mutex.php

View workflow job for this annotation

GitHub Actions / Psalm Validation (PHP 8.3, OS ubuntu-latest)

TooManyTemplateParams

src/Workflow/Mutex.php:37:16: TooManyTemplateParams: React\Promise\PromiseInterface<Temporal\Workflow\Mutex> has too many template params, expecting 0 (see https://psalm.dev/184)
*/
public function lock(): PromiseInterface
{
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/Workflow/MutexTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Unit\Workflow;

use PHPUnit\Framework\TestCase;
use Temporal\Workflow\Mutex;

final class MutexTestCase extends TestCase
{
public function testIsLockedLockUnlock(): void
{
$mutex = new Mutex();

$this->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);
}
}

0 comments on commit 3c181ec

Please sign in to comment.