Skip to content

Commit 9f5ac5f

Browse files
authored
Merge pull request #307 from clue-labs/unhandled-rejections
Update test suite to avoid unhandled promise rejections
2 parents cff482b + 1bae823 commit 9f5ac5f

7 files changed

+66
-20
lines changed

tests/DnsConnectorTest.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,47 +28,59 @@ public function testPassByResolverIfGivenIp()
2828
$this->resolver->expects($this->never())->method('resolve');
2929
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('127.0.0.1:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
3030

31-
$this->connector->connect('127.0.0.1:80');
31+
$promise = $this->connector->connect('127.0.0.1:80');
32+
33+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
3234
}
3335

3436
public function testPassThroughResolverIfGivenHost()
3537
{
3638
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
3739
$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'))));
3840

39-
$this->connector->connect('google.com:80');
41+
$promise = $this->connector->connect('google.com:80');
42+
43+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
4044
}
4145

4246
public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
4347
{
4448
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('::1')));
4549
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
4650

47-
$this->connector->connect('google.com:80');
51+
$promise = $this->connector->connect('google.com:80');
52+
53+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
4854
}
4955

5056
public function testPassByResolverIfGivenCompleteUri()
5157
{
5258
$this->resolver->expects($this->never())->method('resolve');
5359
$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'))));
5460

55-
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
61+
$promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
62+
63+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
5664
}
5765

5866
public function testPassThroughResolverIfGivenCompleteUri()
5967
{
6068
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
6169
$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'))));
6270

63-
$this->connector->connect('scheme://google.com:80/path?query#fragment');
71+
$promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');
72+
73+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
6474
}
6575

6676
public function testPassThroughResolverIfGivenExplicitHost()
6777
{
6878
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4')));
6979
$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'))));
7080

71-
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
81+
$promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');
82+
83+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
7284
}
7385

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

291303
$promise = $this->connector->connect('example.com:80');
304+
305+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
306+
292307
$dns->reject(new \RuntimeException('DNS failed'));
293308
unset($promise, $dns);
294309

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

312327
$promise = $this->connector->connect('example.com:80');
328+
329+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
330+
313331
$dns->resolve('1.2.3.4');
314332
$tcp->reject(new \RuntimeException('Connection failed'));
315333
unset($promise, $dns, $tcp);
@@ -335,6 +353,9 @@ public function testRejectionAfterDnsLookupShouldNotCreateAnyGarbageReferencesAg
335353
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('1.2.3.4:80?hostname=example.com'))->willReturn($tcp->promise());
336354

337355
$promise = $this->connector->connect('example.com:80');
356+
357+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
358+
338359
$dns->resolve('1.2.3.4');
339360

340361
unset($promise, $dns, $tcp);

tests/FunctionalSecureServerTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,13 @@ public function testServerEmitsErrorForClientWithInvalidCertificate()
582582
$connector = new SecureConnector(new TcpConnector(), null, array(
583583
'verify_peer' => false
584584
));
585-
$connector->connect($server->getAddress());
585+
$promise = $connector->connect($server->getAddress());
586+
587+
try {
588+
\React\Async\await($promise);
589+
} catch (\RuntimeException $e) {
590+
// ignore client-side exception
591+
}
586592

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

tests/HappyEyeBallsConnectorTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ public function testPassByResolverIfGivenIpv6()
115115
$this->resolver->expects($this->never())->method('resolveAll');
116116
$this->tcp->expects($this->once())->method('connect')->with($this->equalTo('[::1]:80'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
117117

118-
$this->connector->connect('[::1]:80');
118+
$promise = $this->connector->connect('[::1]:80');
119+
120+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
119121

120122
$this->loop->run();
121123
}
@@ -125,7 +127,9 @@ public function testPassThroughResolverIfGivenHost()
125127
$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'))));
126128
$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'))));
127129

128-
$this->connector->connect('google.com:80');
130+
$promise = $this->connector->connect('google.com:80');
131+
132+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
129133

130134
$this->loop->run();
131135
}
@@ -135,7 +139,9 @@ public function testPassThroughResolverIfGivenHostWhichResolvesToIpv6()
135139
$this->resolver->expects($this->exactly(2))->method('resolveAll')->with($this->equalTo('google.com'), $this->anything())->will($this->returnValue(Promise\resolve(array('::1'))));
136140
$this->tcp->expects($this->exactly(2))->method('connect')->with($this->equalTo('[::1]:80?hostname=google.com'))->will($this->returnValue(Promise\reject(new \Exception('reject'))));
137141

