Skip to content

Commit

Permalink
Removed explicit fiber support (because it's not needed).
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Oct 31, 2022
1 parent 922f333 commit 2152712
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 243 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ jobs:
fail-fast: false
matrix:
php:
- 8.1
- 8.2
- 8.1
- 8.0
dependencies:
- hi
- lo
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ spirit of the original.
Requirements
------------

- [PHP 8.1](http://php.net/)
- [PHP 8.0](http://php.net/)
- [Composer](https://getcomposer.org/)

Usage
Expand Down
14 changes: 5 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@
}
],
"require": {
"php": "^8.1"
"php": "^8"
},
"require-dev": {
"amphp/amp": "^3-beta.9",
"phpunit/phpunit": "^9.5",
"revolt/event-loop": "^0.2"
"phpunit/phpunit": "^9.5"
},
"autoload": {
"files": [
"src/retry.php"
],
"psr-4": {
"ScriptFUSION\\Retry\\": "src"
}
"src/retry.php",
"src/FailingTooHardException.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
18 changes: 3 additions & 15 deletions src/retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace ScriptFUSION\Retry;

use Amp\Future;

/**
* Tries the specified operation up to the specified number of times. If specified, the exception handler will be
* called immediately before retrying the operation. If the error handler returns false, the operation will not be
Expand All @@ -28,24 +26,14 @@ function retry(int $tries, callable $operation, callable $onError = null): mixed

try {
beginning:

if (($result = $operation()) instanceof Future) {
// Wait for Future to complete.
$result = $result->await();
}
$result = $operation();
} catch (\Exception $exception) {
if ($tries === ++$attempts) {
throw new FailingTooHardException($attempts, $exception);
}

if ($onError) {
if (($result = $onError($exception, $attempts, $tries)) instanceof Future) {
$result = $result->await();
}

if ($result === false) {
return null;
}
if ($onError && $onError($exception, $attempts, $tries) === false) {
return null;
}

goto beginning;
Expand Down
204 changes: 0 additions & 204 deletions test/RetryAsyncTest.php

This file was deleted.

46 changes: 33 additions & 13 deletions test/RetryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

final class RetryTest extends TestCase
{
public function testWithoutFailing()
/**
* Tests that when an operation is successful, its result is returned without retrying.
*/
public function testWithoutFailing(): void
{
$invocations = 0;

Expand All @@ -23,7 +26,10 @@ public function testWithoutFailing()
self::assertSame(5, $value);
}

public function testFailingOnce()
/**
* Tests that when an operation fails once, it is retried.
*/
public function testFailingOnce(): void
{
$invocations = 0;
$failed = false;
Expand All @@ -45,7 +51,10 @@ public function testFailingOnce()
self::assertSame(5, $value);
}

public function testZeroTries()
/**
* Tests that when an operation is attempted zero times, the operation is not invoked and returns null.
*/
public function testZeroTries(): void
{
$invocations = 0;

Expand All @@ -59,7 +68,10 @@ public function testZeroTries()
self::assertNull($value);
}

public function testFailingTooHard()
/**
* Tests that when an operation is retried the maximum number of tries, FailingTooHardException is thrown.
*/
public function testFailingTooHard(): void
{
$invocations = 0;
$outerException = $innerException = null;
Expand All @@ -79,10 +91,10 @@ public function testFailingTooHard()
}

/**
* Tests that an error callback receives the exception thrown by the operation, the current attempt and maximum
* number of attempts.
* Tests that when an exception is thrown by the operation, the error callback receives that exception, the current
* attempt index and maximum number of attempts.
*/
public function testErrorCallback()
public function testErrorCallback(): void
{
$invocations = $errors = 0;
$outerException = $innerException = null;
Expand All @@ -108,20 +120,28 @@ public function testErrorCallback()
}

/**
* Tests that an error handler that returns false aborts retrying.
* Tests that when an error handler returns false, retries are aborted.
*/
public function testErrorCallbackHalt()
public function testErrorCallbackHalt(): void
{
$invocations = 0;

retry($tries = 2, static function () use (&$invocations) {
retry(2, static function () use (&$invocations) {
++$invocations;

throw new \RuntimeException;
}, static function () {
return false;
});
}, fn () => false);

self::assertSame(1, $invocations);
}

/**
* Tests that when an exception handler throws an exception, the exception is not caught.
*/
public function testErrorCallbackCanThrow(): void
{
$this->expectExceptionObject($exception = new \LogicException);

retry(2, fn () => throw new \RuntimeException, fn () => throw $exception);
}
}

0 comments on commit 2152712

Please sign in to comment.