Skip to content

Commit 039ce02

Browse files
committed
Update close handler to avoid unhandled promise rejections
1 parent e35481d commit 039ce02

File tree

4 files changed

+11
-34
lines changed

4 files changed

+11
-34
lines changed

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ public function connect($uri)
180180
// either close active connection or cancel pending connection attempt
181181
$connecting->then(function (ConnectionInterface $stream) {
182182
$stream->close();
183+
}, function () {
184+
// ignore to avoid reporting unhandled rejection
183185
});
184186
$connecting->cancel();
185187
});

tests/ClientTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ public function testCreateWithInvalidHostDoesNotConnect()
152152
$promise = $this->client->connect(str_repeat('a', '256') . ':80');
153153

154154
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
155+
156+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
155157
}
156158

157159
public function testCreateWithInvalidPortDoesNotConnect()
@@ -163,6 +165,8 @@ public function testCreateWithInvalidPortDoesNotConnect()
163165
$promise = $this->client->connect('some-random-site:some-random-port');
164166

165167
$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);
168+
169+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
166170
}
167171

168172
public function testConnectorRejectsWillRejectConnection()
@@ -561,6 +565,9 @@ public function testConnectionErrorShouldNotCreateGarbageCycles()
561565
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
562566

563567
$promise = $this->client->connect('google.com:80');
568+
569+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
570+
564571
$deferred->reject(new \RuntimeException());
565572
unset($deferred, $promise);
566573

tests/FunctionalTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Clue\React\Socks\Server;
88
use React\EventLoop\Loop;
99
use React\Promise\Promise;
10+
use React\Socket\ConnectionInterface;
1011
use React\Socket\Connector;
1112
use React\Socket\SecureConnector;
1213
use React\Socket\SocketServer;
@@ -506,9 +507,7 @@ public function testSecureConnectorInvalidUnboundPortTimeout()
506507

507508
private function assertResolveStream($promise)
508509
{
509-
$this->expectPromiseResolve($promise);
510-
511-
$promise->then(function ($stream) {
510+
$promise = $promise->then(function (ConnectionInterface $stream) {
512511
$stream->close();
513512
});
514513

@@ -517,8 +516,6 @@ private function assertResolveStream($promise)
517516

518517
private function assertRejectPromise($promise, $message = null, $code = null)
519518
{
520-
$this->expectPromiseReject($promise);
521-
522519
if (method_exists($this, 'expectException')) {
523520
$this->expectException('Exception');
524521
if ($message !== null) {

tests/TestCase.php

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,6 @@ protected function createCallableMock()
5555
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
5656
}
5757

58-
protected function expectPromiseResolve($promise)
59-
{
60-
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
61-
62-
$that = $this;
63-
$promise->then(null, function($error) use ($that) {
64-
$that->assertNull($error);
65-
$that->fail('promise rejected');
66-
});
67-
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());
68-
69-
return $promise;
70-
}
71-
72-
protected function expectPromiseReject($promise)
73-
{
74-
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
75-
76-
$that = $this;
77-
$promise->then(function($value) use ($that) {
78-
$that->assertNull($value);
79-
$that->fail('promise resolved');
80-
});
81-
82-
$promise->then($this->expectCallableNever(), $this->expectCallableOnce());
83-
84-
return $promise;
85-
}
86-
8758
public function setExpectedException($exception, $message = '', $code = 0)
8859
{
8960
if (method_exists($this, 'expectException')) {

0 commit comments

Comments
 (0)