Skip to content

Commit bfcf142

Browse files
committed
Update test suite to avoid unhandled promise rejections
1 parent 3be0fc8 commit bfcf142

7 files changed

+23
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"react/promise": "^3.0 || ^2.7 || ^1.2.1"
3333
},
3434
"require-dev": {
35-
"phpunit/phpunit": "^9.5 || ^4.8.35",
35+
"phpunit/phpunit": "^9.5 || ^5.7 || ^4.8.35",
3636
"react/async": "^4 || ^3 || ^2",
3737
"react/promise-timer": "^1.9"
3838
},

tests/FunctionalResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public function testResolveShouldNotCauseGarbageReferencesWhenUsingInvalidNamese
187187
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
188188

189189
$promise = $this->resolver->resolve('google.com');
190+
191+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
192+
190193
unset($promise);
191194

192195
$this->assertEquals(0, gc_collect_cycles());
@@ -205,6 +208,9 @@ public function testResolveCachedShouldNotCauseGarbageReferencesWhenUsingInvalid
205208
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
206209

207210
$promise = $this->resolver->resolve('google.com');
211+
212+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
213+
208214
unset($promise);
209215

210216
$this->assertEquals(0, gc_collect_cycles());

tests/Query/CoopExecutorTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public function testQueryTwiceWillPassExactQueryToBaseExecutorTwiceWhenFirstQuer
107107

108108
$connector = new CoopExecutor($base);
109109

110-
$connector->query($query);
110+
$promise = $connector->query($query);
111+
112+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
111113

112114
$deferred->reject(new RuntimeException());
113115

tests/Query/RetryExecutorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ public function queryShouldNotCauseGarbageReferencesOnTimeoutErrors()
258258
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
259259

260260
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
261-
$retryExecutor->query($query);
261+
$promise = $retryExecutor->query($query);
262+
263+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
262264

263265
$this->assertEquals(0, gc_collect_cycles());
264266
}
@@ -322,7 +324,9 @@ public function queryShouldNotCauseGarbageReferencesOnNonTimeoutErrors()
322324
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
323325

324326
$query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN);
325-
$retryExecutor->query($query);
327+
$promise = $retryExecutor->query($query);
328+
329+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
326330

327331
$this->assertEquals(0, gc_collect_cycles());
328332
}

tests/Query/SelectiveTransportExecutorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ public function testRejectedPromiseAfterTruncatedResponseShouldNotCreateAnyGarba
226226
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
227227

228228
$promise = $this->executor->query($query);
229+
230+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
231+
229232
unset($promise);
230233

231234
$this->assertEquals(0, gc_collect_cycles());

tests/Query/TcpTransportExecutorTest.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,12 @@ public function testQueryRejectsWhenClientKeepsSendingWhenServerClosesSocketWith
370370
$query = new Query('google' . str_repeat('.com', 100), Message::TYPE_A, Message::CLASS_IN);
371371

372372
// send a bunch of queries and keep reference to last promise
373+
$exception = null;
373374
for ($i = 0; $i < 2000; ++$i) {
374375
$promise = $executor->query($query);
376+
$promise->then(null, function (\Exception $reason) use (&$exception) {
377+
$exception = $reason;
378+
});
375379
}
376380

377381
$client = stream_socket_accept($server);
@@ -399,11 +403,6 @@ public function testQueryRejectsWhenClientKeepsSendingWhenServerClosesSocketWith
399403
restore_error_handler();
400404
$this->assertNull($error);
401405

402-
$exception = null;
403-
$promise->then(null, function ($reason) use (&$exception) {
404-
$exception = $reason;
405-
});
406-
407406
// expect EPIPE (Broken pipe), except for macOS kernel race condition or legacy HHVM
408407
$this->setExpectedException(
409408
'RuntimeException',
@@ -472,7 +471,6 @@ public function testQueryKeepsPendingIfServerSendsIncompleteMessageLength()
472471
null,
473472
function ($e) use (&$wait) {
474473
$wait = false;
475-
throw $e;
476474
}
477475
);
478476

@@ -509,7 +507,6 @@ public function testQueryKeepsPendingIfServerSendsIncompleteMessageBody()
509507
null,
510508
function ($e) use (&$wait) {
511509
$wait = false;
512-
throw $e;
513510
}
514511
);
515512

tests/Query/UdpTransportExecutorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ public function testQueryKeepsPendingIfServerSendsInvalidMessage()
269269
null,
270270
function ($e) use (&$wait) {
271271
$wait = false;
272-
throw $e;
273272
}
274273
);
275274

@@ -307,7 +306,6 @@ public function testQueryKeepsPendingIfServerSendsInvalidId()
307306
null,
308307
function ($e) use (&$wait) {
309308
$wait = false;
310-
throw $e;
311309
}
312310
);
313311

0 commit comments

Comments
 (0)