From 0d8cf564a311d7eeb386103753ddb4001f8ab8ee Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Thu, 10 Oct 2019 19:14:59 +0200 Subject: [PATCH] Forward compatibility with react/promise 3 --- composer.json | 6 +++--- src/DnsConnector.php | 4 ++-- src/HappyEyeBallsConnectionBuilder.php | 6 +++--- src/StreamEncryption.php | 2 +- tests/DnsConnectorTest.php | 12 ++++++------ tests/FunctionalConnectorTest.php | 2 +- tests/FunctionalTcpServerTest.php | 16 ++++++++++++---- tests/HappyEyeBallsConnectorTest.php | 18 +++++++++--------- tests/LimitingServerTest.php | 10 +++++++--- tests/SecureConnectorTest.php | 5 ++++- tests/ServerTest.php | 8 ++++++-- tests/SocketServerTest.php | 8 ++++++-- tests/TimeoutConnectorTest.php | 2 +- 13 files changed, 61 insertions(+), 38 deletions(-) diff --git a/composer.json b/composer.json index ede57084..ae325fc9 100644 --- a/composer.json +++ b/composer.json @@ -30,14 +30,14 @@ "evenement/evenement": "^3.0 || ^2.0 || ^1.0", "react/dns": "^1.8", "react/event-loop": "^1.2", - "react/promise": "^2.6.0 || ^1.2.1", - "react/promise-timer": "^1.8", + "react/promise": "^3 || ^2.6 || ^1.2.1", + "react/promise-timer": "^1.9", "react/stream": "^1.2" }, "require-dev": { "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35", "react/async": "^4 || ^3 || ^2", - "react/promise-stream": "^1.2" + "react/promise-stream": "^1.4" }, "autoload": { "psr-4": { diff --git a/src/DnsConnector.php b/src/DnsConnector.php index 29523ebf..ba04c4d1 100644 --- a/src/DnsConnector.php +++ b/src/DnsConnector.php @@ -4,7 +4,7 @@ use React\Dns\Resolver\ResolverInterface; use React\Promise; -use React\Promise\CancellablePromiseInterface; +use React\Promise\PromiseInterface; final class DnsConnector implements ConnectorInterface { @@ -103,7 +103,7 @@ function ($_, $reject) use (&$promise, &$resolved, $uri) { } // (try to) cancel pending DNS lookup / connection attempt - if ($promise instanceof CancellablePromiseInterface) { + if ($promise instanceof PromiseInterface && \method_exists($promise, 'cancel')) { // overwrite callback arguments for PHP7+ only, so they do not show // up in the Exception trace and do not cause a possible cyclic reference. $_ = $reject = null; diff --git a/src/HappyEyeBallsConnectionBuilder.php b/src/HappyEyeBallsConnectionBuilder.php index 6bd07168..65e0718f 100644 --- a/src/HappyEyeBallsConnectionBuilder.php +++ b/src/HappyEyeBallsConnectionBuilder.php @@ -7,7 +7,7 @@ use React\EventLoop\LoopInterface; use React\EventLoop\TimerInterface; use React\Promise; -use React\Promise\CancellablePromiseInterface; +use React\Promise\PromiseInterface; /** * @internal @@ -248,13 +248,13 @@ public function cleanUp() $this->connectQueue = array(); foreach ($this->connectionPromises as $connectionPromise) { - if ($connectionPromise instanceof CancellablePromiseInterface) { + if ($connectionPromise instanceof PromiseInterface && \method_exists($connectionPromise, 'cancel')) { $connectionPromise->cancel(); } } foreach ($this->resolverPromises as $resolverPromise) { - if ($resolverPromise instanceof CancellablePromiseInterface) { + if ($resolverPromise instanceof PromiseInterface && \method_exists($resolverPromise, 'cancel')) { $resolverPromise->cancel(); } } diff --git a/src/StreamEncryption.php b/src/StreamEncryption.php index 4aa7fca0..b7aa3f24 100644 --- a/src/StreamEncryption.php +++ b/src/StreamEncryption.php @@ -115,7 +115,7 @@ public function toggleCrypto($socket, Deferred $deferred, $toggle, $method) \restore_error_handler(); if (true === $result) { - $deferred->resolve(); + $deferred->resolve(null); } else if (false === $result) { // overwrite callback arguments for PHP7+ only, so they do not show // up in the Exception trace and do not cause a possible cyclic reference. diff --git a/tests/DnsConnectorTest.php b/tests/DnsConnectorTest.php index 5d37a237..545ec5cb 100644 --- a/tests/DnsConnectorTest.php +++ b/tests/DnsConnectorTest.php @@ -26,7 +26,7 @@ public function setUpMocks() public function testPassByResolverIfGivenIp() { $this->resolver->expects($this->never())->method('resolve'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('127.0.0.1:80'); } @@ -34,7 +34,7 @@ public function testPassByResolverIfGivenIp() public function testPassThroughResolverIfGivenHost() { $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('google.com:80'); } @@ -42,7 +42,7 @@ public function testPassThroughResolverIfGivenHost() public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() { $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('::1'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('google.com:80'); } @@ -50,7 +50,7 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() public function testPassByResolverIfGivenCompleteUri() { $this->resolver->expects($this->never())->method('resolve'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment'); } @@ -58,7 +58,7 @@ public function testPassByResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenCompleteUri() { $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/path?query#fragment'); } @@ -66,7 +66,7 @@ public function testPassThroughResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenExplicitHost() { $this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4'))); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/?hostname=google.de'); } diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index 0d003f40..5e38544f 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -142,7 +142,7 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo $deferred = new Deferred(); $server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred) { $connection->on('close', function () use ($deferred) { - $deferred->resolve(); + $deferred->resolve(null); }); Loop::futureTick(function () use ($promise) { diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index 76026ad7..dff34af2 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -17,7 +17,9 @@ public function testEmitsConnectionForNewConnection() $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $connector = new TcpConnector(); @@ -58,7 +60,9 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() $server->resume(); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $connector = new TcpConnector(); @@ -213,7 +217,9 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $connector = new TcpConnector(); @@ -238,7 +244,9 @@ public function testEmitsConnectionForNewIpv6Connection() $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $connector = new TcpConnector(); diff --git a/tests/HappyEyeBallsConnectorTest.php b/tests/HappyEyeBallsConnectorTest.php index aeceae54..405854a5 100644 --- a/tests/HappyEyeBallsConnectorTest.php +++ b/tests/HappyEyeBallsConnectorTest.php @@ -103,7 +103,7 @@ public function testThatAnyOtherPendingConnectionAttemptsWillBeCanceledOnceAConn public function testPassByResolverIfGivenIp() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\resolve(null))); $this->connector->connect('127.0.0.1:80'); @@ -113,7 +113,7 @@ public function testPassByResolverIfGivenIp() public function testPassByResolverIfGivenIpv6() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('[::1]:80'); @@ -123,7 +123,7 @@ public function testPassByResolverIfGivenIpv6() public function testPassThroughResolverIfGivenHost() { $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4')))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('google.com:80'); @@ -133,7 +133,7 @@ public function testPassThroughResolverIfGivenHost() public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() { $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('::1')))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('google.com:80'); @@ -143,7 +143,7 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6() public function testPassByResolverIfGivenCompleteUri() { $this->resolver->expects($this->never())->method('resolveAll'); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('scheme://127.0.0.1:80/path?query#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment'); @@ -153,7 +153,7 @@ public function testPassByResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenCompleteUri() { $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4')))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/path?query&hostname=google.com#fragment'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/path?query#fragment'); @@ -163,7 +163,7 @@ public function testPassThroughResolverIfGivenCompleteUri() public function testPassThroughResolverIfGivenExplicitHost() { $this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('1.2.3.4')))); - $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('scheme://1.2.3.4:80/?hostname=google.de'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/?hostname=google.de'); @@ -184,7 +184,7 @@ public function testIpv6ResolvesFirstSoIsTheFirstToConnect(array $ipv6, array $i $this->returnValue(Promise\resolve($ipv6)), $this->returnValue($deferred->promise()) ); - $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(']:80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/?hostname=google.com'); @@ -209,7 +209,7 @@ public function testIpv6DoesntResolvesWhileIpv4DoesFirstSoIpv4Connects(array $ip $this->returnValue($deferred->promise()), $this->returnValue(Promise\resolve($ipv4)) ); - $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject())); + $this->tcp->expects($this->any())->method('connect')->with($this->stringContains(':80/?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject')))); $this->connector->connect('scheme://google.com:80/?hostname=google.com'); diff --git a/tests/LimitingServerTest.php b/tests/LimitingServerTest.php index 1d054f73..1430e362 100644 --- a/tests/LimitingServerTest.php +++ b/tests/LimitingServerTest.php @@ -152,7 +152,9 @@ public function testSocketDisconnectionWillRemoveFromList() $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { - $connection->on('close', $resolve); + $connection->on('close', function () use ($resolve) { + $resolve(null); + }); }); }); @@ -171,7 +173,9 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper $server->on('error', $this->expectCallableNever()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $first = stream_socket_client($server->getAddress()); @@ -197,7 +201,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog() ++$connections; if ($connections >= 2) { - $resolve(); + $resolve(null); } }); }); diff --git a/tests/SecureConnectorTest.php b/tests/SecureConnectorTest.php index 591012e5..28035a85 100644 --- a/tests/SecureConnectorTest.php +++ b/tests/SecureConnectorTest.php @@ -231,9 +231,12 @@ public function testCancelDuringStreamEncryptionCancelsEncryptionAndClosesConnec $ref->setAccessible(true); $ref->setValue($this->connector, $encryption); - $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn(Promise\resolve($connection)); + $deferred = new Deferred(); + $this->tcp->expects($this->once())->method('connect')->with($this->equalTo('example.com:80'))->willReturn($deferred->promise()); $promise = $this->connector->connect('example.com:80'); + $deferred->resolve($connection); + $promise->cancel(); $exception = null; diff --git a/tests/ServerTest.php b/tests/ServerTest.php index fa1c894e..a87a9b6e 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -122,7 +122,9 @@ public function testEmitsConnectionForNewConnection() $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $client = stream_socket_client($server->getAddress()); @@ -150,7 +152,9 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { - $server->on('connection', $resolve); + $server->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $client = stream_socket_client($server->getAddress()); diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index 2f5d93b9..9bc50340 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -157,7 +157,9 @@ public function testEmitsConnectionForNewConnection() $socket->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($socket) { - $socket->on('connection', $resolve); + $socket->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $client = stream_socket_client($socket->getAddress()); @@ -185,7 +187,9 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $socket->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($socket) { - $socket->on('connection', $resolve); + $socket->on('connection', function () use ($resolve) { + $resolve(null); + }); }); $client = stream_socket_client($socket->getAddress()); diff --git a/tests/TimeoutConnectorTest.php b/tests/TimeoutConnectorTest.php index b9bff26c..2ac9690e 100644 --- a/tests/TimeoutConnectorTest.php +++ b/tests/TimeoutConnectorTest.php @@ -61,7 +61,7 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects() public function testResolvesWhenConnectorResolves() { - $promise = Promise\resolve(); + $promise = Promise\resolve(null); $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise));