-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathMemcachedMutexTest.php
82 lines (66 loc) · 2.24 KB
/
MemcachedMutexTest.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
<?php
declare(strict_types=1);
namespace Malkusch\Lock\Tests\Mutex;
use Malkusch\Lock\Exception\LockAcquireTimeoutException;
use Malkusch\Lock\Exception\LockReleaseException;
use Malkusch\Lock\Mutex\MemcachedMutex;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Please provide the environment variable MEMCACHE_HOST.
*
* @requires extension memcached
*/
#[RequiresPhpExtension('memcached')]
class MemcachedMutexTest extends TestCase
{
/** @var \Memcached&MockObject */
protected $memcached;
/** @var MemcachedMutex */
private $mutex;
#[\Override]
protected function setUp(): void
{
parent::setUp();
$this->memcached = $this->createMock(\Memcached::class);
$this->mutex = new MemcachedMutex('test', $this->memcached, 1, 2);
}
public function testAcquireFail(): void
{
$this->memcached->expects(self::atLeastOnce())
->method('add')
->with('php-malkusch-lock:test', true, 3)
->willReturn(false);
$this->expectException(LockAcquireTimeoutException::class);
$this->mutex->synchronized(static function () {
self::fail();
});
}
public function testReleaseFail(): void
{
$this->memcached->expects(self::once())
->method('add')
->with('php-malkusch-lock:test', true, 3)
->willReturn(true);
$this->memcached->expects(self::once())
->method('delete')
->with('php-malkusch-lock:test')
->willReturn(false);
$this->expectException(LockReleaseException::class);
$this->mutex->synchronized(static function () {});
}
public function testAcquireExpireTimeoutLimit(): void
{
$this->mutex = new MemcachedMutex('test', $this->memcached);
$this->memcached->expects(self::once())
->method('add')
->with('php-malkusch-lock:test', true, 0)
->willReturn(true);
$this->memcached->expects(self::once())
->method('delete')
->with('php-malkusch-lock:test')
->willReturn(true);
$this->mutex->synchronized(static function () {});
}
}