Skip to content

Commit

Permalink
Merge pull request #214 from WyriHaximus-labs/promise-3
Browse files Browse the repository at this point in the history
Forward compatibility with react/promise 3
  • Loading branch information
WyriHaximus authored Aug 25, 2022
2 parents 2f505b7 + 0d8cf56 commit 9d496ba
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 38 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/DnsConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/HappyEyeBallsConnectionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use React\Promise;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;

/**
* @internal
Expand Down Expand Up @@ -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();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/StreamEncryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions tests/DnsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,47 +26,47 @@ 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');
}

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');
}

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');
}

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');
}

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');
}

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');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FunctionalConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 12 additions & 4 deletions tests/FunctionalTcpServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions tests/HappyEyeBallsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand All @@ -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');

Expand Down
10 changes: 7 additions & 3 deletions tests/LimitingServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});

Expand All @@ -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());
Expand All @@ -197,7 +201,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog()
++$connections;

if ($connections >= 2) {
$resolve();
$resolve(null);
}
});
});
Expand Down
5 changes: 4 additions & 1 deletion tests/SecureConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
8 changes: 6 additions & 2 deletions tests/SocketServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/TimeoutConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 9d496ba

Please sign in to comment.