-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRedisSimpleLockTest.php
77 lines (59 loc) · 2.1 KB
/
RedisSimpleLockTest.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
<?php
use PHPUnit\Framework\TestCase;
use TH\RedisLock\RedisSimpleLock;
class RedisSimpleLockTest extends TestCase
{
private $redisClient;
public function setUp(): void
{
$this->redisClient = new \Predis\Client(getenv("REDIS_URI"));
$this->redisClient->flushdb();
}
public function testLock()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
$lock2 = new RedisSimpleLock("lock identifier", $this->redisClient);
$lock1->acquire();
// Only the second acquire is supposed to fail
$this->expectException("Exception");
$lock2->acquire();
}
public function testLockTtl()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
$lock2 = new RedisSimpleLock("lock identifier", $this->redisClient);
$lock1->acquire();
usleep(100000);
// first lock sould have been released
$lock2->acquire();
}
public function testLockSafeRelease()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
$lock2 = new RedisSimpleLock("lock identifier", $this->redisClient);
$lock1->acquire();
usleep(100000);
$lock2->acquire();
$lock1->release();
// lock should still exists
$this->assertTrue($this->redisClient->exists("lock identifier") === 1, "Lock should not have been released");
}
public function testLockRelease()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
$lock2 = new RedisSimpleLock("lock identifier", $this->redisClient);
$lock1->acquire();
$lock1->release();
// first lock sould have been released
$lock2->acquire();
}
public function testLockAutoRelease()
{
$lock1 = new RedisSimpleLock("lock identifier", $this->redisClient, 50);
$lock2 = new RedisSimpleLock("lock identifier", $this->redisClient);
$lock1->acquire();
unset($lock1);
// first lock sould have been released
$lock2->acquire();
}
}