Skip to content

Commit 541fe16

Browse files
authored
Merge pull request #109 from clue-labs/lazy-close
Fix explicit close() on lazy connection when connection is active
2 parents b277c24 + bc67e3a commit 541fe16

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
@@ -701,6 +701,25 @@ public function testCloseAfterPingWillCancelTimerWhenPingFromUnderlyingConnectio
701701
$connection->close();
702702
}
703703

704+
public function testCloseAfterPingHasResolvedWillCloseUnderlyingConnectionWithoutTryingToCancelConnection()
705+
{
706+
$base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(array('ping', 'close'))->disableOriginalConstructor()->getMock();
707+
$base->expects($this->once())->method('ping')->willReturn(\React\Promise\resolve());
708+
$base->expects($this->once())->method('close')->willReturnCallback(function () use ($base) {
709+
$base->emit('close');
710+
});
711+
712+
$factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock();
713+
$factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base));
714+
715+
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
716+
717+
$connection = new LazyConnection($factory, '', $loop);
718+
719+
$connection->ping();
720+
$connection->close();
721+
}
722+
704723
public function testCloseAfterQuitAfterPingWillCloseUnderlyingConnectionWhenQuitIsStillPending()
705724
{
706725
$base = $this->getMockBuilder('React\MySQL\ConnectionInterface')->getMock();

0 commit comments

Comments
 (0)