Skip to content

Commit d0b8d7a

Browse files
committed
Added AsyncMilliSleepExceptionHandler and accompanying test.
Upgraded PHPUnit 4.8 -> 6. Changed PHP minimum version from 5.5 -> 7.0.
1 parent cd6a0c8 commit d0b8d7a

11 files changed

+94
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
22
/.*/
3+
/composer.lock

.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ sudo: false
66
language: php
77

88
php:
9-
- 5.5
10-
- 5.6
119
- 7.0
1210
- 7.1
1311
- 7.2
12+
- 7.3
1413

1514
env:
1615
matrix:

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
}
99
],
1010
"require": {
11+
"php": "^7",
1112
"scriptfusion/retry": "^1|^2"
1213
},
1314
"require-dev": {
14-
"phpunit/phpunit": "^4.8"
15+
"phpunit/phpunit": "^6",
16+
"amphp/amp": "^2"
1517
},
1618
"autoload": {
1719
"psr-4": {
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Retry\ExceptionHandler;
5+
6+
use Amp\Delayed;
7+
8+
/**
9+
* Delays for a series of millisecond delays on each invocation.
10+
*/
11+
class AsyncMilliSleepExceptionHandler
12+
{
13+
private $delays;
14+
15+
public function __construct(\Iterator $delays)
16+
{
17+
$this->delays = $delays;
18+
}
19+
20+
public function __invoke()
21+
{
22+
$delay = $this->delays->current();
23+
24+
$this->delays->next();
25+
26+
return new Delayed($delay);
27+
}
28+
}

src/ExponentialBackoffExceptionHandler.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSION\Retry\ExceptionHandler;
34

45
use ScriptFUSION\Retry\ExceptionHandler\Sequence\PowersOfTwoSequence;
@@ -15,11 +16,11 @@ class ExponentialBackoffExceptionHandler extends MicroSleepExceptionHandler
1516
public function __construct($microTimeCoefficient = self::DEFAULT_COEFFICIENT)
1617
{
1718
parent::__construct($this->generateSequence(
18-
$this->microTimeCoefficient = $microTimeCoefficient|0
19+
$this->microTimeCoefficient = $microTimeCoefficient | 0
1920
));
2021
}
2122

22-
private function generateSequence($coefficient)
23+
private function generateSequence($coefficient): \Generator
2324
{
2425
foreach (new PowersOfTwoSequence as $base) {
2526
yield $base * $coefficient;

src/MicroSleepExceptionHandler.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSION\Retry\ExceptionHandler;
34

45
/**
5-
* Sleeps for a series of microsecond delays for each invocation.
6+
* Sleeps for a series of microsecond delays on each invocation.
67
*/
78
class MicroSleepExceptionHandler
89
{

src/Sequence/PowersOfTwoSequence.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSION\Retry\ExceptionHandler\Sequence;
34

45
final class PowersOfTwoSequence implements \IteratorAggregate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSIONTest\Retry\ExceptionHandler\Unit;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use ScriptFUSION\Retry\ExceptionHandler\AsyncMilliSleepExceptionHandler;
8+
9+
/**
10+
* @see AsyncMilliSleepExceptionHandler
11+
*/
12+
final class AsyncMilliSleepExceptionHandlerTest extends TestCase
13+
{
14+
public function testValue()
15+
{
16+
$handler = new AsyncMilliSleepExceptionHandler(new \ArrayIterator([1000]));
17+
18+
$start = microtime(true);
19+
\Amp\Promise\wait($handler());
20+
21+
self::assertGreaterThan($start + 1, microtime(true));
22+
}
23+
24+
public function testSeries()
25+
{
26+
$handler = new AsyncMilliSleepExceptionHandler(
27+
new \ArrayIterator($delays = array_fill(0, $limit = 10, 100))
28+
);
29+
30+
$start = microtime(true);
31+
32+
\Amp\Loop::run(static function () use ($handler, $limit): \Generator {
33+
for ($counter = 0; $counter < $limit; ++$counter) {
34+
yield $handler();
35+
}
36+
});
37+
38+
self::assertGreaterThan($start + 1, microtime(true));
39+
}
40+
}

test/Unit/ExponentialBackoffExceptionHandlerTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSIONTest\Retry\Unit\ExceptionHandler;
34

5+
use PHPUnit\Framework\TestCase;
46
use ScriptFUSION\Retry\ExceptionHandler\ExponentialBackoffExceptionHandler;
57

6-
final class ExponentialBackoffExceptionHandlerTest extends \PHPUnit_Framework_TestCase
8+
final class ExponentialBackoffExceptionHandlerTest extends TestCase
79
{
810
public function test()
911
{

test/Unit/MicroSleepExceptionHandlerTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSIONTest\Retry\Unit\ExceptionHandler;
34

5+
use PHPUnit\Framework\TestCase;
46
use ScriptFUSION\Retry\ExceptionHandler\MicroSleepExceptionHandler;
57

6-
final class MicroSleepExceptionHandlerTest extends \PHPUnit_Framework_TestCase
8+
final class MicroSleepExceptionHandlerTest extends TestCase
79
{
810
public function testValue()
911
{

test/Unit/Sequence/PowersOfTwoSequenceTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<?php
1+
<?php declare(strict_types=1);
2+
23
namespace ScriptFUSIONTest\Retry\Unit\ExceptionHandler\Sequence;
34

5+
use PHPUnit\Framework\TestCase;
46
use ScriptFUSION\Retry\ExceptionHandler\Sequence\PowersOfTwoSequence;
57

6-
final class PowersOfTwoSequenceTest extends \PHPUnit_Framework_TestCase
8+
final class PowersOfTwoSequenceTest extends TestCase
79
{
810
public function testSequence()
911
{

0 commit comments

Comments
 (0)