Skip to content

Commit

Permalink
Update close handler to avoid unhandled promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed May 28, 2024
1 parent e35481d commit 039ce02
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public function connect($uri)
// either close active connection or cancel pending connection attempt
$connecting->then(function (ConnectionInterface $stream) {
$stream->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
$connecting->cancel();
});
Expand Down
7 changes: 7 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public function testCreateWithInvalidHostDoesNotConnect()
$promise = $this->client->connect(str_repeat('a', '256') . ':80');

$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}

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

$this->assertInstanceOf('\React\Promise\PromiseInterface', $promise);

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}

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

$promise = $this->client->connect('google.com:80');

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$deferred->reject(new \RuntimeException());
unset($deferred, $promise);

Expand Down
7 changes: 2 additions & 5 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Clue\React\Socks\Server;
use React\EventLoop\Loop;
use React\Promise\Promise;
use React\Socket\ConnectionInterface;
use React\Socket\Connector;
use React\Socket\SecureConnector;
use React\Socket\SocketServer;
Expand Down Expand Up @@ -506,9 +507,7 @@ public function testSecureConnectorInvalidUnboundPortTimeout()

private function assertResolveStream($promise)
{
$this->expectPromiseResolve($promise);

$promise->then(function ($stream) {
$promise = $promise->then(function (ConnectionInterface $stream) {
$stream->close();
});

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

private function assertRejectPromise($promise, $message = null, $code = null)
{
$this->expectPromiseReject($promise);

if (method_exists($this, 'expectException')) {
$this->expectException('Exception');
if ($message !== null) {
Expand Down
29 changes: 0 additions & 29 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,6 @@ protected function createCallableMock()
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}

protected function expectPromiseResolve($promise)
{
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$that = $this;
$promise->then(null, function($error) use ($that) {
$that->assertNull($error);
$that->fail('promise rejected');
});
$promise->then($this->expectCallableOnce(), $this->expectCallableNever());

return $promise;
}

protected function expectPromiseReject($promise)
{
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);

$that = $this;
$promise->then(function($value) use ($that) {
$that->assertNull($value);
$that->fail('promise resolved');
});

$promise->then($this->expectCallableNever(), $this->expectCallableOnce());

return $promise;
}

public function setExpectedException($exception, $message = '', $code = 0)
{
if (method_exists($this, 'expectException')) {
Expand Down

0 comments on commit 039ce02

Please sign in to comment.