From 6b11ca811fc96a5337357318efb2d293e117662b Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Thu, 27 Sep 2018 09:06:36 +0200 Subject: [PATCH 1/2] Do not use defer --- src/Plugin/RetryPlugin.php | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/Plugin/RetryPlugin.php b/src/Plugin/RetryPlugin.php index 02ec39b..8446246 100644 --- a/src/Plugin/RetryPlugin.php +++ b/src/Plugin/RetryPlugin.php @@ -2,7 +2,6 @@ namespace Http\Client\Common\Plugin; -use Http\Client\Common\Deferred; use Http\Client\Common\Plugin; use Http\Client\Exception; use Psr\Http\Message\RequestInterface; @@ -77,22 +76,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl { $chainIdentifier = spl_object_hash((object) $first); - $promise = $next($request); - $deferred = new Deferred(function () use ($promise) { - $promise->wait(false); - }); - - $onFulfilled = function (ResponseInterface $response) use ($chainIdentifier, $deferred) { + return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) { if (array_key_exists($chainIdentifier, $this->retryStorage)) { unset($this->retryStorage[$chainIdentifier]); } - $deferred->resolve($response); - return $response; - }; - - $onRejected = function (Exception $exception) use ($request, $next, $onFulfilled, &$onRejected, $chainIdentifier, $deferred) { + }, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) { if (!array_key_exists($chainIdentifier, $this->retryStorage)) { $this->retryStorage[$chainIdentifier] = 0; } @@ -100,8 +90,6 @@ public function handleRequest(RequestInterface $request, callable $next, callabl if ($this->retryStorage[$chainIdentifier] >= $this->retry) { unset($this->retryStorage[$chainIdentifier]); - $deferred->reject($exception); - throw $exception; } @@ -114,15 +102,10 @@ public function handleRequest(RequestInterface $request, callable $next, callabl // Retry in synchrone ++$this->retryStorage[$chainIdentifier]; + $promise = $this->handleRequest($request, $next, $first); - $next($request)->then($onFulfilled, $onRejected); - - throw $exception; - }; - - $promise->then($onFulfilled, $onRejected); - - return $deferred; + return $promise->wait(); + }); } /** From 802a6f357ef031685c59c68a90198725fa1daaac Mon Sep 17 00:00:00 2001 From: Nyholm Date: Fri, 28 Sep 2018 15:40:41 +0100 Subject: [PATCH 2/2] Reverted tests --- spec/Plugin/RetryPluginSpec.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/spec/Plugin/RetryPluginSpec.php b/spec/Plugin/RetryPluginSpec.php index 852fb73..37800ae 100644 --- a/spec/Plugin/RetryPluginSpec.php +++ b/spec/Plugin/RetryPluginSpec.php @@ -30,9 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo } }; - $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred'); - $promise->wait()->shouldReturn($response); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } function it_throws_exception_on_multiple_exceptions(RequestInterface $request) @@ -55,7 +53,7 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request) }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow($exception2)->duringWait(); } @@ -78,7 +76,7 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); $promise->wait()->shouldReturn($response); } @@ -100,13 +98,8 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp } }; - $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred'); - $promise->wait()->shouldReturn($response); - - $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred'); - $promise->wait()->shouldReturn($response); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } function it_has_an_exponential_default_delay(RequestInterface $request, Exception\HttpException $exception)