-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathAbstractSpinlockMutexTest.php
118 lines (103 loc) · 3.48 KB
/
AbstractSpinlockMutexTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
declare(strict_types=1);
namespace Malkusch\Lock\Tests\Mutex;
use Malkusch\Lock\Exception\LockAcquireException;
use Malkusch\Lock\Exception\LockAcquireTimeoutException;
use Malkusch\Lock\Exception\LockReleaseException;
use Malkusch\Lock\Mutex\AbstractSpinlockMutex;
use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\MockEnabledException;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class AbstractSpinlockMutexTest extends TestCase
{
use PHPMock;
#[\Override]
protected function setUp(): void
{
parent::setUp();
$sleepBuilder = new SleepEnvironmentBuilder();
$sleepBuilder->addNamespace(__NAMESPACE__);
$sleepBuilder->addNamespace('Malkusch\Lock\Mutex');
$sleepBuilder->addNamespace('Malkusch\Lock\Util');
$sleep = $sleepBuilder->build();
try {
$sleep->enable();
$this->registerForTearDown($sleep);
} catch (MockEnabledException $e) {
// workaround for burn testing
\assert($e->getMessage() === 'microtime is already enabled. Call disable() on the existing mock.');
}
}
/**
* @return AbstractSpinlockMutex&MockObject
*/
private function createSpinlockMutexMock(float $acquireTimeout = 3): AbstractSpinlockMutex
{
return $this->getMockBuilder(AbstractSpinlockMutex::class)
->setConstructorArgs(['test', $acquireTimeout])
->onlyMethods(['acquire', 'release'])
->getMock();
}
/**
* Tests failing to acquire the lock.
*/
public function testFailAcquireLock(): void
{
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::any())
->method('acquire')
->willThrowException(new LockAcquireException());
$this->expectException(LockAcquireException::class);
$mutex->synchronized(static function () {
self::fail();
});
}
/**
* Tests failing to acquire the lock due to a timeout.
*/
public function testAcquireTimeouts(): void
{
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::atLeastOnce())
->method('acquire')
->willReturn(false);
$this->expectException(LockAcquireTimeoutException::class);
$this->expectExceptionMessage('Lock acquire timeout of 3.0 seconds has been exceeded');
$mutex->synchronized(static function () {
self::fail();
});
}
/**
* Tests executing code which barely doesn't hit the acquire timeout.
*/
public function testExecuteBarelySucceeds(): void
{
$mutex = $this->createSpinlockMutexMock(0.5);
$mutex->expects(self::any())
->method('acquire')
->willReturn(true);
$mutex->expects(self::once())
->method('release')
->willReturn(true);
$mutex->synchronized(static function () {
usleep(499 * 1000);
});
}
/**
* Tests failing to release a lock.
*/
public function testFailReleasingLock(): void
{
$mutex = $this->createSpinlockMutexMock();
$mutex->expects(self::any())
->method('acquire')
->willReturn(true);
$mutex->expects(self::any())
->method('release')
->willReturn(false);
$this->expectException(LockReleaseException::class);
$mutex->synchronized(static function () {});
}
}