1515use Hyperf \Config \Config ;
1616use Hyperf \Contract \ConfigInterface ;
1717use Hyperf \Di \Container ;
18+ use Hyperf \Pool \Channel ;
19+ use Hyperf \Pool \LowFrequencyInterface ;
20+ use Hyperf \Pool \PoolOption ;
21+ use Hyperf \Redis \Frequency ;
22+ use Hyperf \Utils \ApplicationContext ;
1823use HyperfTest \Redis \Stub \RedisPoolStub ;
1924use Mockery ;
2025use PHPUnit \Framework \TestCase ;
@@ -70,10 +75,33 @@ public function testRedisConnectionReconnect()
7075 $ this ->assertSame (null , $ connection ->getDatabase ());
7176 }
7277
78+ public function testRedisCloseInLowFrequency ()
79+ {
80+ $ pool = $ this ->getRedisPool ();
81+
82+ $ connection1 = $ pool ->get ()->getConnection ();
83+ $ connection2 = $ pool ->get ()->getConnection ();
84+ $ connection3 = $ pool ->get ()->getConnection ();
85+
86+ $ this ->assertSame (3 , $ pool ->getCurrentConnections ());
87+
88+ $ connection1 ->release ();
89+ $ connection2 ->release ();
90+ $ connection3 ->release ();
91+
92+ $ this ->assertSame (3 , $ pool ->getCurrentConnections ());
93+
94+ $ connection = $ pool ->get ()->getConnection ();
95+
96+ $ this ->assertSame (1 , $ pool ->getCurrentConnections ());
97+
98+ $ connection ->release ();
99+ }
100+
73101 private function getRedisPool ()
74102 {
75103 $ container = Mockery::mock (Container::class);
76- $ container ->shouldReceive ('get ' )->once ()-> with (ConfigInterface::class)->andReturn (new Config ([
104+ $ container ->shouldReceive ('get ' )->with (ConfigInterface::class)->andReturn (new Config ([
77105 'redis ' => [
78106 'default ' => [
79107 'host ' => 'redis ' ,
@@ -91,6 +119,18 @@ private function getRedisPool()
91119 ],
92120 ]));
93121
122+ $ frequency = Mockery::mock (LowFrequencyInterface::class);
123+ $ frequency ->shouldReceive ('isLowFrequency ' )->andReturn (true );
124+ $ container ->shouldReceive ('make ' )->with (Frequency::class, Mockery::any ())->andReturn ($ frequency );
125+ $ container ->shouldReceive ('make ' )->with (PoolOption::class, Mockery::any ())->andReturnUsing (function ($ class , $ args ) {
126+ return new PoolOption (...array_values ($ args ));
127+ });
128+ $ container ->shouldReceive ('make ' )->with (Channel::class, Mockery::any ())->andReturnUsing (function ($ class , $ args ) {
129+ return new Channel ($ args ['size ' ]);
130+ });
131+
132+ ApplicationContext::setContainer ($ container );
133+
94134 return new RedisPoolStub ($ container , 'default ' );
95135 }
96136}
0 commit comments