Skip to content

Commit 6baa0b5

Browse files
committed
v160rc2
1 parent ee57467 commit 6baa0b5

File tree

4 files changed

+46
-33
lines changed

4 files changed

+46
-33
lines changed

phpunit.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
<php>
3636
<const name="TEST_REDIS_SERVER_2x6_1" value="127.0.0.1:6381" />
3737
<const name="TEST_REDIS_SERVER_2x6_2" value="127.0.0.1:6382" />
38-
<const name="TEST_REDIS_SERVER_2x8_1" value="127.0.0.1:6383" />
39-
<const name="TEST_REDIS_SERVER_2x8_2" value="127.0.0.1:6384" />
38+
<const name="TEST_REDIS_SERVER_2x8_1" value="localhost:6383" />
39+
<const name="TEST_REDIS_SERVER_2x8_2" value="localhost:6384" />
4040
<const name="TEST_REDIS_SERVER_3x0_1" value="127.0.0.1:6385" />
4141
<const name="TEST_REDIS_SERVER_3x0_2" value="127.0.0.1:6386" />
42-
<const name="TEST_REDIS_SERVER_3x2_1" value="127.0.0.1:6387" />
43-
<const name="TEST_REDIS_SERVER_3x2_2" value="127.0.0.1:6388" />
42+
<const name="TEST_REDIS_SERVER_3x2_1" value="localhost:6387" />
43+
<const name="TEST_REDIS_SERVER_3x2_2" value="localhost:6388" />
4444
<const name="TEST_REDIS_SERVER_4x0_1" value="127.0.0.1:6389" />
4545
<const name="TEST_REDIS_SERVER_4x0_2" value="127.0.0.1:6390" />
4646

