Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 5, 2024
1 parent ad9bc48 commit f8e4d58
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions tests/mutex/SpinlockMutexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,25 @@ protected function setUp(): void
}
}

/**
* @return SpinlockMutex&MockObject
*/
private function createSpinlockMutexMock(float $timeout = 3): SpinlockMutex
{
return $this->getMockBuilder(SpinlockMutex::class)
->setConstructorArgs(['test', $timeout])
->onlyMethods(['acquire', 'release'])
->getMock();
}

/**
* Tests failing to acquire the lock.
*/
public function testFailAcquireLock(): void
{
$this->expectException(LockAcquireException::class);

$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::any())
->method('acquire')
->willThrowException(new LockAcquireException());
Expand All @@ -59,7 +70,7 @@ public function testAcquireTimesOut(): void
$this->expectException(TimeoutException::class);
$this->expectExceptionMessage('Timeout of 3.0 seconds exceeded');

$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::atLeastOnce())
->method('acquire')
->willReturn(false);
Expand All @@ -74,7 +85,7 @@ public function testAcquireTimesOut(): void
*/
public function testExecuteTooLong(): void
{
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]);
$mutex = $this->createSpinlockMutexMock(0.5);
$mutex->expects(self::any())
->method('acquire')
->willReturn(true);
Expand All @@ -99,7 +110,7 @@ public function testExecuteTooLong(): void
*/
public function testExecuteBarelySucceeds(): void
{
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]);
$mutex = $this->createSpinlockMutexMock(0.5);
$mutex->expects(self::any())->method('acquire')->willReturn(true);
$mutex->expects(self::once())->method('release')->willReturn(true);

Expand All @@ -115,7 +126,7 @@ public function testFailReleasingLock(): void
{
$this->expectException(LockReleaseException::class);

$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test']);
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::any())->method('acquire')->willReturn(true);
$mutex->expects(self::any())->method('release')->willReturn(false);

Expand All @@ -127,7 +138,7 @@ public function testFailReleasingLock(): void
*/
public function testExecuteTimeoutLeavesOneSecondForKeyToExpire(): void
{
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.2]);
$mutex = $this->createSpinlockMutexMock(0.2);
$mutex->expects(self::once())
->method('acquire')
->with(self::anything(), 1.2)
Expand Down

0 comments on commit f8e4d58

Please sign in to comment.