diff --git a/src/redis/src/RedisConnection.php b/src/redis/src/RedisConnection.php index 910089dbd..1cef7a3bb 100644 --- a/src/redis/src/RedisConnection.php +++ b/src/redis/src/RedisConnection.php @@ -36,14 +36,9 @@ class RedisConnection extends BaseConnection implements ConnectionInterface 'timeout' => 0.0, ]; - /** - * @var bool - */ - protected $dbChanged = false; - /** * Current database. - * @var int + * @var null|int */ protected $database; @@ -116,7 +111,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface parent::release(); } - public function setDatabase(int $database): void + public function setDatabase($database): void { $this->database = $database; } diff --git a/src/redis/tests/RedisConnectionTest.php b/src/redis/tests/RedisConnectionTest.php new file mode 100644 index 000000000..0ea2074c9 --- /dev/null +++ b/src/redis/tests/RedisConnectionTest.php @@ -0,0 +1,95 @@ +getRedisPool(); + + $config = $pool->get()->getConfig(); + + $this->assertSame([ + 'host' => 'redis', + 'port' => 16379, + 'auth' => 'redis', + 'db' => 0, + 'timeout' => 0.0, + 'pool' => [ + 'min_connections' => 1, + 'max_connections' => 30, + 'connect_timeout' => 10.0, + 'wait_timeout' => 3.0, + 'heartbeat' => -1, + 'max_idle_time' => 1, + ], + ], $config); + } + + public function testRedisConnectionReconnect() + { + $pool = $this->getRedisPool(); + + $connection = $pool->get()->getConnection(); + $this->assertSame(null, $connection->getDatabase()); + + $connection->setDatabase(2); + $connection->reconnect(); + $this->assertSame(2, $connection->getDatabase()); + + $connection->release(); + $connection = $pool->get()->getConnection(); + $this->assertSame(null, $connection->getDatabase()); + } + + private function getRedisPool() + { + $container = Mockery::mock(Container::class); + $container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([ + 'redis' => [ + 'default' => [ + 'host' => 'redis', + 'auth' => 'redis', + 'port' => 16379, + 'pool' => [ + 'min_connections' => 1, + 'max_connections' => 30, + 'connect_timeout' => 10.0, + 'wait_timeout' => 3.0, + 'heartbeat' => -1, + 'max_idle_time' => 1, + ], + ], + ], + ])); + + return new RedisPoolStub($container, 'default'); + } +} diff --git a/src/redis/tests/RedisTest.php b/src/redis/tests/RedisTest.php index 54577edd1..565e90540 100644 --- a/src/redis/tests/RedisTest.php +++ b/src/redis/tests/RedisTest.php @@ -59,10 +59,13 @@ class RedisTest extends TestCase $res = $redis->get('xxxx'); $this->assertSame('db:2 name:get argument:xxxx', $res); - parallel([function () use ($redis) { - $res = $redis->get('xxxx'); - $this->assertSame('db:0 name:get argument:xxxx', $res); + $this->assertSame(2, $redis->getDatabase()); + + $res = parallel([function () use ($redis) { + return $redis->get('xxxx'); }]); + + $this->assertSame('db:0 name:get argument:xxxx', $res[0]); } private function getRedis() @@ -81,7 +84,7 @@ class RedisTest extends TestCase 'connect_timeout' => 10.0, 'wait_timeout' => 3.0, 'heartbeat' => -1, - 'max_idle_time' => 1, + 'max_idle_time' => 60, ], ], ], diff --git a/src/redis/tests/Stub/RedisConnectionStub.php b/src/redis/tests/Stub/RedisConnectionStub.php index ab91e35e5..b0964b3d4 100644 --- a/src/redis/tests/Stub/RedisConnectionStub.php +++ b/src/redis/tests/Stub/RedisConnectionStub.php @@ -33,22 +33,33 @@ class RedisConnectionStub extends RedisConnection public function reconnect(): bool { - $this->host = $this->config['host'] ?? 'localhost'; - $this->port = $this->config['port'] ?? 6379; - $this->auth = $this->config['auth'] ?? null; - $this->db = $this->config['db'] ?? 0; - $this->timeout = $this->config['timeout'] ?? 0.0; + $this->host = $this->config['host']; + $this->port = $this->config['port']; + $this->auth = $this->config['auth']; + $this->db = $this->config['db']; + $this->timeout = $this->config['timeout']; return true; } - public function getConnection() - { - return parent::getConnection(); - } - public function select($db) { $this->db = $db; } + + /** + * @return array + */ + public function getConfig(): array + { + return $this->config; + } + + /** + * @return null|int + */ + public function getDatabase() + { + return $this->database; + } }