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 8815639
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 240 deletions.
12 changes: 4 additions & 8 deletions composer.json
Expand Up @@ -16,17 +16,13 @@
"php": "^8.1"
},
"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
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
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 8815639

Please sign in to comment.