-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathRedisSentinelTest.php
More file actions
160 lines (130 loc) · 3.68 KB
/
RedisSentinelTest.php
File metadata and controls
160 lines (130 loc) · 3.68 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
require_once __DIR__ . "/TestSuite.php";
class Redis_Sentinel_Test extends TestSuite
{
const NAME = 'mymaster';
/**
* @var RedisSentinel
*/
public $sentinel;
/**
* Common fields
*/
protected $fields = [
'name',
'ip',
'port',
'runid',
'flags',
'link-pending-commands',
'link-refcount',
'last-ping-sent',
'last-ok-ping-reply',
'last-ping-reply',
'down-after-milliseconds',
];
protected function newInstance()
{
return new RedisSentinel(['host' => $this->getHost()]);
}
public function setUp()
{
$this->sentinel = $this->newInstance();
}
public function testCkquorum()
{
$this->assertTrue($this->sentinel->ckquorum(self::NAME));
}
public function testFailover()
{
$this->assertFalse($this->sentinel->failover(self::NAME));
}
public function testFlushconfig()
{
$this->assertTrue($this->sentinel->flushconfig());
}
public function testGetMasterAddrByName()
{
$result = $this->sentinel->getMasterAddrByName(self::NAME);
$this->assertTrue(is_array($result));
$this->assertEquals(2, count($result));
}
protected function checkFields(array $fields)
{
foreach ($this->fields as $k) {
$this->assertTrue(array_key_exists($k, $fields));
}
}
public function testMaster()
{
$result = $this->sentinel->master(self::NAME);
$this->assertTrue(is_array($result));
$this->checkFields($result);
}
public function testMasters()
{
$result = $this->sentinel->masters();
$this->assertTrue(is_array($result));
foreach ($result as $master) {
$this->checkFields($master);
}
}
public function testMyid()
{
$result = $this->sentinel->myid();
$this->assertTrue(is_string($result));
}
public function testPing()
{
$this->assertTrue($this->sentinel->ping());
}
public function testReset()
{
$this->assertEquals(1, $this->sentinel->reset('*'));
}
public function testSentinels()
{
$result = $this->sentinel->sentinels(self::NAME);
$this->assertTrue(is_array($result));
foreach ($result as $sentinel) {
$this->checkFields($sentinel);
}
}
public function testSlaves()
{
$result = $this->sentinel->slaves(self::NAME);
$this->assertTrue(is_array($result));
foreach ($result as $slave) {
$this->checkFields($slave);
}
}
protected function getClients(Redis $redis, string $cmd)
{
$result = [];
foreach ($redis->client('list') as $client) {
if ($client['cmd'] !== $cmd)
continue;
$result[] = $client['id'];
}
return $result;
}
public function testPersistent() {
/* I think the tests just use the default port */
$redis = new Redis;
$redis->connect($this->getHost(), 26379);
$id = null;
for ($i = 0; $i < 3; $i++) {
$sentinel = new RedisSentinel([
'host' => $this->getHost(),
'persistent' => 'sentinel',
]);
$this->assertTrue($sentinel->ping());
$clients = $this->getClients($redis, 'ping');
/* Capture the ping client */
$id ??= $clients[0];
unset($sentinel);
}
/* The same client should have been reused */
$this->assertEquals($id, $clients[0]);
}
}