Skip to content

Commit

Permalink
Update close handler to avoid unhandled promise rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 1, 2025
1 parent 42b3c0a commit 6070b21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/Io/LazyDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public function close()
if ($this->promise !== null) {
$this->promise->then(function (DatabaseInterface $db) {
$db->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
$this->promise->cancel();
Expand Down
6 changes: 5 additions & 1 deletion src/Io/ProcessIoDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function quit()
$promise = $this->send('close', array());

if ($this->process->stdin === $this->process->stdout) {
$promise->then(function () { $this->process->stdin->close(); });
$promise->then(function () {
$this->process->stdin->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
} else {
$this->process->stdin->end();
}
Expand Down
20 changes: 13 additions & 7 deletions tests/Io/LazyDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ public function testExecAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreateN
new Promise(function () { })
);

$this->db->exec('CREATE');
$promise = $this->db->exec('CREATE');
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$deferred->reject($error);

$this->db->exec('CREATE');
Expand Down Expand Up @@ -392,9 +394,11 @@ public function testQueryAfterPreviousFactoryRejectsUnderlyingDatabaseWillCreate
$this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls(
$deferred->promise(),
new Promise(function () { })
);
);

$promise = $this->db->query('CREATE');
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$this->db->query('CREATE');
$deferred->reject($error);

$this->db->query('CREATE');
Expand All @@ -408,7 +412,7 @@ public function testQueryAfterPreviousUnderlyingDatabaseAlreadyClosedWillCreateN
$this->factory->expects($this->exactly(2))->method('open')->willReturnOnConsecutiveCalls(
\React\Promise\resolve($client),
new Promise(function () { })
);
);

$this->db->query('CREATE');
$client->emit('close');
Expand All @@ -433,7 +437,7 @@ public function testQueryAfterQueryWillNotStartIdleTimerWhenFirstQueryResolves()
$client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls(
$deferred->promise(),
new Promise(function () { })
);
);

$this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client));

Expand All @@ -451,7 +455,7 @@ public function testQueryAfterQueryWillStartAndCancelIdleTimerWhenSecondQuerySta
$client->expects($this->exactly(2))->method('query')->willReturnOnConsecutiveCalls(
$deferred->promise(),
new Promise(function () { })
);
);

$this->factory->expects($this->once())->method('open')->willReturn(\React\Promise\resolve($client));

Expand Down Expand Up @@ -524,7 +528,9 @@ public function testCloseAfterExecWillEmitCloseWithoutErrorWhenUnderlyingDatabas
$this->db->on('error', $this->expectCallableNever());
$this->db->on('close', $this->expectCallableOnce());

$this->db->exec('CREATE');
$promise = $this->db->exec('CREATE');
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$this->db->close();
}

Expand Down

0 comments on commit 6070b21

Please sign in to comment.