Skip to content

Commit bac85ce

Browse files
[HttpClient] resolve promise chains on HttplugClient::wait()
1 parent 6e7f325 commit bac85ce

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

src/Symfony/Component/HttpClient/HttplugClient.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,17 @@ public function sendAsyncRequest(RequestInterface $request): Promise
112112
return new RejectedPromise($e);
113113
}
114114

115-
$cancel = function () use ($response) {
116-
$response->cancel();
117-
unset($this->promisePool[$response]);
118-
};
119-
120115
$promise = new GuzzlePromise(function () use ($response) {
121116
$this->pendingResponse = $response;
122117
$this->wait();
123-
}, $cancel);
118+
}, function () use ($response) {
119+
$response->cancel();
120+
unset($this->promisePool[$response]);
121+
});
124122

125123
$this->promisePool[$response] = [$request, $promise];
126124

127-
return new HttplugPromise($promise, $cancel);
125+
return new HttplugPromise($promise);
128126
}
129127

130128
/**

src/Symfony/Component/HttpClient/Response/HttplugPromise.php

+1-15
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@
2323
final class HttplugPromise implements HttplugPromiseInterface
2424
{
2525
private $promise;
26-
private $cancel;
2726

28-
public function __construct(GuzzlePromiseInterface $promise, callable $cancel = null)
27+
public function __construct(GuzzlePromiseInterface $promise)
2928
{
3029
$this->promise = $promise;
31-
$this->cancel = $cancel;
3230
}
3331

3432
public function then(callable $onFulfilled = null, callable $onRejected = null): self
@@ -58,16 +56,4 @@ public function wait($unwrap = true)
5856
{
5957
return $this->promise->wait($unwrap);
6058
}
61-
62-
public function __destruct()
63-
{
64-
if ($this->cancel) {
65-
($this->cancel)();
66-
}
67-
}
68-
69-
public function __wakeup()
70-
{
71-
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
72-
}
7359
}

src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,31 @@ public function testSendAsyncRequest()
7575
$this->assertSame('HTTP/1.1', $body['SERVER_PROTOCOL']);
7676
}
7777

78+
public function testWait()
79+
{
80+
$client = new HttplugClient(new NativeHttpClient());
81+
82+
$successCallableCalled = false;
83+
$failureCallableCalled = false;
84+
$client->sendAsyncRequest($client->createRequest('GET', 'http://localhost:8057'))
85+
->then(function (ResponseInterface $response) use (&$successCallableCalled) {
86+
$successCallableCalled = true;
87+
88+
return $response;
89+
}, function (\Exception $exception) use (&$failureCallableCalled) {
90+
$failureCallableCalled = true;
91+
92+
throw $exception;
93+
});
94+
95+
$client->wait(0);
96+
$this->assertFalse($successCallableCalled, '$promise->then() should not be called yet.');
97+
98+
$client->wait();
99+
$this->assertTrue($successCallableCalled, '$promise->then() should have been called.');
100+
$this->assertFalse($failureCallableCalled, 'Failure callable should not be called when request is successful.');
101+
}
102+
78103
public function testPostRequest()
79104
{
80105
$client = new HttplugClient(new NativeHttpClient());

0 commit comments

Comments
 (0)