Skip to content

Commit bc67e3a

Browse files
committed
Fix explicit close() on lazy connection when connection is active
1 parent c420ca0 commit bc67e3a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Io/LazyConnection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ public function close()
217217
$this->connecting->then(function (ConnectionInterface $connection) {
218218
$connection->close();
219219
});
220-
$this->connecting->cancel();
221-
$this->connecting = null;
220+
if ($this->connecting !== null) {
221+
$this->connecting->cancel();
222+
$this->connecting = null;
223+
}
222224
}
223225

224226
if ($this->idleTimer !== null) {

tests/Io/LazyConnectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,25 @@ public function testCloseAfterPingWillCancelTimerWhenPingFromUnderlyingConnectio
678678
$connection->close();
679679
}
680680

681+
public function testCloseAfterPingHasResolvedWillCloseUnderlyingConnectionWithoutTryingToCancelConnection()
682+
{
683+
$base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(array('ping', 'close'))->disableOriginalConstructor()->getMock();
684+
$base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve());
685+
$base->expects($this->once())->method('close')->willReturnCallback(function () use ($base) {
686+
$base->emit('close');
687+
});
688+
689+
$factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock();
690+
$factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base));
691+
692+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
693+
694+
$connection = new LazyConnection($factory, '', $loop);
695+
696+
$connection->ping();
697+
$connection->close();
698+
}
699+
681700
public function testCloseAfterQuitAfterPingWillCloseUnderlyingConnectionWhenQuitIsStillPending()
682701
{
683702
$base = $this->getMockBuilder('React\MySQL\ConnectionInterface')->getMock();

0 commit comments

Comments
 (0)