138-
$this->connector->connect('google.com:80');
142+
$promise = $this->connector->connect('google.com:80');
143+
144+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
139145

140146
$this->loop->run();
141147
}
@@ -145,7 +151,9 @@ public function testPassByResolverIfGivenCompleteUri()
145151
$this->resolver->expects($this->never())->method('resolveAll');
146152
$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'))));
147153

148-
$this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
154+
$promise = $this->connector->connect('scheme://127.0.0.1:80/path?query#fragment');
155+
156+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
149157

150158
$this->loop->run();
151159
}
@@ -155,7 +163,9 @@ public function testPassThroughResolverIfGivenCompleteUri()
155163
$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'))));
156164
$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'))));
157165

158-
$this->connector->connect('scheme://google.com:80/path?query#fragment');
166+
$promise = $this->connector->connect('scheme://google.com:80/path?query#fragment');
167+
168+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
159169

160170
$this->loop->run();
161171
}
@@ -165,7 +175,9 @@ public function testPassThroughResolverIfGivenExplicitHost()
165175
$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'))));
166176
$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'))));
167177

168-
$this->connector->connect('scheme://google.com:80/?hostname=google.de');
178+
$promise = $this->connector->connect('scheme://google.com:80/?hostname=google.de');
179+
180+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
169181

170182
$this->loop->run();
171183
}
@@ -321,7 +333,6 @@ public function throwRejection($promise)
321333
public function provideIpvAddresses()
322334
{
323335
$ipv6 = array(
324-
array(),
325336
array('1:2:3:4'),
326337
array('1:2:3:4', '5:6:7:8'),
327338
array('1:2:3:4', '5:6:7:8', '9:10:11:12'),

tests/IntegrationTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen
174174
null,
175175
function ($e) use (&$wait) {
176176
$wait = false;
177-
throw $e;
178177
}
179178
);
180179

@@ -209,7 +208,6 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny
209208
null,
210209
function ($e) use (&$wait) {
211210
$wait = false;
212-
throw $e;
213211
}
214212
);
215213

@@ -241,7 +239,6 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat
241239
null,
242240
function ($e) use (&$wait) {
243241
$wait = false;
244-
throw $e;
245242
}
246243
);
247244

@@ -273,7 +270,6 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer
273270
null,
274271
function ($e) use (&$wait) {
275272
$wait = false;
276-
throw $e;
277273
}
278274
);
279275

@@ -315,7 +311,6 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer
315311
null,
316312
function ($e) use (&$wait) {
317313
$wait = false;
318-
throw $e;
319314
}
320315
);
321316

tests/SecureConnectorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
265265
$this->tcp->expects($this->once())->method('connect')->willReturn($tcp->promise());
266266

267267
$promise = $this->connector->connect('example.com:80');
268+
269+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
270+
268271
$tcp->reject(new \RuntimeException());
269272
unset($promise, $tcp);
270273

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

295298
$promise = $this->connector->connect('example.com:80');
299+
300+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
301+
296302
$tcp->resolve($connection);
297303
$tls->reject(new \RuntimeException());
298304
unset($promise, $tcp, $tls);

tests/TcpConnectorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded()
116116
// dummy rejected promise to make sure autoloader has initialized all classes
117117
class_exists('React\Socket\SocketServer', true);
118118
class_exists('PHPUnit\Framework\Error\Warning', true);
119-
new Promise(function () { throw new \RuntimeException('dummy'); });
119+
$promise = new Promise(function () { throw new \RuntimeException('dummy'); });
120+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
121+
unset($promise);
120122

121123
// keep creating dummy file handles until all file descriptors are exhausted
122124
$fds = array();

tests/TimeoutConnectorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences
208208
$timeout = new TimeoutConnector($connector, 0.01);
209209

210210
$promise = $timeout->connect('example.com:80');
211+
212+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
213+
211214
$connection->reject(new \RuntimeException('Connection failed'));
212215
unset($promise, $connection);
213216

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

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

238+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
239+
235240
Loop::run();
236241
unset($promise, $connection);
237242

0 commit comments

Comments
 (0)