Skip to content

Commit

Permalink
Merge pull request #307 from clue-labs/unhandled-rejections
Browse files Browse the repository at this point in the history
Update test suite to avoid unhandled promise rejections
  • Loading branch information
WyriHaximus committed Jul 7, 2023
2 parents cff482b + 1bae823 commit 9f5ac5f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 20 deletions.
33 changes: 27 additions & 6 deletions tests/DnsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,47 +28,59 @@ 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(new \Exception('reject'))));

$this->connector->connect('127.0.0.1:80');
$promise = $this->connector->connect('127.0.0.1:80');

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

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(new \Exception('reject'))));

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

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

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(new \Exception('reject'))));

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

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

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(new \Exception('reject'))));

$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
$promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');

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

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(new \Exception('reject'))));

$this->connector->connect('scheme://google.com:80/path?query#fragment');
$promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');

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

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(new \Exception('reject'))));

$this->connector->connect('scheme://google.com:80/?hostname=google.de');
$promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');

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

public function testRejectsImmediatelyIfUriIsInvalid()
Expand Down Expand Up @@ -289,6 +301,9 @@ public function testRejectionDuringDnsLookupShouldNotCreateAnyGarbageReferences(
$this->tcp->expects($this->never())->method('connect');

$promise = $this->connector->connect('example.com:80');

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

$dns->reject(new \RuntimeException('DNS failed'));
unset($promise, $dns);

Expand All @@ -310,6 +325,9 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferences()
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());

$promise = $this->connector->connect('example.com:80');

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

$dns->resolve('1.2.3.4');
$tcp->reject(new \RuntimeException('Connection failed'));
unset($promise, $dns, $tcp);
Expand All @@ -335,6 +353,9 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());

$promise = $this->connector->connect('example.com:80');

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

$dns->resolve('1.2.3.4');

unset($promise, $dns, $tcp);
Expand Down
8 changes: 7 additions & 1 deletion tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,13 @@ public function testServerEmitsErrorForClientWithInvalidCertificate()
$connector = new SecureConnector(new TcpConnector(), null, array(
'verify_peer' => false
));
$connector->connect($server->getAddress());
$promise = $connector->connect($server->getAddress());

try {
\React\Async\await($promise);
} catch (\RuntimeException $e) {
// ignore client-side exception
}

$this->setExpectedException('RuntimeException', 'handshake');

Expand Down
25 changes: 18 additions & 7 deletions tests/HappyEyeBallsConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ 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(new \Exception('reject'))));

$this->connector->connect('[::1]:80');
$promise = $this->connector->connect('[::1]:80');

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

$this->loop->run();
}
Expand All @@ -125,7 +127,9 @@ 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(new \Exception('reject'))));

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

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

$this->loop->run();
}
Expand All @@ -135,7 +139,9 @@ 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(new \Exception('reject'))));

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

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

$this->loop->run();
}
Expand All @@ -145,7 +151,9 @@ 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(new \Exception('reject'))));

$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
$promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');

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

$this->loop->run();
}
Expand All @@ -155,7 +163,9 @@ 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(new \Exception('reject'))));

$this->connector->connect('scheme://google.com:80/path?query#fragment');
$promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');

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

$this->loop->run();
}
Expand All @@ -165,7 +175,9 @@ 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(new \Exception('reject'))));

$this->connector->connect('scheme://google.com:80/?hostname=google.de');
$promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');

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

$this->loop->run();
}
Expand Down Expand Up @@ -321,7 +333,6 @@ public function throwRejection($promise)
public function provideIpvAddresses()
{
$ipv6 = array(
array(),
array('1:2:3:4'),
array('1:2:3:4', '5:6:7:8'),
array('1:2:3:4', '5:6:7:8', '9:10:11:12'),
Expand Down
5 changes: 0 additions & 5 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

Expand Down Expand Up @@ -209,7 +208,6 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

Expand Down Expand Up @@ -241,7 +239,6 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

Expand Down Expand Up @@ -273,7 +270,6 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

Expand Down Expand Up @@ -315,7 +311,6 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer
null,
function ($e) use (&$wait) {
$wait = false;
throw $e;
}
);

Expand Down
6 changes: 6 additions & 0 deletions tests/SecureConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
$this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise());

$promise = $this->connector->connect('example.com:80');

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

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

Expand Down Expand Up @@ -293,6 +296,9 @@ public function testRejectionDuringTlsHandshakeShouldNotCreateAnyGarbageReferenc
$ref->setValue($this->connector, $encryption);

$promise = $this->connector->connect('example.com:80');

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

$tcp->resolve($connection);
$tls->reject(new \RuntimeException());
unset($promise, $tcp, $tls);
Expand Down
4 changes: 3 additions & 1 deletion tests/TcpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
// dummy rejected promise to make sure autoloader has initialized all classes
class_exists('React\Socket\SocketServer', true);
class_exists('PHPUnit\Framework\Error\Warning', true);
new Promise(function () { throw new \RuntimeException('dummy'); });
$promise = new Promise(function () { throw new \RuntimeException('dummy'); });
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
unset($promise);

// keep creating dummy file handles until all file descriptors are exhausted
$fds = array();
Expand Down
5 changes: 5 additions & 0 deletions tests/TimeoutConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
$timeout = new TimeoutConnector($connector, 0.01);

$promise = $timeout->connect('example.com:80');

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

$connection->reject(new \RuntimeException('Connection failed'));
unset($promise, $connection);

Expand All @@ -232,6 +235,8 @@ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences()

$promise = $timeout->connect('example.com:80');

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

Loop::run();
unset($promise, $connection);

Expand Down

0 comments on commit 9f5ac5f

Please sign in to comment.