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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
require_once dirname(__FILE__).'/../Client.php';
require_once dirname(__FILE__).'/../Cluster.php';
require_once dirname(__FILE__).'/CredisTestCommon.php';
class CredisClusterTest extends CredisTestCommon
{
/** @var Credis_Cluster */
protected $cluster;
protected function setUp()
{
parent::setUp();
$clients = array_slice($this->redisConfig,0,4);
$this->cluster = new Credis_Cluster($clients,2,$this->useStandalone);
}
protected function tearDown()
{
if($this->cluster) {
$this->cluster->flushAll();
foreach($this->cluster->clients() as $client){
if($client->isConnected()) {
$client->close();
}
}
$this->cluster = NULL;
}
}
public function testKeyHashing()
{
$this->tearDown();
$this->cluster = new Credis_Cluster(array_slice($this->redisConfig, 0, 3), 2, $this->useStandalone);
$keys = array();
$lines = explode("\n", file_get_contents("keys.test"));
foreach ($lines as $line) {
$pair = explode(':', trim($line));
if (count($pair) >= 2) {
$keys[$pair[0]] = $pair[1];
}
}
foreach ($keys as $key => $value) {
$this->assertTrue($this->cluster->set($key, $value));
}
$this->cluster = new Credis_Cluster(array_slice($this->redisConfig, 0, 4), 2, true, $this->useStandalone);
$hits = 0;
foreach ($keys as $key => $value) {
if ($this->cluster->all('get',$key)) {
$hits++;
}
}
$this->assertEquals(count($keys),$hits);
}
public function testAlias()
{
$slicedConfig = array_slice($this->redisConfig, 0, 4);
foreach($slicedConfig as $config) {
$this->assertEquals($config['port'],$this->cluster->client($config['alias'])->getPort());
}
foreach($slicedConfig as $offset => $config) {
$this->assertEquals($config['port'],$this->cluster->client($offset)->getPort());
}
$alias = "non-existent-alias";
$this->setExpectedExceptionShim('CredisException',"Client $alias does not exist.");
$this->cluster->client($alias);
}
public function testMasterSlave()
{
$this->tearDown();
$this->cluster = new Credis_Cluster(array($this->redisConfig[0],$this->redisConfig[6]), 2, $this->useStandalone);
$this->assertTrue($this->cluster->client('master')->set('key','value'));
$this->waitForSlaveReplication();
$this->assertEquals('value',$this->cluster->client('slave')->get('key'));
$this->assertEquals('value',$this->cluster->get('key'));
try
{
$this->cluster->client('slave')->set('key2', 'value');
$this->fail('Writing to readonly slave');
}
catch(CredisException $e)
{
}
$this->tearDown();
$writeOnlyConfig = $this->redisConfig[0];
$writeOnlyConfig['write_only'] = true;
$this->cluster = new Credis_Cluster(array($writeOnlyConfig,$this->redisConfig[6]), 2, $this->useStandalone);
$this->assertTrue($this->cluster->client('master')->set('key','value'));
$this->waitForSlaveReplication();
$this->assertEquals('value',$this->cluster->client('slave')->get('key'));
$this->assertEquals('value',$this->cluster->get('key'));
$this->setExpectedExceptionShim('CredisException');
$this->assertFalse($this->cluster->client('slave')->set('key2','value'));
}
public function testMasterWithoutSlavesAndWriteOnlyFlag()
{
$this->tearDown();
$writeOnlyConfig = $this->redisConfig[0];
$writeOnlyConfig['write_only'] = true;
$this->cluster = new Credis_Cluster(array($writeOnlyConfig),2,$this->useStandalone);
$this->assertTrue($this->cluster->set('key','value'));
$this->assertEquals('value',$this->cluster->get('key'));
}
public function testDontHashForCodeCoverage()
{
$this->assertInternalType('array',$this->cluster->info());
}
public function testByHash()
{
$this->cluster->set('key','value');
$this->assertEquals(6379,$this->cluster->byHash('key')->getPort());
}
public function testRwsplit()
{
$readOnlyCommands = array(
'EXISTS',
'TYPE',
'KEYS',
'SCAN',
'RANDOMKEY',
'TTL',
'GET',
'MGET',
'SUBSTR',
'STRLEN',
'GETRANGE',
'GETBIT',
'LLEN',
'LRANGE',
'LINDEX',
'SCARD',
'SISMEMBER',
'SINTER',
'SUNION',
'SDIFF',
'SMEMBERS',
'SSCAN',
'SRANDMEMBER',
'ZRANGE',
'ZREVRANGE',
'ZRANGEBYSCORE',
'ZREVRANGEBYSCORE',
'ZCARD',
'ZSCORE',
'ZCOUNT',
'ZRANK',
'ZREVRANK',
'ZSCAN',
'HGET',
'HMGET',
'HEXISTS',
'HLEN',
'HKEYS',
'HVALS',
'HGETALL',
'HSCAN',
'PING',
'AUTH',
'SELECT',
'ECHO',
'QUIT',
'OBJECT',
'BITCOUNT',
'TIME',
'SORT'
);
foreach($readOnlyCommands as $command){
$this->assertTrue($this->cluster->isReadOnlyCommand($command));
}
$this->assertFalse($this->cluster->isReadOnlyCommand("SET"));
$this->assertFalse($this->cluster->isReadOnlyCommand("HDEL"));
$this->assertFalse($this->cluster->isReadOnlyCommand("RPUSH"));
$this->assertFalse($this->cluster->isReadOnlyCommand("SMOVE"));
$this->assertFalse($this->cluster->isReadOnlyCommand("ZADD"));
}
public function testCredisClientInstancesInConstructor()
{
$this->tearDown();
$two = new Credis_Client($this->redisConfig[1]['host'], $this->redisConfig[1]['port']);
$three = new Credis_Client($this->redisConfig[2]['host'], $this->redisConfig[2]['port']);
$four = new Credis_Client($this->redisConfig[3]['host'], $this->redisConfig[3]['port']);
$this->cluster = new Credis_Cluster(array($two,$three,$four),2,$this->useStandalone);
$this->assertTrue($this->cluster->set('key','value'));
$this->assertEquals('value',$this->cluster->get('key'));
$this->setExpectedExceptionShim('CredisException','Server should either be an array or an instance of Credis_Client');
new Credis_Cluster(array(new stdClass()),2,$this->useStandalone);
}
public function testSetMasterClient()
{
$this->tearDown();
$master = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port']);
$slave = new Credis_Client($this->redisConfig[6]['host'], $this->redisConfig[6]['port']);
$this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
$this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master));
$this->assertCount(2,$this->cluster->clients());
$this->assertEquals($this->redisConfig[6]['port'], $this->cluster->client(0)->getPort());
$this->assertEquals($this->redisConfig[0]['port'], $this->cluster->client('master')->getPort());
$this->cluster = new Credis_Cluster(array($this->redisConfig[0]), 2, $this->useStandalone);
$this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient(new Credis_Client($this->redisConfig[1]['host'], $this->redisConfig[1]['port'])));
$this->assertEquals($this->redisConfig[0]['port'], $this->cluster->client('master')->getPort());
$this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
$this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master,true));
$this->assertCount(1,$this->cluster->clients());
}
}