Skip to content

Commit 54a4fc9

Browse files
Nyholmdbu
authored andcommitted
Do not use defer (#111)
* Do not use defer * Reverted tests
1 parent f77e88b commit 54a4fc9

File tree

2 files changed

+10
-34
lines changed

2 files changed

+10
-34
lines changed

spec/Plugin/RetryPluginSpec.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
3030
}
3131
};
3232

33-
$promise = $this->handleRequest($request, $next, function () {});
34-
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
35-
$promise->wait()->shouldReturn($response);
33+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
3634
}
3735

3836
function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
@@ -55,7 +53,7 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
5553
};
5654

5755
$promise = $this->handleRequest($request, $next, function () {});
58-
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
56+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise');
5957
$promise->shouldThrow($exception2)->duringWait();
6058
}
6159

@@ -78,7 +76,7 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn
7876
};
7977

8078
$promise = $this->handleRequest($request, $next, function () {});
81-
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
79+
$promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
8280
$promise->wait()->shouldReturn($response);
8381
}
8482

@@ -100,13 +98,8 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp
10098
}
10199
};
102100

103-
$promise = $this->handleRequest($request, $next, function () {});
104-
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
105-
$promise->wait()->shouldReturn($response);
106-
107-
$promise = $this->handleRequest($request, $next, function () {});
108-
$promise->shouldReturnAnInstanceOf('Http\Client\Common\Deferred');
109-
$promise->wait()->shouldReturn($response);
101+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
102+
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise');
110103
}
111104

112105
function it_has_an_exponential_default_delay(RequestInterface $request, Exception\HttpException $exception)

src/Plugin/RetryPlugin.php

+5-22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Http\Client\Common\Plugin;
44

5-
use Http\Client\Common\Deferred;
65
use Http\Client\Common\Plugin;
76
use Http\Client\Exception;
87
use Psr\Http\Message\RequestInterface;
@@ -77,31 +76,20 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
7776
{
7877
$chainIdentifier = spl_object_hash((object) $first);
7978

80-
$promise = $next($request);
81-
$deferred = new Deferred(function () use ($promise) {
82-
$promise->wait(false);
83-
});
84-
85-
$onFulfilled = function (ResponseInterface $response) use ($chainIdentifier, $deferred) {
79+
return $next($request)->then(function (ResponseInterface $response) use ($request, $chainIdentifier) {
8680
if (array_key_exists($chainIdentifier, $this->retryStorage)) {
8781
unset($this->retryStorage[$chainIdentifier]);
8882
}
8983

90-
$deferred->resolve($response);
91-
9284
return $response;
93-
};
94-
95-
$onRejected = function (Exception $exception) use ($request, $next, $onFulfilled, &$onRejected, $chainIdentifier, $deferred) {
85+
}, function (Exception $exception) use ($request, $next, $first, $chainIdentifier) {
9686
if (!array_key_exists($chainIdentifier, $this->retryStorage)) {
9787
$this->retryStorage[$chainIdentifier] = 0;
9888
}
9989

10090
if ($this->retryStorage[$chainIdentifier] >= $this->retry) {
10191
unset($this->retryStorage[$chainIdentifier]);
10292

103-
$deferred->reject($exception);
104-
10593
throw $exception;
10694
}
10795

@@ -114,15 +102,10 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
114102

115103
// Retry in synchrone
116104
++$this->retryStorage[$chainIdentifier];
105+
$promise = $this->handleRequest($request, $next, $first);
117106

118-
$next($request)->then($onFulfilled, $onRejected);
119-
120-
throw $exception;
121-
};
122-
123-
$promise->then($onFulfilled, $onRejected);
124-
125-
return $deferred;
107+
return $promise->wait();
108+
});
126109
}
127110

128111
/**

0 commit comments

Comments
 (0)