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
161
162
163
164
165
166
167
168
169
170
171
172
<?php
require_once dirname(__FILE__).'/../Client.php';
require_once dirname(__FILE__).'/../Cluster.php';
require_once dirname(__FILE__).'/../Sentinel.php';
require_once dirname(__FILE__).'/CredisTestCommon.php';
class CredisSentinelTest extends CredisTestCommon
{
/** @var Credis_Sentinel */
protected $sentinel;
protected $sentinelConfig;
protected function setUp()
{
parent::setUp();
if($this->sentinelConfig === NULL) {
$configFile = dirname(__FILE__).'/sentinel_config.json';
if( ! file_exists($configFile) || ! ($config = file_get_contents($configFile))) {
$this->markTestSkipped('Could not load '.$configFile);
return;
}
$this->sentinelConfig = json_decode($config);
}
$sentinelClient = new Credis_Client($this->sentinelConfig->host, $this->sentinelConfig->port);
$this->sentinel = new Credis_Sentinel($sentinelClient);
if($this->useStandalone) {
$this->sentinel->forceStandalone();
}
$this->waitForSlaveReplication();
}
protected function tearDown()
{
if($this->sentinel) {
$this->sentinel = NULL;
}
}
public function testMasterClient()
{
$master = $this->sentinel->getMasterClient($this->sentinelConfig->clustername);
$this->assertInstanceOf('Credis_Client',$master);
$this->assertEquals($this->redisConfig[0]['port'],$master->getPort());
$this->setExpectedExceptionShim('CredisException','Master not found');
$this->sentinel->getMasterClient('non-existing-cluster');
}
public function testMasters()
{
$masters = $this->sentinel->masters();
$this->assertInternalType('array',$masters);
$this->assertCount(2,$masters);
$this->assertArrayHasKey(0,$masters);
$this->assertArrayHasKey(1,$masters);
$this->assertArrayHasKey(1,$masters[0]);
$this->assertArrayHasKey(1,$masters[1]);
$this->assertArrayHasKey(5,$masters[1]);
if($masters[0][1] == 'masterdown'){
$this->assertEquals($this->sentinelConfig->clustername,$masters[1][1]);
$this->assertEquals($this->redisConfig[0]['port'],$masters[1][5]);
} else {
$this->assertEquals('masterdown',$masters[1][1]);
$this->assertEquals($this->sentinelConfig->clustername,$masters[0][1]);
$this->assertEquals($this->redisConfig[0]['port'],$masters[0][5]);
}
}
public function testMaster()
{
$master = $this->sentinel->master($this->sentinelConfig->clustername);
$this->assertInternalType('array',$master);
$this->assertArrayHasKey(1,$master);
$this->assertArrayHasKey(5,$master);
$this->assertEquals($this->sentinelConfig->clustername,$master[1]);
$this->assertEquals($this->redisConfig[0]['port'],$master[5]);
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->master('non-existing-cluster');
}
public function testSlaveClient()
{
$slaves = $this->sentinel->getSlaveClients($this->sentinelConfig->clustername);
$this->assertInternalType('array',$slaves);
$this->assertCount(1,$slaves);
foreach($slaves as $slave){
$this->assertInstanceOf('Credis_Client',$slave);
}
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->getSlaveClients('non-existing-cluster');
}
public function testSlaves()
{
$slaves = $this->sentinel->slaves($this->sentinelConfig->clustername);
$this->assertInternalType('array',$slaves);
$this->assertCount(1,$slaves);
$this->assertArrayHasKey(0,$slaves);
$this->assertArrayHasKey(5,$slaves[0]);
$this->assertEquals(6385,$slaves[0][5]);
$slaves = $this->sentinel->slaves('masterdown');
$this->assertInternalType('array',$slaves);
$this->assertCount(0,$slaves);
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->slaves('non-existing-cluster');
}
public function testNonExistingClusterNameWhenCreatingSlaves()
{
$this->setExpectedExceptionShim('CredisException','No such master with that name');
$this->sentinel->createSlaveClients('non-existing-cluster');
}
public function testCreateCluster()
{
$cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername);
$this->assertInstanceOf('Credis_Cluster',$cluster);
$this->assertCount(2,$cluster->clients());
$cluster = $this->sentinel->createCluster($this->sentinelConfig->clustername,0,1,false);
$this->assertInstanceOf('Credis_Cluster',$cluster);
$this->assertCount(2,$cluster->clients());
$this->setExpectedExceptionShim('CredisException','The master is down');
$this->sentinel->createCluster($this->sentinelConfig->downclustername);
}
public function testGetCluster()
{
$cluster = $this->sentinel->getCluster($this->sentinelConfig->clustername);
$this->assertInstanceOf('Credis_Cluster',$cluster);
$this->assertCount(2,$cluster->clients());
}
public function testGetClusterOnDbNumber2()
{
$cluster = $this->sentinel->getCluster($this->sentinelConfig->clustername,2);
$this->assertInstanceOf('Credis_Cluster',$cluster);
$this->assertCount(2,$cluster->clients());
$clients = $cluster->clients();
$this->assertEquals(2,$clients[0]->getSelectedDb());
$this->assertEquals(2,$clients[1]->getSelectedDb());
}
public function testGetMasterAddressByName()
{
$address = $this->sentinel->getMasterAddressByName($this->sentinelConfig->clustername);
$this->assertInternalType('array',$address);
$this->assertCount(2,$address);
$this->assertArrayHasKey(0,$address);
$this->assertArrayHasKey(1,$address);
$this->assertEquals($this->redisConfig[0]['host'],$address[0]);
$this->assertEquals($this->redisConfig[0]['port'],$address[1]);
}
public function testPing()
{
$pong = $this->sentinel->ping();
$this->assertEquals("PONG",$pong);
}
public function testGetHostAndPort()
{
$host = 'localhost';
$port = '123456';
$client = $this->createMockShim('\Credis_Client');
$sentinel = new Credis_Sentinel($client);
$client->expects($this->once())->method('getHost')->willReturn($host);
$client->expects($this->once())->method('getPort')->willReturn($port);
$this->assertEquals($host, $sentinel->getHost());
$this->assertEquals($port, $sentinel->getPort());
}
public function testNonExistingMethod()
{
$this->setExpectedExceptionShim('CredisException','Unknown sentinel subcommand \'bla\'');
$this->sentinel->bla();
}
}