Skip to content

Commit 2152712

Browse files
committed
Removed explicit fiber support (because it's not needed).
1 parent 922f333 commit 2152712

File tree

6 files changed

+44
-243
lines changed

6 files changed

+44
-243
lines changed

.github/workflows/Tests.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ jobs:
1515
fail-fast: false
1616
matrix:
1717
php:
18-
- 8.1
1918
- 8.2
19+
- 8.1
20+
- 8.0
2021
dependencies:
2122
- hi
2223
- lo

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spirit of the original.
1515
Requirements
1616
------------
1717

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

2121
Usage

composer.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@
1313
}
1414
],
1515
"require": {
16-
"php": "^8.1"
16+
"php": "^8"
1717
},
1818
"require-dev": {
19-
"amphp/amp": "^3-beta.9",
20-
"phpunit/phpunit": "^9.5",
21-
"revolt/event-loop": "^0.2"
19+
"phpunit/phpunit": "^9.5"
2220
},
2321
"autoload": {
2422
"files": [
25-
"src/retry.php"
26-
],
27-
"psr-4": {
28-
"ScriptFUSION\\Retry\\": "src"
29-
}
23+
"src/retry.php",
24+
"src/FailingTooHardException.php"
25+
]
3026
},
3127
"autoload-dev": {
3228
"psr-4": {

src/retry.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
namespace ScriptFUSION\Retry;
55

6-
use Amp\Future;
7-
86
/**
97
* Tries the specified operation up to the specified number of times. If specified, the exception handler will be
108
* called immediately before retrying the operation. If the error handler returns false, the operation will not be
@@ -28,24 +26,14 @@ function retry(int $tries, callable $operation, callable $onError = null): mixed
2826

2927
try {
3028
beginning:
31-
32-
if (($result = $operation()) instanceof Future) {
33-
// Wait for Future to complete.
34-
$result = $result->await();
35-
}
29+
$result = $operation();
3630
} catch (\Exception $exception) {
3731
if ($tries === ++$attempts) {
3832
throw new FailingTooHardException($attempts, $exception);
3933
}
4034

41-
if ($onError) {
42-
if (($result = $onError($exception, $attempts, $tries)) instanceof Future) {
43-
$result = $result->await();
44-
}
45-
46-
if ($result === false) {
47-
return null;
48-
}
35+
if ($onError && $onError($exception, $attempts, $tries) === false) {
36+
return null;
4937
}
5038

5139
goto beginning;

test/RetryAsyncTest.php

Lines changed: 0 additions & 204 deletions
This file was deleted.

test/RetryTest.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
final class RetryTest extends TestCase
1111
{
12-
public function testWithoutFailing()
12+
/**
13+
* Tests that when an operation is successful, its result is returned without retrying.
14+
*/
15+
public function testWithoutFailing(): void
1316
{
1417
$invocations = 0;
1518

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

26-
public function testFailingOnce()
29+
/**
30+
* Tests that when an operation fails once, it is retried.
31+
*/
32+
public function testFailingOnce(): void
2733
{
2834
$invocations = 0;
2935
$failed = false;
@@ -45,7 +51,10 @@ public function testFailingOnce()
4551
self::assertSame(5, $value);
4652
}
4753

48-
public function testZeroTries()
54+
/**
55+
* Tests that when an operation is attempted zero times, the operation is not invoked and returns null.
56+
*/
57+
public function testZeroTries(): void
4958
{
5059
$invocations = 0;
5160

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

62-
public function testFailingTooHard()
71+
/**
72+
* Tests that when an operation is retried the maximum number of tries, FailingTooHardException is thrown.
73+
*/
74+
public function testFailingTooHard(): void
6375
{
6476
$invocations = 0;
6577
$outerException = $innerException = null;
@@ -79,10 +91,10 @@ public function testFailingTooHard()
7991
}
8092

8193
/**
82-
* Tests that an error callback receives the exception thrown by the operation, the current attempt and maximum
83-
* number of attempts.
94+
* Tests that when an exception is thrown by the operation, the error callback receives that exception, the current
95+
* attempt index and maximum number of attempts.
8496
*/
85-
public function testErrorCallback()
97+
public function testErrorCallback(): void
8698
{
8799
$invocations = $errors = 0;
88100
$outerException = $innerException = null;
@@ -108,20 +120,28 @@ public function testErrorCallback()
108120
}
109121

110122
/**
111-
* Tests that an error handler that returns false aborts retrying.
123+
* Tests that when an error handler returns false, retries are aborted.
112124
*/
113-
public function testErrorCallbackHalt()
125+
public function testErrorCallbackHalt(): void
114126
{
115127
$invocations = 0;
116128

117-
retry($tries = 2, static function () use (&$invocations) {
129+
retry(2, static function () use (&$invocations) {
118130
++$invocations;
119131

120132
throw new \RuntimeException;
121-
}, static function () {
122-
return false;
123-
});
133+
}, fn () => false);
124134

125135
self::assertSame(1, $invocations);
126136
}
137+
138+
/**
139+
* Tests that when an exception handler throws an exception, the exception is not caught.
140+
*/
141+
public function testErrorCallbackCanThrow(): void
142+
{
143+
$this->expectExceptionObject($exception = new \LogicException);
144+
145+
retry(2, fn () => throw new \RuntimeException, fn () => throw $exception);
146+
}
127147
}

0 commit comments

Comments
 (0)