Skip to content

Commit 41c73fc

Browse files
committed
Added Fiber support in lieu of coroutines.
1 parent d2eefd1 commit 41c73fc

13 files changed

+47
-55
lines changed

.github/workflows/CI.yml .github/workflows/Tests.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
name: CI
1+
name: Tests
22

33
on:
44
push:
55
pull_request:
6+
workflow_dispatch:
67
schedule:
78
- cron: 0 6 * * *
89

@@ -14,9 +15,8 @@ jobs:
1415
fail-fast: false
1516
matrix:
1617
php:
17-
- 7.4
18-
- 8.0
1918
- 8.1
19+
- 8.2
2020
dependencies:
2121
- hi
2222
- lo
@@ -49,7 +49,7 @@ jobs:
4949

5050
- name: Upload test coverage
5151
run: |
52-
composer global require php-coveralls/php-coveralls:^2
52+
composer global require php-coveralls/php-coveralls
5353
php-coveralls -v
5454
env:
5555
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ Provides exception handlers for [Retry][Retry].
1212
Requirements
1313
------------
1414

15-
- [PHP 5.5](http://php.net/)
15+
- [PHP 8.1](http://php.net/)
1616
- [Composer](https://getcomposer.org/)
1717

1818

1919
[Releases]: https://github.com/ScriptFUSION/Retry-exception-handlers/releases
2020
[Version image]: https://poser.pugx.org/scriptfusion/retry-exception-handlers/v/stable "Latest version"
2121
[Downloads]: https://packagist.org/packages/scriptfusion/retry-exception-handlers
2222
[Downloads image]: https://poser.pugx.org/scriptfusion/retry-exception-handlers/downloads "Total downloads"
23-
[Build]: http://travis-ci.org/ScriptFUSION/Retry-exception-handlers
24-
[Build image]: https://travis-ci.org/ScriptFUSION/Retry-exception-handlers.svg "Build status"
23+
[Build]: https://github.com/ScriptFUSION/Retry-exception-handlers/actions/workflows/Tests.yaml
24+
[Build image]: https://github.com/ScriptFUSION/Retry-exception-handlers/actions/workflows/Tests.yaml/badge.svg "Build status"
2525
[Coverage]: https://coveralls.io/github/ScriptFUSION/Retry-exception-handlers
2626
[Coverage image]: https://coveralls.io/repos/ScriptFUSION/Retry-exception-handlers/badge.svg "Test coverage"
2727
[Style]: https://styleci.io/repos/76198855

composer.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
}
99
],
1010
"require": {
11-
"php": "^7.2|^8",
12-
"scriptfusion/retry": "^1|^2|^3"
11+
"php": "^8.1",
12+
"scriptfusion/retry": "^1|^2|^3|^4"
1313
},
1414
"require-dev": {
15-
"phpunit/phpunit": "^9.5",
16-
"amphp/amp": "^2.1"
15+
"amphp/amp": "^3-beta.9",
16+
"phpunit/phpunit": "^9.5.23",
17+
"revolt/event-loop": "^0.2"
1718
},
1819
"autoload": {
1920
"psr-4": {
@@ -27,5 +28,8 @@
2728
},
2829
"scripts": {
2930
"test": "phpunit -c test"
31+
},
32+
"config": {
33+
"sort-packages": true
3034
}
3135
}

src/AsyncExponentialBackoffExceptionHandler.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
*/
1313
class AsyncExponentialBackoffExceptionHandler extends AsyncMilliSleepExceptionHandler
1414
{
15-
const DEFAULT_COEFFICIENT = 102;
15+
public const DEFAULT_COEFFICIENT = 102;
1616

17-
private $millisecondCoefficient;
18-
19-
public function __construct(int $millisecondCoefficient = self::DEFAULT_COEFFICIENT)
17+
public function __construct(private readonly int $millisecondCoefficient = self::DEFAULT_COEFFICIENT)
2018
{
21-
parent::__construct($this->generateSequence($this->millisecondCoefficient = $millisecondCoefficient));
19+
parent::__construct($this->generateSequence($this->millisecondCoefficient));
2220
}
2321

2422
private function generateSequence($coefficient): \Generator

src/AsyncMilliSleepExceptionHandler.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33

44
namespace ScriptFUSION\Retry\ExceptionHandler;
55

6-
use Amp\Delayed;
6+
use function Amp\delay;
77

88
/**
99
* Delays the current asynchronous execution context for a series of millisecond delays on each invocation.
1010
*/
1111
class AsyncMilliSleepExceptionHandler
1212
{
13-
private $delays;
14-
15-
public function __construct(\Iterator $delays)
13+
public function __construct(private readonly \Iterator $delays)
1614
{
17-
$this->delays = $delays;
1815
}
1916

20-
public function __invoke()
17+
public function __invoke(): void
2118
{
2219
// TODO: Iterator validation. i.e. What happens when the iterator is no longer valid?
2320
$delay = $this->delays->current();
2421

2522
$this->delays->next();
2623

27-
return new Delayed($delay);
24+
delay($delay / 1000);
2825
}
2926
}

src/ExponentialBackoffExceptionHandler.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
*/
1313
class ExponentialBackoffExceptionHandler extends MicroSleepExceptionHandler
1414
{
15-
const DEFAULT_COEFFICIENT = 102000;
15+
public const DEFAULT_COEFFICIENT = 102000;
1616

17-
private $microTimeCoefficient;
18-
19-
public function __construct($microTimeCoefficient = self::DEFAULT_COEFFICIENT)
17+
public function __construct(private readonly int $microTimeCoefficient = self::DEFAULT_COEFFICIENT)
2018
{
21-
parent::__construct($this->generateSequence(
22-
$this->microTimeCoefficient = $microTimeCoefficient | 0
23-
));
19+
parent::__construct($this->generateSequence($this->microTimeCoefficient));
2420
}
2521

26-
private function generateSequence($coefficient): \Generator
22+
private function generateSequence(int $coefficient): \Generator
2723
{
2824
foreach (new PowersOfTwoSequence as $base) {
2925
yield $base * $coefficient;

src/MicroSleepExceptionHandler.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
*/
99
class MicroSleepExceptionHandler
1010
{
11-
private $delays;
12-
13-
public function __construct(\Iterator $delays)
11+
public function __construct(private readonly \Iterator $delays)
1412
{
15-
$this->delays = $delays;
1613
}
1714

18-
public function __invoke()
15+
public function __invoke(): void
1916
{
2017
usleep($this->delays->current());
2118

src/Sequence/PowersOfTwoSequence.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
final class PowersOfTwoSequence implements \IteratorAggregate
1212
{
13-
private $power = 0;
13+
private int $power = 0;
1414

1515
public function getIterator(): \Traversable
1616
{

test/Unit/AsyncExponentialBackoffExceptionHandlerTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55

66
use PHPUnit\Framework\TestCase;
77
use ScriptFUSION\Retry\ExceptionHandler\AsyncExponentialBackoffExceptionHandler;
8+
use function Amp\async;
89

910
/**
1011
* @see AsyncExponentialBackoffExceptionHandler
1112
*/
1213
final class AsyncExponentialBackoffExceptionHandlerTest extends TestCase
1314
{
14-
public function test()
15+
public function test(): void
1516
{
1617
$handler = new AsyncExponentialBackoffExceptionHandler;
1718

1819
$start = microtime(true);
1920

20-
\Amp\Loop::run(static function () use ($handler): \Generator {
21+
async(static function () use ($handler): void {
2122
for ($counter = 0; $counter < 4; ++$counter) {
22-
yield $handler();
23+
$handler();
2324
}
24-
});
25+
})->await();
2526

2627
self::assertGreaterThan($start + 1.499, microtime(true));
2728
}

test/Unit/AsyncMilliSleepExceptionHandlerTest.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@
55

66
use PHPUnit\Framework\TestCase;
77
use ScriptFUSION\Retry\ExceptionHandler\AsyncMilliSleepExceptionHandler;
8+
use function Amp\async;
89

910
/**
1011
* @see AsyncMilliSleepExceptionHandler
1112
*/
1213
final class AsyncMilliSleepExceptionHandlerTest extends TestCase
1314
{
14-
public function testValue()
15+
public function testValue(): void
1516
{
1617
$handler = new AsyncMilliSleepExceptionHandler(new \ArrayIterator([1000]));
1718

1819
$start = microtime(true);
1920

20-
\Amp\Loop::run(static function () use ($handler): \Generator {
21-
yield $handler();
22-
});
21+
async(fn () => $handler())->await();
2322

2423
self::assertGreaterThan($start + .999, microtime(true));
2524
}
2625

27-
public function testSeries()
26+
public function testSeries(): void
2827
{
2928
$handler = new AsyncMilliSleepExceptionHandler(
30-
new \ArrayIterator($delays = array_fill(0, $limit = 10, 100))
29+
new \ArrayIterator(array_fill(0, $limit = 10, 100))
3130
);
3231

3332
$start = microtime(true);
3433

35-
\Amp\Loop::run(static function () use ($handler, $limit): \Generator {
34+
async(static function () use ($handler, $limit): void {
3635
for ($counter = 0; $counter < $limit; ++$counter) {
37-
yield $handler();
36+
$handler();
3837
}
39-
});
38+
})->await();
4039

4140
self::assertGreaterThan($start + .999, microtime(true));
4241
}

test/Unit/ExponentialBackoffExceptionHandlerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
final class ExponentialBackoffExceptionHandlerTest extends TestCase
1313
{
14-
public function test()
14+
public function test(): void
1515
{
1616
$handler = new ExponentialBackoffExceptionHandler;
1717

test/Unit/MicroSleepExceptionHandlerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
final class MicroSleepExceptionHandlerTest extends TestCase
1010
{
11-
public function testValue()
11+
public function testValue(): void
1212
{
1313
$handler = new MicroSleepExceptionHandler(new \ArrayIterator([1000000]));
1414

@@ -18,7 +18,7 @@ public function testValue()
1818
self::assertGreaterThan($start + 1, microtime(true));
1919
}
2020

21-
public function testSeries()
21+
public function testSeries(): void
2222
{
2323
$handler = new MicroSleepExceptionHandler(new \ArrayIterator($delays = array_fill(0, $limit = 10, 100000)));
2424

test/Unit/Sequence/PowersOfTwoSequenceTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class PowersOfTwoSequenceTest extends TestCase
1414
/**
1515
* Tests that the iterator yields the expected sequence values.
1616
*/
17-
public function testSequence()
17+
public function testSequence(): void
1818
{
1919
$powers = new \LimitIterator((new PowersOfTwoSequence)->getIterator(), 0, 6);
2020

@@ -24,7 +24,7 @@ public function testSequence()
2424
/**
2525
* Tests that the iterator will not yield values forever.
2626
*/
27-
public function testIsFiniteSequence()
27+
public function testIsFiniteSequence(): void
2828
{
2929
$counter = 0;
3030
foreach (new \LimitIterator((new PowersOfTwoSequence)->getIterator(), 0, $limit = 100) as $_) {

0 commit comments

Comments
 (0)