From 039ce02c33520b25e2a914335e7555249fba5fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 23 May 2024 20:13:05 +0200 Subject: [PATCH] Update close handler to avoid unhandled promise rejections --- src/Client.php | 2 ++ tests/ClientTest.php | 7 +++++++ tests/FunctionalTest.php | 7 ++----- tests/TestCase.php | 29 ----------------------------- 4 files changed, 11 insertions(+), 34 deletions(-) diff --git a/src/Client.php b/src/Client.php index b657025..fb689c8 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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(); }); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e0e3520..b11c1ba 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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() @@ -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() @@ -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); diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 6ea67fd..e5323aa 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -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; @@ -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(); }); @@ -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) { diff --git a/tests/TestCase.php b/tests/TestCase.php index 76e77bb..c429f67 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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')) {