Skip to content

Commit 62a4c69

Browse files
committed
refactor: use pool factory contract in PoolProxy
1 parent 369f23a commit 62a4c69

File tree

12 files changed

+82
-43
lines changed

12 files changed

+82
-43
lines changed

src/object-pool/src/Contracts/Factory.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ interface Factory
99
/**
1010
* Get a managed pool by name.
1111
*/
12-
public function getPool(string $name): ObjectPool;
12+
public function get(string $name): ObjectPool;
1313

1414
/**
1515
* Create and register a new object pool.
1616
*/
17-
public function createPool(string $name, callable $callback, array $options = []): ObjectPool;
17+
public function create(string $name, callable $callback, array $options = []): ObjectPool;
1818

1919
/**
2020
* Get all registered pools.
@@ -24,5 +24,10 @@ public function pools(): array;
2424
/**
2525
* Check if a pool exists.
2626
*/
27-
public function hasPool(string $name): bool;
27+
public function has(string $name): bool;
28+
29+
/**
30+
* Remove a pool from the manager.
31+
*/
32+
public function remove(string $name): static;
2833
}

src/object-pool/src/ObjectRecycler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function stop(): void
113113
*/
114114
public function getLastRecycledAt(string $name): null|DateTime|int
115115
{
116-
return $this->manager->getPool($name)
116+
return $this->manager->get($name)
117117
->getLastRecycledAt();
118118
}
119119

