Skip to content

Commit 66ba710

Browse files
committed
Added unit tests for default recoverable exception handlers.
1 parent f6e401f commit 66ba710

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSIONTest\Unit\Connector\Recoverable;
5+
6+
use Amp\PHPUnit\AsyncTestCase;
7+
use ScriptFUSION\Porter\Connector\Recoverable\ExponentialAsyncDelayRecoverableExceptionHandler;
8+
use ScriptFUSIONTest\Stubs\TestRecoverableException;
9+
10+
/**
11+
* @see ExponentialAsyncDelayRecoverableExceptionHandler
12+
*/
13+
final class ExponentialAsyncDelayRecoverableExceptionHandlerTest extends AsyncTestCase
14+
{
15+
/** @var ExponentialAsyncDelayRecoverableExceptionHandler */
16+
private $handler;
17+
18+
/** @var TestRecoverableException */
19+
private $exception;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->handler = new ExponentialAsyncDelayRecoverableExceptionHandler;
26+
$this->handler->initialize();
27+
$this->exception = new TestRecoverableException;
28+
}
29+
30+
/**
31+
* Tests that when the handler is invoked, each delay is longer than the last.
32+
*/
33+
public function testDelay(): \Generator
34+
{
35+
for ($i = $previousDuration = 0; $i < 4; ++$i) {
36+
$start = microtime(true);
37+
yield ($this->handler)($this->exception);
38+
$duration = microtime(true) - $start;
39+
40+
self::assertGreaterThan($previousDuration, $duration);
41+
$previousDuration = $duration;
42+
}
43+
44+
self::assertGreaterThan(.8, $previousDuration);
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSIONTest\Unit\Connector\Recoverable;
5+
6+
use ScriptFUSION\Porter\Connector\Recoverable\ExponentialSleepRecoverableExceptionHandler;
7+
use PHPUnit\Framework\TestCase;
8+
use ScriptFUSIONTest\Stubs\TestRecoverableException;
9+
10+
/**
11+
* @see ExponentialSleepRecoverableExceptionHandler
12+
*/
13+
final class ExponentialSleepRecoverableExceptionHandlerTest extends TestCase
14+
{
15+
/** @var ExponentialSleepRecoverableExceptionHandler */
16+
private $handler;
17+
18+
/** @var TestRecoverableException */
19+
private $exception;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->handler = new ExponentialSleepRecoverableExceptionHandler;
26+
$this->handler->initialize();
27+
$this->exception = new TestRecoverableException;
28+
}
29+
30+
/**
31+
* Tests that when the handler is invoked, each delay is longer than the last.
32+
*/
33+
public function testDelay(): void
34+
{
35+
for ($i = $previousDuration = 0; $i < 4; ++$i) {
36+
$start = microtime(true);
37+
($this->handler)($this->exception);
38+
$duration = microtime(true) - $start;
39+
40+
self::assertGreaterThan($previousDuration, $duration);
41+
$previousDuration = $duration;
42+
}
43+
44+
self::assertGreaterThan(.8, $previousDuration);
45+
}
46+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace ScriptFUSIONTest\Unit\Connector\FetchExceptionHandler;
4+
namespace ScriptFUSIONTest\Unit\Connector\Recoverable;
55

66
use PHPUnit\Framework\TestCase;
77
use ScriptFUSION\Porter\Connector\Recoverable\StatelessRecoverableExceptionHandler;

0 commit comments

Comments
 (0)