mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-04 04:37:46 +08:00
Removed dbChanged.
This commit is contained in:
parent
b40f2f6ba5
commit
fd4207899c
@ -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;
|
||||
}
|
||||
|
95
src/redis/tests/RedisConnectionTest.php
Normal file
95
src/redis/tests/RedisConnectionTest.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\Redis;
|
||||
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\Contract\ConfigInterface;
|
||||
use Hyperf\Di\Container;
|
||||
use HyperfTest\Redis\Stub\RedisPoolStub;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class RedisConnectionTest extends TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
public function testRedisConnectionConfig()
|
||||
{
|
||||
$pool = $this->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');
|
||||
}
|
||||
}
|
@ -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,
|
||||
],
|
||||
],
|
||||
],
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user