src/object-pool/src/PoolManager.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(
2929
/**
3030
* Get a managed pool by name.
3131
*/
32-
public function getPool(string $name): ObjectPool
32+
public function get(string $name): ObjectPool
3333
{
3434
if (! $pool = $this->pools[$name] ?? null) {
3535
throw new RuntimeException("The pool name `{$name}` does not exist.");
@@ -41,7 +41,7 @@ public function getPool(string $name): ObjectPool
4141
/**
4242
* Create and register a new object pool.
4343
*/
44-
public function createPool(string $name, callable $callback, array $options = []): ObjectPool
44+
public function create(string $name, callable $callback, array $options = []): ObjectPool
4545
{
4646
if (isset($this->pools[$name])) {
4747
throw new RuntimeException("The pool name `{$name}` already exists.");
@@ -67,7 +67,7 @@ public function pools(): array
6767
/**
6868
* Set a pool to the manager.
6969
*/
70-
public function setPool(string $name, ObjectPool $pool): static
70+
public function set(string $name, ObjectPool $pool): static
7171
{
7272
$this->pools[$name] = $pool;
7373

@@ -80,7 +80,7 @@ public function setPool(string $name, ObjectPool $pool): static
8080
public function setPools(array $pools): static
8181
{
8282
foreach ($pools as $name => $pool) {
83-
$this->setPool($name, $pool);
83+
$this->set($name, $pool);
8484
}
8585

8686
return $this;
@@ -89,15 +89,15 @@ public function setPools(array $pools): static
8989
/**
9090
* Check if a pool exists.
9191
*/
92-
public function hasPool(string $name): bool
92+
public function has(string $name): bool
9393
{
9494
return isset($this->pools[$name]);
9595
}
9696

9797
/**
9898
* Remove a pool from the manager.
9999
*/
100-
public function removePool(string $name): static
100+
public function remove(string $name): static
101101
{
102102
unset($this->pools[$name]);
103103

src/object-pool/src/PoolProxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use Hyperf\Context\ApplicationContext;
9+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
910
use Hypervel\ObjectPool\Contracts\ObjectPool;
1011

1112
class PoolProxy
@@ -30,8 +31,8 @@ public function __construct(
3031
protected ?Closure $releaseCallback = null,
3132
) {
3233
$this->pool = ApplicationContext::getContainer()
33-
->get(PoolManager::class)
34-
->createPool(
34+
->get(PoolFactory::class)
35+
->create(
3536
$this->name,
3637
$this->resolver,
3738
$this->options

tests/Filesystem/FilesystemManagerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Hypervel\Filesystem\Contracts\Filesystem;
1414
use Hypervel\Filesystem\FilesystemManager;
1515
use Hypervel\Filesystem\FilesystemPoolProxy;
16+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
17+
use Hypervel\ObjectPool\PoolManager;
1618
use InvalidArgumentException;
1719
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
1820
use PHPUnit\Framework\TestCase;
@@ -197,6 +199,7 @@ protected function getContainer(array $config = []): ContainerInterface
197199
return new Container(
198200
new DefinitionSource([
199201
ConfigInterface::class => fn () => $config,
202+
PoolFactory::class => PoolManager::class,
200203
])
201204
);
202205
}

tests/HttpClient/HttpClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
use Hypervel\HttpClient\RequestException;
2929
use Hypervel\HttpClient\Response;
3030
use Hypervel\HttpClient\ResponseSequence;
31+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
32+
use Hypervel\ObjectPool\PoolManager;
3133
use Hypervel\Support\Arr;
3234
use Hypervel\Support\Carbon;
3335
use Hypervel\Support\Collection;
@@ -3217,6 +3219,7 @@ protected function getContainer(array $config = []): ContainerInterface
32173219
return new \Hyperf\Di\Container(
32183220
new \Hyperf\Di\Definition\DefinitionSource([
32193221
ConfigInterface::class => fn () => $config,
3222+
PoolFactory::class => PoolManager::class,
32203223
])
32213224
);
32223225
}

tests/Mail/MailManagerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Hyperf\ViewEngine\Contract\FactoryInterface as ViewFactory;
1313
use Hypervel\Mail\MailManager;
1414
use Hypervel\Mail\TransportPoolProxy;
15+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
16+
use Hypervel\ObjectPool\PoolManager;
1517
use InvalidArgumentException;
1618
use Mockery;
1719
use PHPUnit\Framework\TestCase;
@@ -100,6 +102,7 @@ protected function getContainer(): Container
100102
ConfigInterface::class => fn () => new Config([]),
101103
ViewFactory::class => fn () => Mockery::mock(ViewFactory::class),
102104
EventDispatcherInterface::class => fn () => Mockery::mock(EventDispatcherInterface::class),
105+
PoolFactory::class => PoolManager::class,
103106
])
104107
);
105108

tests/Mail/MailSesTransportTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Hyperf\ViewEngine\Contract\FactoryInterface as ViewFactory;
1616
use Hypervel\Mail\MailManager;
1717
use Hypervel\Mail\Transport\SesTransport;
18+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
19+
use Hypervel\ObjectPool\PoolManager;
1820
use Mockery as m;
1921
use PHPUnit\Framework\TestCase;
2022
use Psr\EventDispatcher\EventDispatcherInterface;
@@ -151,6 +153,7 @@ protected function mockContainer(): Container
151153
ConfigInterface::class => fn () => new Config([]),
152154
ViewFactory::class => fn () => m::mock(ViewFactory::class),
153155
EventDispatcherInterface::class => fn () => m::mock(EventDispatcherInterface::class),
156+
PoolFactory::class => PoolManager::class,
154157
])
155158
);
156159

tests/Notifications/NotificationChannelManagerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Hypervel\Notifications\Notification;
2020
use Hypervel\Notifications\NotificationPoolProxy;
2121
use Hypervel\Notifications\SendQueuedNotifications;
22+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
23+
use Hypervel\ObjectPool\PoolManager;
2224
use Hypervel\Queue\Contracts\ShouldQueue;
2325
use Mockery as m;
2426
use PHPUnit\Framework\TestCase;
@@ -133,6 +135,7 @@ protected function getContainer(): Container
133135
ConfigInterface::class => fn () => new Config([]),
134136
BusDispatcherContract::class => fn () => m::mock(BusDispatcherContract::class),
135137
EventDispatcherInterface::class => fn () => m::mock(EventDispatcherInterface::class),
138+
PoolFactory::class => PoolManager::class,
136139
])
137140
);
138141

tests/ObjectPool/PoolManagerTest.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace Hypervel\Tests\ObjectPool;
66

77
use Hyperf\Context\ApplicationContext;
8+
use Hyperf\Di\Container;
9+
use Hyperf\Di\Definition\DefinitionSource;
810
use Hypervel\Foundation\Testing\Concerns\RunTestsInCoroutine;
11+
use Hypervel\ObjectPool\Contracts\Factory as PoolFactory;
912
use Hypervel\ObjectPool\ObjectPool;
1013
use Hypervel\ObjectPool\PoolManager;
1114
use 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

140155
class Bar
141156
{
142-
public function tick()
157+
public function handle(): bool
143158
{
144-
return 1;
159+
return true;
145160
}
146161
}
147162

0 commit comments

Comments
 (0)