-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathAbstractSpinlockWithTokenMutexTest.php
85 lines (72 loc) · 2.81 KB
/
AbstractSpinlockWithTokenMutexTest.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
<?php
declare(strict_types=1);
namespace Malkusch\Lock\Tests\Mutex;
use Malkusch\Lock\Exception\ExecutionOutsideLockException;
use Malkusch\Lock\Mutex\AbstractSpinlockWithTokenMutex;
use phpmock\environment\SleepEnvironmentBuilder;
use phpmock\MockEnabledException;
use phpmock\phpunit\PHPMock;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class AbstractSpinlockWithTokenMutexTest 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 AbstractSpinlockWithTokenMutex&MockObject
*/
private function createSpinlockWithTokenMutexMock(float $acquireTimeout = 3, float $expireTimeout = \INF): AbstractSpinlockWithTokenMutex
{
return $this->getMockBuilder(AbstractSpinlockWithTokenMutex::class)
->setConstructorArgs(['test', $acquireTimeout, $expireTimeout])
->onlyMethods(['acquireWithToken', 'releaseWithToken'])
->getMock();
}
public function testExecuteExpireTimeout(): void
{
$mutex = $this->createSpinlockWithTokenMutexMock(0.1, 0.2);
$mutex->expects(self::once())
->method('acquireWithToken')
->with(self::anything(), 0.2)
->willReturn('xx');
$mutex->expects(self::once())
->method('releaseWithToken')
->with(self::anything(), 'xx')
->willReturn(true);
$mutex->synchronized(static function () {
usleep(199 * 1000);
});
}
public function testExecuteTooLong(): void
{
$mutex = $this->createSpinlockWithTokenMutexMock(0.1, 0.2);
$mutex->expects(self::any())
->method('acquireWithToken')
->with(self::anything(), 0.2)
->willReturn('xx');
$mutex->expects(self::any())
->method('releaseWithToken')
->willReturn(true);
$this->expectException(ExecutionOutsideLockException::class);
$this->expectExceptionMessageMatches('~^The code executed for 0\.2\d+ seconds\. But the expire timeout is 0\.2 seconds. The last 0\.0\d+ seconds were executed outside of the lock\.$~');
$mutex->synchronized(static function () {
usleep(201 * 1000);
});
}
}