1515use Hyperf \Config \Config ;
1616use Hyperf \Contract \ConfigInterface ;
1717use Hyperf \Di \Container ;
18+ use Hyperf \Pool \Channel ;
19+ use Hyperf \Pool \PoolOption ;
20+ use Hyperf \Redis \Frequency ;
1821use Hyperf \Redis \Pool \PoolFactory ;
1922use Hyperf \Redis \Pool \RedisPool ;
2023use Hyperf \Redis \Redis ;
2124use Hyperf \Utils \ApplicationContext ;
25+ use Hyperf \Utils \Context ;
26+ use HyperfTest \Redis \Stub \RedisPoolFailedStub ;
2227use HyperfTest \Redis \Stub \RedisPoolStub ;
2328use Mockery ;
2429use 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}
0 commit comments