-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathMongoLockTest.php
107 lines (79 loc) · 2.81 KB
/
MongoLockTest.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
<?php
namespace MongoDB\Laravel\Tests\Cache;
use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use MongoDB\Laravel\Cache\MongoLock;
use MongoDB\Laravel\Tests\TestCase;
use function now;
class MongoLockTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::connection('mongodb')->getCollection('foo_cache_locks')
->createIndex(['name' => 1], ['unique' => true]);
}
public function tearDown(): void
{
DB::connection('mongodb')->getCollection('foo_cache_locks')->drop();
parent::tearDown();
}
public function testLockCanBeAcquired()
{
$lock = $this->getCache()->lock('foo');
$this->assertTrue($lock->get());
$this->assertTrue($lock->get());
$otherLock = $this->getCache()->lock('foo');
$this->assertFalse($otherLock->get());
$lock->release();
$otherLock = $this->getCache()->lock('foo');
$this->assertTrue($otherLock->get());
$this->assertTrue($otherLock->get());
$otherLock->release();
}
public function testLockCanBeForceReleased()
{
$lock = $this->getCache()->lock('foo');
$this->assertTrue($lock->get());
$otherLock = $this->getCache()->lock('foo');
$otherLock->forceRelease();
$this->assertTrue($otherLock->get());
$otherLock->release();
}
public function testExpiredLockCanBeRetrieved()
{
$lock = $this->getCache()->lock('foo');
$this->assertTrue($lock->get());
DB::table('foo_cache_locks')->update(['expiration' => now()->subDays(1)->getTimestamp()]);
$otherLock = $this->getCache()->lock('foo');
$this->assertTrue($otherLock->get());
$otherLock->release();
}
public function testOwnedByCurrentProcess()
{
$lock = $this->getCache()->lock('foo');
$this->assertFalse($lock->isOwnedByCurrentProcess());
$lock->acquire();
$this->assertTrue($lock->isOwnedByCurrentProcess());
$otherLock = $this->getCache()->lock('foo');
$this->assertFalse($otherLock->isOwnedByCurrentProcess());
}
public function testRestoreLock()
{
$lock = $this->getCache()->lock('foo');
$lock->acquire();
$this->assertInstanceOf(MongoLock::class, $lock);
$owner = $lock->owner();
$resoredLock = $this->getCache()->restoreLock('foo', $owner);
$this->assertTrue($resoredLock->isOwnedByCurrentProcess());
$resoredLock->release();
$this->assertFalse($resoredLock->isOwnedByCurrentProcess());
}
private function getCache(): Repository
{
$repository = Cache::driver('mongodb');
$this->assertInstanceOf(Repository::class, $repository);
return $repository;
}
}