Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update close handler to avoid unhandled promise rejections #117

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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