Repository navigation
Expand file tree
/
Copy pathPoolFactoryTest.php
More file actions
132 lines (115 loc) · 4.18 KB
/
Copy pathPoolFactoryTest.php
File metadata and controls
132 lines (115 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Redis;
use Hyperf\Config\Config;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Container;
use Hyperf\Pool\Channel;
use Hyperf\Pool\PoolOption;
use Hyperf\Redis\Frequency;
use Hyperf\Redis\Pool\PoolFactory;
use Hyperf\Redis\Pool\RedisPool;
use HyperfTest\Redis\Stub\RedisPoolStub;
use Mockery;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class PoolFactoryTest extends TestCase
{
protected function setUp(): void
{
Mockery::close();
}
protected function tearDown(): void
{
Mockery::close();
}
public function testFlushAll()
{
$container = $this->getContainer();
$factory = new PoolFactory($container);
$pool1 = $factory->getPool('default');
$pool2 = $factory->getPool('cache');
$conn1 = $pool1->get();
$conn2 = $pool1->get();
$conn3 = $pool2->get();
$pool1->release($conn1);
$pool1->release($conn2);
$pool2->release($conn3);
$this->assertSame(2, $pool1->getConnectionsInChannel());
$this->assertSame(1, $pool2->getConnectionsInChannel());
$factory->flushAll();
$this->assertSame(0, $pool1->getConnectionsInChannel());
$this->assertSame(0, $pool2->getConnectionsInChannel());
}
protected function getContainer()
{
$container = Mockery::mock(Container::class);
ApplicationContext::setContainer($container);
$container->shouldReceive('get')->with(ConfigInterface::class)->andReturn(new Config([
'redis' => [
'default' => [
'host' => 'localhost',
'port' => 6379,
'auth' => null,
'db' => 0,
'timeout' => 0.0,
'options' => [],
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => 60.0,
],
],
'cache' => [
'host' => 'localhost',
'port' => 6379,
'auth' => null,
'db' => 1,
'timeout' => 0.0,
'options' => [],
'pool' => [
'min_connections' => 1,
'max_connections' => 10,
'connect_timeout' => 10.0,
'wait_timeout' => 3.0,
'heartbeat' => -1,
'max_idle_time' => 60.0,
],
],
],
]));
$container->shouldReceive('has')->with(StdoutLoggerInterface::class)->andReturn(false);
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturn(false);
$container->shouldReceive('has')->with(Frequency::class)->andReturn(false);
$container->shouldReceive('make')->with(Channel::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new Channel($args['size']);
});
$container->shouldReceive('make')->with(PoolOption::class, Mockery::any())->andReturnUsing(function ($class, $args) {
return new PoolOption(...$args);
});
$container->shouldReceive('make')->with(Frequency::class, Mockery::any())->andReturn(new Frequency());
$container->shouldReceive('make')->with(RedisPool::class, Mockery::any())->andReturnUsing(function ($class, $args) use ($container) {
return new RedisPoolStub($container, $args['name']);
});
return $container;
}
}