src/RedisClient/Client/AbstractRedisClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ protected function getConfig($param = null) {
108108
protected function getProtocol() {
109109
if (!$this->Protocol) {
110110
$this->Protocol = ProtocolFactory::createRedisProtocol($this, $this->getConfig());
111+
if ($this->ClusterMap) {
112+
$this->ClusterMap->addProtocol($this->Protocol);
113+
}
111114
}
112115
return $this->Protocol;
113116
}
@@ -198,7 +201,7 @@ protected function getProtocolByKey($keys) {
198201
if (isset($keys) && $this->ClusterMap) {
199202
$key = is_array($keys) ? $keys[0] : $keys;
200203
if ($Protocol = $this->ClusterMap->getProtocolByKey($key)) {
201-
$this->Protocol = $Protocol;
204+
return $this->Protocol = $Protocol;
202205
}
203206
}
204207
return $this->getProtocol();

src/RedisClient/Cluster/ClusterMap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ public function getProtocolByServer($server) {
119119
return $this->protocols[$server];
120120
}
121121

122+
/**
123+
* @param ProtocolInterface $Protocol
124+
*/
125+
public function addProtocol(ProtocolInterface $Protocol) {
126+
$server = $Protocol->getConnection()->getServer();
127+
if (0 === strpos($server, 'tcp://')) {
128+
$server = substr($server, 6);
129+
}
130+
$this->protocols[$server] = $Protocol;
131+
}
132+
122133
/**
123134
* @param $key
124135
* @return ProtocolInterface|null

tests/Unit/Client/AbstractRedisClientIsolatedTest.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
include_once(__DIR__ . '/../GlobalFunctionMock.php');
1414

1515
use RedisClient\Client\AbstractRedisClient;
16-
use RedisClient\Cluster\ClusterMap;
1716
use RedisClient\Exception\MovedResponseException;
1817
use RedisClient\RedisClient;
1918
use Test\Unit\GlobalFunctionMock;
@@ -86,9 +85,9 @@ public function test_ClusterEmptyMovedErrorResponse() {
8685
GlobalFunctionMock::mockFunction('RedisClient\Connection::fwrite', function($h, $m, $c) {
8786
static $data = [
8887
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
89-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
90-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo1\r\n"],
91-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
88+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
89+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo1\r\n"],
90+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
9291
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
9392
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
9493
];
@@ -100,7 +99,7 @@ public function test_ClusterEmptyMovedErrorResponse() {
10099

101100
GlobalFunctionMock::mockFunction('RedisClient\Connection::fgets', function() {
102101
static $data = [
103-
"-MOVED 12182 127.0.0.1:7003\r\n",
102+
"-MOVED 12182 127.0.0.3:7003\r\n",
104103
"\$3\r\n",
105104
"\$4\r\n",
106105
"-MOVED 1044 127.0.0.1:7001\r\n",
@@ -135,16 +134,16 @@ public function test_ClusterEmptyMovedErrorResponse() {
135134
$this->assertSame('bar2', $Redis->get('foo2'));
136135
$this->assertSame('bar3', $Redis->get('foo2'));
137136

138-
$this->assertSame(3, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_socket_client'));
139-
$this->assertSame(3, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_set_timeout'));
137+
$this->assertSame(2, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_socket_client'));
138+
$this->assertSame(2, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_set_timeout'));
140139
$this->assertSame(6, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fwrite'));
141140
$this->assertSame(6, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fgets'));
142141
$this->assertSame(4, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fread'));
143142
}
144143

145144
public function test_ClusterFullMovedErrorResponse() {
146145
GlobalFunctionMock::mockFunction('RedisClient\Connection::fwrite', function($h, $m, $c) {
147-
$this->assertSame('tcp://127.0.0.1:7003', $h);
146+
$this->assertSame('tcp://127.0.0.3:7003', $h);
148147
$this->assertSame("*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n", $m);
149148
$this->assertSame(22, $c);
150149
return $c;
@@ -164,8 +163,8 @@ public function test_ClusterFullMovedErrorResponse() {
164163
'enabled' => true,
165164
'clusters' => [
166165
5460 => '127.0.0.1:7001',
167-
10922 => '127.0.0.1:7002',
168-
16383 => '127.0.0.1:7003',
166+
10922 => '127.0.0.2:7002',
167+
16383 => '127.0.0.3:7003',
169168
]
170169
]
171170
]);
@@ -183,19 +182,19 @@ public function test_ClusterFullAskErrorResponse() {
183182
GlobalFunctionMock::mockFunction('RedisClient\Connection::fwrite', function($h, $m, $c) {
184183
static $data = [
185184
[
186-
'tcp://127.0.0.1:7003',
185+
'tcp://127.0.0.3:7003',
187186
"*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n",
188187
],
189188
[
190-
'tcp://127.0.0.1:7002',
189+
'tcp://127.0.0.2:7002',
191190
"*1\r\n\$6\r\nASKING\r\n",
192191
],
193192
[
194-
'tcp://127.0.0.1:7002',
193+
'tcp://127.0.0.2:7002',
195194
"*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n",
196195
],
197196
[
198-
'tcp://127.0.0.1:7003',
197+
'tcp://127.0.0.3:7003',
199198
"*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n",
200199
],
201200
];
@@ -207,7 +206,7 @@ public function test_ClusterFullAskErrorResponse() {
207206

208207
GlobalFunctionMock::mockFunction('RedisClient\Connection::fgets', function() {
209208
static $data = [
210-
"-ASK 12182 127.0.0.1:7002\r\n",
209+
"-ASK 12182 127.0.0.2:7002\r\n",
211210
"+OK\r\n",
212211
"\$3\r\n",
213212
"\$7\r\n",
@@ -229,8 +228,8 @@ public function test_ClusterFullAskErrorResponse() {
229228
'enabled' => true,
230229
'clusters' => [
231230
5460 => '127.0.0.1:7001',
232-
10922 => '127.0.0.1:7002',
233-
16383 => '127.0.0.1:7003',
231+
10922 => '127.0.0.2:7002',
232+
16383 => '127.0.0.3:7003',
234233
]
235234
]
236235
]);
@@ -249,15 +248,15 @@ public function test_ClusterEmptyMixResponse() {
249248
GlobalFunctionMock::mockFunction('RedisClient\Connection::fwrite', function($h, $m, $c) {
250249
static $data = [
251250
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
252-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
253-
['tcp://127.0.0.1:7002', "*1\r\n\$6\r\nASKING\r\n"],
254-
['tcp://127.0.0.1:7002', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
255-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
256-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
251+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
252+
['tcp://127.0.0.2:7002', "*1\r\n\$6\r\nASKING\r\n"],
253+
['tcp://127.0.0.2:7002', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
254+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
255+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
257256
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
258-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
257+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$3\r\nfoo\r\n"],
259258
['tcp://127.0.0.1:7001', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo2\r\n"],
260-
['tcp://127.0.0.1:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo1\r\n"],
259+
['tcp://127.0.0.3:7003', "*2\r\n\$3\r\nGET\r\n\$4\r\nfoo1\r\n"],
261260
];
262261
$datum = array_shift($data);
263262
$this->assertSame($datum[0], $h);
@@ -267,8 +266,8 @@ public function test_ClusterEmptyMixResponse() {
267266

268267
GlobalFunctionMock::mockFunction('RedisClient\Connection::fgets', function() {
269268
static $data = [
270-
"-MOVED 12182 127.0.0.1:7003\r\n",
271-
"-ASK 12182 127.0.0.1:7002\r\n",
269+
"-MOVED 12182 127.0.0.3:7003\r\n",
270+
"-ASK 12182 127.0.0.2:7002\r\n",
272271
"+OK\r\n",
273272
"\$3\r\n",
274273
"\$4\r\n",
@@ -310,8 +309,8 @@ public function test_ClusterEmptyMixResponse() {
310309
$this->assertSame('bar4', $Redis->get('foo2'));
311310
$this->assertSame('bar5', $Redis->get('foo1'));
312311

313-
$this->assertSame(4, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_socket_client'));
314-
$this->assertSame(4, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_set_timeout'));
312+
$this->assertSame(3, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_socket_client'));
313+
$this->assertSame(3, GlobalFunctionMock::getCountCalls('RedisClient\Connection::stream_set_timeout'));
315314
$this->assertSame(10, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fwrite'));
316315
$this->assertSame(10, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fgets'));
317316
$this->assertSame(6, GlobalFunctionMock::getCountCalls('RedisClient\Connection::fread'));

0 commit comments

Comments
 (0)