Skip to content

Commit 82c1c89

Browse files
committed
Fixed bug.
1 parent ff0b1e6 commit 82c1c89

File tree

4 files changed

+90
-8
lines changed

4 files changed

+90
-8
lines changed

tests/RedisTest.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
use Hyperf\Config\Config;
1616
use Hyperf\Contract\ConfigInterface;
1717
use Hyperf\Di\Container;
18+
use Hyperf\Pool\Channel;
19+
use Hyperf\Pool\PoolOption;
20+
use Hyperf\Redis\Frequency;
1821
use Hyperf\Redis\Pool\PoolFactory;
1922
use Hyperf\Redis\Pool\RedisPool;
2023
use Hyperf\Redis\Redis;
2124
use Hyperf\Utils\ApplicationContext;
25+
use Hyperf\Utils\Context;
26+
use HyperfTest\Redis\Stub\RedisPoolFailedStub;
2227
use HyperfTest\Redis\Stub\RedisPoolStub;
2328
use Mockery;
2429
use PHPUnit\Framework\TestCase;
@@ -32,6 +37,7 @@ class RedisTest extends TestCase
3237
public function tearDown()
3338
{
3439
Mockery::close();
40+
Context::set('redis.connection.default', null);
3541
}
3642

3743
public function testRedisConnect()
@@ -68,9 +74,38 @@ public function testRedisSelect()
6874
$this->assertSame('db:0 name:get argument:xxxx', $res[0]);
6975
}
7076

77+
public function testRedisReuseAfterThrowable()
78+
{
79+
$container = $this->getContainer();
80+
$pool = new RedisPoolFailedStub($container, 'default');
81+
$container->shouldReceive('make')->once()->with(RedisPool::class, ['name' => 'default'])->andReturn($pool);
82+
$factory = new PoolFactory($container);
83+
$redis = new Redis($factory);
84+
try {
85+
$redis->set('xxxx', 'yyyy');
86+
} catch (\Throwable $exception) {
87+
$this->assertSame('Get connection failed.', $exception->getMessage());
88+
}
89+
90+
$this->assertSame(1, $pool->getConnectionsInChannel());
91+
$this->assertSame(1, $pool->getCurrentConnections());
92+
}
93+
7194
private function getRedis()
95+
{
96+
$container = $this->getContainer();
97+
$pool = new RedisPoolStub($container, 'default');
98+
$container->shouldReceive('make')->once()->with(RedisPool::class, ['name' => 'default'])->andReturn($pool);
99+
$factory = new PoolFactory($container);
100+
101+
return new Redis($factory);
102+
}
103+
104+
private function getContainer()
72105
{
73106
$container = Mockery::mock(Container::class);
107+
ApplicationContext::setContainer($container);
108+
74109
$container->shouldReceive('get')->once()->with(ConfigInterface::class)->andReturn(new Config([
75110
'redis' => [
76111
'default' => [
@@ -89,13 +124,13 @@ private function getRedis()
89124
],
90125
],
91126
]));
92-
$pool = new RedisPoolStub($container, 'default');
93-
$container->shouldReceive('make')->once()->with(RedisPool::class, ['name' => 'default'])->andReturn($pool);
94-
95-
ApplicationContext::setContainer($container);
96-
97-
$factory = new PoolFactory($container);
98-
99-
return new Redis($factory);
127+
$container->shouldReceive('make')->with(Frequency::class, Mockery::any())->andReturn(new Frequency());
128+
$container->shouldReceive('make')->with(PoolOption::class, Mockery::any())->andReturnUsing(function ($class, $args) {
129+
return new PoolOption(...array_values($args));
130+
});
131+
$container->shouldReceive('make')->with(Channel::class, Mockery::any())->andReturnUsing(function ($class, $args) {
132+
return new Channel($args['size']);
133+
});
134+
return $container;
100135
}
101136
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://doc.hyperf.io
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Redis\Stub;
14+
15+
class RedisConnectionFailedStub extends RedisConnectionStub
16+
{
17+
public function getConnection()
18+
{
19+
throw new \Exception('Get connection failed.');
20+
}
21+
}

tests/Stub/RedisConnectionStub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function reconnect(): bool
3939
$this->db = $this->config['db'];
4040
$this->timeout = $this->config['timeout'];
4141

42+
$this->lastUseTime = microtime(true);
43+
4244
return true;
4345
}
4446

tests/Stub/RedisPoolFailedStub.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://doc.hyperf.io
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Redis\Stub;
14+
15+
use Hyperf\Contract\ConnectionInterface;
16+
use Hyperf\Redis\Pool\RedisPool;
17+
18+
class RedisPoolFailedStub extends RedisPool
19+
{
20+
protected function createConnection(): ConnectionInterface
21+
{
22+
return new RedisConnectionFailedStub($this->container, $this, $this->config);
23+
}
24+
}

0 commit comments

Comments
 (0)