mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added throwbale
to the end of arguments of fallback for retry
component. (#2475)
* Added argument `throwbale`to the end of arguments for fallback. * Update CHANGELOG-2.0.md * Update CHANGELOG-2.0.md
This commit is contained in:
parent
4fc2077659
commit
94fdaddc62
@ -4,7 +4,8 @@
|
||||
|
||||
- [#2455](https://github.com/hyperf/hyperf/pull/2455) Added method `Socket::getRequest` to retrieve psr7 request from socket for socketio-server.
|
||||
- [#2459](https://github.com/hyperf/hyperf/pull/2459) Added `ReloadChannelListener` to reload timeout or failed channels automatically for async-queue.
|
||||
- [#2643](https://github.com/hyperf/hyperf/pull/2463) Added optional visitor `ModelRewriteGetterSetterVisitor` for `gen:model`.
|
||||
- [#2463](https://github.com/hyperf/hyperf/pull/2463) Added optional visitor `ModelRewriteGetterSetterVisitor` for `gen:model`.
|
||||
- [#2475](https://github.com/hyperf/hyperf/pull/2475) Added `throwbale` to the end of arguments of fallback for `retry` component.
|
||||
|
||||
## Fixed
|
||||
|
||||
|
@ -41,6 +41,7 @@ class FallbackRetryPolicy extends BaseRetryPolicy implements RetryPolicyInterfac
|
||||
if (! is_callable($fallback)) {
|
||||
return false;
|
||||
}
|
||||
$throwable = $retryContext['lastThrowable'] ?? null;
|
||||
$retryContext['lastThrowable'] = $retryContext['lastResult'] = null;
|
||||
if (isset($retryContext['proceedingJoinPoint'])) {
|
||||
$arguments = $retryContext['proceedingJoinPoint']->getArguments();
|
||||
@ -48,6 +49,8 @@ class FallbackRetryPolicy extends BaseRetryPolicy implements RetryPolicyInterfac
|
||||
$arguments = [];
|
||||
}
|
||||
|
||||
$arguments[] = $throwable;
|
||||
|
||||
try {
|
||||
$retryContext['lastResult'] = call_user_func($fallback, ...$arguments);
|
||||
} catch (\Throwable $throwable) {
|
||||
|
@ -29,6 +29,7 @@ use Hyperf\Retry\NoOpRetryBudget;
|
||||
use Hyperf\Retry\Policy\TimeoutRetryPolicy;
|
||||
use Hyperf\Retry\RetryBudgetInterface;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use HyperfTest\Retry\Stub\Foo;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Swoole\Coroutine\Channel;
|
||||
@ -297,6 +298,34 @@ class RetryAnnotationAspectTest extends TestCase
|
||||
$aspect->process($point);
|
||||
}
|
||||
|
||||
public function testFallbackForCircuitBreaker()
|
||||
{
|
||||
$aspect = new RetryAnnotationAspect();
|
||||
$point = Mockery::mock(ProceedingJoinPoint::class);
|
||||
|
||||
$point->shouldReceive('getAnnotationMetadata')->andReturns(
|
||||
new class() extends AnnotationMetadata {
|
||||
public $method;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$state = new \Hyperf\Retry\CircuitBreakerState(10);
|
||||
$retry = new CircuitBreaker(['circuitBreakerState' => $state]);
|
||||
$retry->sleepStrategyClass = FlatStrategy::class;
|
||||
$retry->fallback = Foo::class . '@fallbackWithThrowable';
|
||||
$retry->maxAttempts = 2;
|
||||
$this->method = [
|
||||
AbstractRetry::class => $retry,
|
||||
];
|
||||
}
|
||||
}
|
||||
);
|
||||
$point->shouldReceive('process')->times(2)->andThrow(new \RuntimeException('ok'));
|
||||
$point->shouldReceive('getArguments')->andReturns([$string = uniqid()]);
|
||||
$result = $aspect->process($point);
|
||||
$this->assertSame($string . ':ok', $result);
|
||||
}
|
||||
|
||||
public function testTimeout()
|
||||
{
|
||||
$aspect = new RetryAnnotationAspect();
|
||||
|
@ -115,8 +115,18 @@ class RetryTest extends TestCase
|
||||
});
|
||||
$this->assertEquals(10, $result);
|
||||
|
||||
$this->assertTrue(is_callable('Hyperf\\Utils\\Arr::accessible'));
|
||||
$this->assertTrue(is_callable(Foo::class . '::fallback'));
|
||||
$this->assertTrue(is_callable(Foo::class . '::staticCall'));
|
||||
}
|
||||
|
||||
public function testThrowableInFallback()
|
||||
{
|
||||
$instance = Mockery::mock(Foo::class);
|
||||
$instance->shouldReceive('test')->twice()->andThrowExceptions([new \RuntimeException()]);
|
||||
Retry::whenThrows()->max(2)->fallback(function ($throwable) {
|
||||
$this->assertInstanceOf(\RuntimeException::class, $throwable);
|
||||
})->call(function () use ($instance) {
|
||||
return $instance->test();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace HyperfTest\Retry\Stub;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function fallback()
|
||||
@ -22,4 +24,9 @@ class Foo
|
||||
{
|
||||
return 10;
|
||||
}
|
||||
|
||||
public function fallbackWithThrowable(string $string, ?Throwable $throwable = null)
|
||||
{
|
||||
return $string . ':' . $throwable->getMessage();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user