Removed dbChanged.

This commit is contained in:
李铭昕 2019-07-02 18:43:23 +08:00
parent b40f2f6ba5
commit fd4207899c
4 changed files with 125 additions and 21 deletions

View File

@ -36,14 +36,9 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
'timeout' => 0.0, 'timeout' => 0.0,
]; ];
/**
* @var bool
*/
protected $dbChanged = false;
/** /**
* Current database. * Current database.
* @var int * @var null|int
*/ */
protected $database; protected $database;
@ -116,7 +111,7 @@ class RedisConnection extends BaseConnection implements ConnectionInterface
parent::release(); parent::release();
} }
public function setDatabase(int $database): void public function setDatabase($database): void
{ {
$this->database = $database; $this->database = $database;
} }

View 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');
}
}

View File

@ -59,10 +59,13 @@ class RedisTest extends TestCase
$res = $redis->get('xxxx'); $res = $redis->get('xxxx');
$this->assertSame('db:2 name:get argument:xxxx', $res); $this->assertSame('db:2 name:get argument:xxxx', $res);
parallel([function () use ($redis) { $this->assertSame(2, $redis->getDatabase());
$res = $redis->get('xxxx');
$this->assertSame('db:0 name:get argument:xxxx', $res); $res = parallel([function () use ($redis) {
return $redis->get('xxxx');
}]); }]);
$this->assertSame('db:0 name:get argument:xxxx', $res[0]);
} }
private function getRedis() private function getRedis()
@ -81,7 +84,7 @@ class RedisTest extends TestCase
'connect_timeout' => 10.0, 'connect_timeout' => 10.0,
'wait_timeout' => 3.0, 'wait_timeout' => 3.0,
'heartbeat' => -1, 'heartbeat' => -1,
'max_idle_time' => 1, 'max_idle_time' => 60,
], ],
], ],
], ],

View File

@ -33,22 +33,33 @@ class RedisConnectionStub extends RedisConnection
public function reconnect(): bool public function reconnect(): bool
{ {
$this->host = $this->config['host'] ?? 'localhost'; $this->host = $this->config['host'];
$this->port = $this->config['port'] ?? 6379; $this->port = $this->config['port'];
$this->auth = $this->config['auth'] ?? null; $this->auth = $this->config['auth'];
$this->db = $this->config['db'] ?? 0; $this->db = $this->config['db'];
$this->timeout = $this->config['timeout'] ?? 0.0; $this->timeout = $this->config['timeout'];
return true; return true;
} }
public function getConnection()
{
return parent::getConnection();
}
public function select($db) public function select($db)
{ {
$this->db = $db; $this->db = $db;
} }
/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}
/**
* @return null|int
*/
public function getDatabase()
{
return $this->database;
}
} }