Skip to content

Commit c32a92a

Browse files
linting
1 parent 05d27a9 commit c32a92a

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/Pools/Pool.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Utopia\Pools;
44

55
use Exception;
6+
use Throwable;
67
use Utopia\Pools\Adapter as PoolAdapter;
78
use Utopia\Telemetry\Adapter as Telemetry;
89
use Utopia\Telemetry\Adapter\None as NoTelemetry;
@@ -225,12 +226,15 @@ public function setTelemetry(Telemetry $telemetry): static
225226
*/
226227
public function use(callable $callback, int $retries = 0): mixed
227228
{
229+
/**
230+
* @var Throwable $lastException
231+
*/
228232
$lastException = null;
229-
233+
230234
for ($attempt = 0; $attempt <= $retries; $attempt++) {
231235
$start = microtime(true);
232236
$connection = null;
233-
237+
234238
try {
235239
$connection = $this->pop();
236240
$result = $callback($connection->getResource());

tests/Pools/Scopes/PoolTestScope.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,18 +380,18 @@ public function testPoolUseWithRetrySuccess(): void
380380
$attempts = 0;
381381
$result = $pool->use(function ($resource) use (&$attempts) {
382382
$attempts++;
383-
383+
384384
// Fail on first two attempts, succeed on third
385385
if ($attempts < 3) {
386386
throw new Exception("Simulated connection failure");
387387
}
388-
388+
389389
return "success: {$resource}";
390390
}, 3); // Allow up to 3 retries (4 total attempts)
391391

392392
$this->assertEquals(3, $attempts);
393393
$this->assertEquals("success: connection-3", $result);
394-
394+
395395
// Pool should have connections available (destroyed failed ones, created new)
396396
$this->assertGreaterThan(0, $pool->count());
397397
});
@@ -400,15 +400,15 @@ public function testPoolUseWithRetrySuccess(): void
400400
$pool = new Pool($this->getAdapter(), 'testIntermittent', 5, fn () => 'resource');
401401

402402
$callCount = 0;
403-
403+
404404
$result = $pool->use(function ($resource) use (&$callCount) {
405405
$callCount++;
406-
406+
407407
// Fail on odd attempts, succeed on even
408408
if ($callCount % 2 === 1) {
409409
throw new Exception("Odd attempt failure");
410410
}
411-
411+
412412
return "success on attempt {$callCount}";
413413
}, 5); // Allow 5 retries
414414

@@ -423,14 +423,12 @@ public function testPoolUseWithRetryFailure(): void
423423
$pool = new Pool($this->getAdapter(), 'testRetryFail', 3, fn () => 'x');
424424

425425
$attempts = 0;
426-
426+
427427
try {
428428
$pool->use(function ($resource) use (&$attempts) {
429429
$attempts++;
430430
throw new Exception("Persistent failure");
431431
}, 2); // Allow up to 2 retries (3 total attempts)
432-
433-
$this->fail('Expected exception was not thrown');
434432
} catch (Exception $e) {
435433
$this->assertEquals("Persistent failure", $e->getMessage());
436434
$this->assertEquals(3, $attempts); // Should have tried 3 times (initial + 2 retries)
@@ -444,14 +442,12 @@ public function testPoolUseWithoutRetry(): void
444442
$pool = new Pool($this->getAdapter(), 'testNoRetry', 2, fn () => 'x');
445443

446444
$attempts = 0;
447-
445+
448446
try {
449447
$pool->use(function ($resource) use (&$attempts) {
450448
$attempts++;
451449
throw new Exception("First attempt failure");
452450
}); // No retries (default)
453-
454-
$this->fail('Expected exception was not thrown');
455451
} catch (Exception $e) {
456452
$this->assertEquals("First attempt failure", $e->getMessage());
457453
$this->assertEquals(1, $attempts); // Should only try once
@@ -470,23 +466,23 @@ public function testPoolUseRetryDestroysFailedConnections(): void
470466

471467
$attempts = 0;
472468
$seenResources = [];
473-
469+
474470
$pool->use(function ($resource) use (&$attempts, &$seenResources) {
475471
$attempts++;
476472
$seenResources[] = $resource;
477-
473+
478474
// Fail twice, succeed on third
479475
if ($attempts < 3) {
480476
throw new Exception("Connection failed");
481477
}
482-
478+
483479
return "success";
484480
}, 3);
485481

486482
// Should have created 3 connections (one for each attempt)
487483
$this->assertEquals(3, $i);
488484
$this->assertEquals(3, $attempts);
489-
485+
490486
// Each attempt should have gotten a different connection (failed ones were destroyed)
491487
$this->assertCount(3, array_unique($seenResources));
492488
$this->assertEquals(['connection-1', 'connection-2', 'connection-3'], $seenResources);

0 commit comments

Comments
 (0)