55namespace Hypervel \Tests \ObjectPool ;
66
77use Hyperf \Context \ApplicationContext ;
8+ use Hyperf \Di \Container ;
9+ use Hyperf \Di \Definition \DefinitionSource ;
810use Hypervel \Foundation \Testing \Concerns \RunTestsInCoroutine ;
11+ use Hypervel \ObjectPool \Contracts \Factory as PoolFactory ;
912use Hypervel \ObjectPool \ObjectPool ;
1013use Hypervel \ObjectPool \PoolManager ;
1114use Hypervel \ObjectPool \PoolProxy ;
@@ -39,44 +42,44 @@ protected function setUp(): void
3942 $ this ->container = $ container ;
4043 }
4144
42- public function testGetCreatesNewPoolIfNotExists ()
45+ public function testCreateNewPoolIfNotExists ()
4346 {
4447 $ this ->manager = new PoolManager ($ this ->container );
4548 $ name = 'test-pool ' ;
4649 $ callback = fn () => new Bar ();
4750
48- $ pool = $ this ->manager ->createPool ($ name , $ callback );
51+ $ pool = $ this ->manager ->create ($ name , $ callback );
4952
5053 $ this ->assertInstanceOf (ObjectPool::class, $ pool );
51- $ this ->assertTrue ($ this ->manager ->hasPool ($ name ));
54+ $ this ->assertTrue ($ this ->manager ->has ($ name ));
5255 $ this ->assertSame ($ pool , $ this ->manager ->pools ()[$ name ]);
5356 }
5457
55- public function testCreatePoolThrowsExceptionIfExisted ()
58+ public function testCreateThrowsExceptionIfExisted ()
5659 {
5760 $ this ->manager = new PoolManager ($ this ->container );
5861 $ name = 'duplicate-test-pool ' ;
5962 $ callback = fn () => new Bar ();
6063
61- $ this ->manager ->createPool ($ name , $ callback );
64+ $ this ->manager ->create ($ name , $ callback );
6265
6366 $ this ->expectException (RuntimeException::class);
6467 $ this ->expectExceptionMessage ("The pool name ` {$ name }` already exists. " );
6568
66- $ this ->manager ->createPool ($ name , $ callback );
69+ $ this ->manager ->create ($ name , $ callback );
6770 }
6871
69- public function testHasPool ()
72+ public function testHas ()
7073 {
7174 $ this ->manager = new PoolManager ($ this ->container );
7275 $ name = 'test-pool ' ;
7376 $ callback = fn () => new Bar ();
7477
75- $ this ->assertFalse ($ this ->manager ->hasPool ($ name ));
78+ $ this ->assertFalse ($ this ->manager ->has ($ name ));
7679
77- $ this ->manager ->createPool ($ name , $ callback );
80+ $ this ->manager ->create ($ name , $ callback );
7881
79- $ this ->assertTrue ($ this ->manager ->hasPool ($ name ));
82+ $ this ->assertTrue ($ this ->manager ->has ($ name ));
8083 }
8184
8285 public function testRemovePool ()
@@ -85,20 +88,20 @@ public function testRemovePool()
8588 $ name = 'test-pool ' ;
8689 $ callback = fn () => new Bar ();
8790
88- $ this ->manager ->createPool ($ name , $ callback );
89- $ this ->assertTrue ($ this ->manager ->hasPool ($ name ));
91+ $ this ->manager ->create ($ name , $ callback );
92+ $ this ->assertTrue ($ this ->manager ->has ($ name ));
9093
91- $ this ->manager ->removePool ($ name );
94+ $ this ->manager ->remove ($ name );
9295
93- $ this ->assertFalse ($ this ->manager ->hasPool ($ name ));
96+ $ this ->assertFalse ($ this ->manager ->has ($ name ));
9497 $ this ->assertEmpty ($ this ->manager ->pools ());
9598 }
9699
97100 public function testFlush ()
98101 {
99102 $ this ->manager = new PoolManager ($ this ->container );
100- $ this ->manager ->createPool ('pool1 ' , fn () => new Bar ());
101- $ this ->manager ->createPool ('pool2 ' , fn () => new Bar ());
103+ $ this ->manager ->create ('pool1 ' , fn () => new Bar ());
104+ $ this ->manager ->create ('pool2 ' , fn () => new Bar ());
102105
103106 $ this ->assertCount (2 , $ this ->manager ->pools ());
104107
@@ -112,36 +115,48 @@ public function testGetPool()
112115 $ name = 'test-pool ' ;
113116 $ callback = fn () => new Bar ();
114117
115- $ pool = $ this ->manager ->createPool ($ name , $ callback );
118+ $ pool = $ this ->manager ->create ($ name , $ callback );
116119
117- $ this ->assertSame ($ pool , $ this ->manager ->getPool ($ name ));
120+ $ this ->assertSame ($ pool , $ this ->manager ->get ($ name ));
118121 }
119122
120123 public function testPoolProxyIntegration ()
121124 {
125+ $ this ->mockContainer ();
126+
122127 $ bar = new BarPoolProxy (
123128 BarPoolProxy::class . ':bar ' ,
124- fn () => new Bar (),
125- [
126- 'recycle_time ' => 10 ,
127- ]
129+ fn () => new Bar ()
128130 );
129131
130- $ this ->assertEquals ( 1 , $ bar ->tick ());
132+ $ this ->assertTrue ( $ bar ->handle ());
131133
132134 $ poolName = BarPoolProxy::class . ':bar ' ;
133- $ this ->assertTrue ($ this ->manager ->hasPool ($ poolName ));
135+ $ this ->assertTrue ($ this ->manager ->has ($ poolName ));
134136
135137 $ pool = $ this ->manager ->pools ()[$ poolName ];
136138 $ this ->assertGreaterThan (0 , $ pool ->getCurrentObjectNumber ());
137139 }
140+
141+ protected function mockContainer (): Container
142+ {
143+ $ container = new Container (
144+ new DefinitionSource ([
145+ PoolFactory::class => fn () => $ this ->manager ,
146+ ])
147+ );
148+
149+ ApplicationContext::setContainer ($ container );
150+
151+ return $ container ;
152+ }
138153}
139154
140155class Bar
141156{
142- public function tick ()
157+ public function handle (): bool
143158 {
144- return 1 ;
159+ return true ;
145160 }
146161}
147162
0 commit comments