Skip to content

Commit

Permalink
Use separate class to retry HTTP client requests
Browse files Browse the repository at this point in the history
Set minimum test coverage to 100 percent
  • Loading branch information
rapkis committed Aug 28, 2023
1 parent f008fc9 commit b687792
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ jobs:
run: composer show -D

- name: Execute tests
run: XDEBUG_MODE=coverage php ./vendor/bin/pest --ci --coverage --do-not-cache-result
run: XDEBUG_MODE=coverage php ./vendor/bin/pest --ci --coverage --min=100 --do-not-cache-result
4 changes: 1 addition & 3 deletions src/Api/ControlDFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function make(): ControlD
->acceptJson()
->baseUrl($this->config->get('controld.url'))
->withToken($this->config->get('controld.secret'))
->retry(3, 250, function ($e) {
return $e instanceof ConnectionException || $e->getCode() >= 500;
});
->retry(3, 250, new RetryCallback());

foreach ($this->config->get('controld.middleware') ?? [] as $middleware) {
$this->request->withMiddleware(app($middleware));
Expand Down
16 changes: 16 additions & 0 deletions src/Api/RetryCallback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rapkis\Controld\Api;

use Illuminate\Http\Client\ConnectionException;
use Throwable;

class RetryCallback
{
public function __invoke(Throwable $exception): bool
{
return $exception instanceof ConnectionException || $exception->getCode() >= 500;
}
}
8 changes: 4 additions & 4 deletions tests/Api/ControlDFactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Illuminate\Config\Repository;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\PendingRequest;
use Rapkis\Controld\Api\ControlDFactory;
use Rapkis\Controld\Api\RetryCallback;
use Rapkis\Controld\Tests\Api\TestMiddleware;

it('creates an api client', function () {
Expand All @@ -29,9 +29,9 @@
->with($this->createStub(TestMiddleware::class))
->willReturnSelf();

$request->expects($this->once())->method('retry')->with(3, 250, function ($e) {
return $e instanceof ConnectionException || $e->getCode() >= 500;
})->willReturnSelf();
$request->expects($this->once())->method('retry')
->with(3, 250, new RetryCallback())
->willReturnSelf();

$factory->make();
});
17 changes: 17 additions & 0 deletions tests/Api/RetryCallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Http\Client\ConnectionException;
use Rapkis\Controld\Api\RetryCallback;

it('will retry for exception', function (Throwable $exception, bool $willRetry) {
$retry = new RetryCallback();

expect($retry($exception))->toBe($willRetry);
})->with([
[new Exception(), false],
[new Exception('', 500), true],
[new Exception('', 422), false],
[new ConnectionException(), true],
]);

0 comments on commit b687792

Please sign in to comment.