Skip to content

Commit 34381b9

Browse files
committed
Merge branch 'master' into 2.1-merge
# Conflicts: # README.md # src/amqp/src/Message/ConsumerMessageInterface.php # src/utils/src/Coroutine/Concurrent.php # src/websocket-server/src/Server.php
2 parents 7258e9a + 8bc8ce3 commit 34381b9

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

src/ConfigProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public function __invoke(): array
2525
__DIR__,
2626
],
2727
],
28-
'ignore_annotations' => [
29-
'mixin',
30-
],
3128
],
3229
'publish' => [
3330
[

src/RedisConnection.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,17 @@ public function setDatabase(?int $database): void
164164
protected function createRedisCluster()
165165
{
166166
try {
167-
$seeds = $this->config['cluster']['seeds'] ?? [];
168-
$name = $this->config['cluster']['name'] ?? null;
169-
$readTimeout = $this->config['cluster']['read_timeout'] ?? 0.0;
170-
$persistent = $this->config['cluster']['persistent'] ?? false;
171-
$timeout = $this->config['timeout'] ?? null;
172-
$auth = $this->config['auth'] ?? null;
173-
174-
$redis = new \RedisCluster($name, $seeds, $timeout, $readTimeout, $persistent, $auth);
167+
$paramaters = [];
168+
$paramaters[] = $this->config['cluster']['name'] ?? null;
169+
$paramaters[] = $this->config['cluster']['seeds'] ?? [];
170+
$paramaters[] = $this->config['timeout'] ?? 0.0;
171+
$paramaters[] = $this->config['cluster']['read_timeout'] ?? 0.0;
172+
$paramaters[] = $this->config['cluster']['persistent'] ?? false;
173+
if (isset($this->config['auth'])) {
174+
$paramaters[] = $this->config['auth'];
175+
}
176+
177+
$redis = new \RedisCluster(...$paramaters);
175178
} catch (\Throwable $e) {
176179
throw new ConnectionException('Connection reconnect failed ' . $e->getMessage());
177180
}

tests/RedisTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
use Hyperf\Contract\ConfigInterface;
1616
use Hyperf\Di\Container;
1717
use Hyperf\Pool\Channel;
18+
use Hyperf\Pool\Exception\ConnectionException;
1819
use Hyperf\Pool\PoolOption;
1920
use Hyperf\Redis\Frequency;
2021
use Hyperf\Redis\Pool\PoolFactory;
2122
use Hyperf\Redis\Pool\RedisPool;
2223
use Hyperf\Redis\Redis;
24+
use Hyperf\Redis\RedisProxy;
2325
use Hyperf\Utils\ApplicationContext;
2426
use Hyperf\Utils\Context;
2527
use Hyperf\Utils\Coroutine;
@@ -137,6 +139,39 @@ public function testRedisReuseAfterThrowable()
137139
$this->assertSame(1, $pool->getCurrentConnections());
138140
}
139141

142+
public function testRedisClusterConstructor()
143+
{
144+
$ref = new \ReflectionClass(\RedisCluster::class);
145+
$method = $ref->getMethod('__construct');
146+
$names = [
147+
'name', 'seeds', 'timeout', 'read_timeout', 'persistent', 'auth',
148+
];
149+
foreach ($method->getParameters() as $parameter) {
150+
$this->assertSame(array_shift($names), $parameter->getName());
151+
if ($parameter->getName() === 'seeds') {
152+
$this->assertSame('array', $parameter->getType()->getName());
153+
} else {
154+
$this->assertNull($parameter->getType());
155+
}
156+
}
157+
}
158+
159+
public function testNewRedisCluster()
160+
{
161+
try {
162+
$container = $this->getContainer();
163+
$pool = new RedisPoolStub($container, 'cluster1');
164+
$container->shouldReceive('make')->once()->with(RedisPool::class, ['name' => 'cluster1'])->andReturn($pool);
165+
$factory = new PoolFactory($container);
166+
$redis = new RedisProxy($factory, 'cluster1');
167+
$redis->get('test');
168+
$this->assertTrue(false);
169+
} catch (\Throwable $exception) {
170+
$this->assertInstanceOf(ConnectionException::class, $exception);
171+
$this->assertStringNotContainsString('RedisCluster::__construct() expects parameter', $exception->getMessage());
172+
}
173+
}
174+
140175
private function getRedis()
141176
{
142177
$container = $this->getContainer();
@@ -168,6 +203,17 @@ private function getContainer()
168203
'max_idle_time' => 60,
169204
],
170205
],
206+
'cluster1' => [
207+
'timeout' => 1.0,
208+
'auth' => null,
209+
'cluster' => [
210+
'enable' => true,
211+
'name' => 'mycluster',
212+
'seeds' => [],
213+
'read_timeout' => 1.0,
214+
'persistent' => false,
215+
],
216+
],
171217
],
172218
]));
173219
$container->shouldReceive('make')->with(Frequency::class, Mockery::any())->andReturn(new Frequency());

tests/Stub/RedisConnectionStub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function reconnect(): bool
5050

5151
$this->lastUseTime = microtime(true);
5252

53+
if ($this->config['cluster']['enable'] ?? false) {
54+
$this->createRedisCluster();
55+
}
56+
5357
return true;
5458
}
5559

0 commit comments

Comments
 (0)