diff --git a/src/Io/LazyConnection.php b/src/Io/LazyConnection.php index 2dc35f2..3f5d69f 100644 --- a/src/Io/LazyConnection.php +++ b/src/Io/LazyConnection.php @@ -89,7 +89,7 @@ private function idle() { --$this->pending; - if ($this->pending < 1 && $this->idlePeriod >= 0) { + if ($this->pending < 1 && $this->idlePeriod >= 0 && $this->connecting !== null) { $this->idleTimer = $this->loop->addTimer($this->idlePeriod, function () { $this->connecting->then(function (ConnectionInterface $connection) { $this->disconnecting = $connection; diff --git a/tests/Io/LazyConnectionTest.php b/tests/Io/LazyConnectionTest.php index 18e9c22..7f4dac9 100644 --- a/tests/Io/LazyConnectionTest.php +++ b/tests/Io/LazyConnectionTest.php @@ -513,6 +513,29 @@ public function testPingWillRejectAndStartTimerWhenPingFromUnderlyingConnectionR $ret->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); } + public function testPingWillRejectAndNotStartIdleTimerWhenPingFromUnderlyingConnectionRejectsBecauseConnectionIsDead() + { + $error = new \RuntimeException(); + + $base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(array('ping', 'close'))->disableOriginalConstructor()->getMock(); + $base->expects($this->once())->method('ping')->willReturnCallback(function () use ($base, $error) { + $base->emit('close'); + return \React\Promise\reject($error); + }); + $base->expects($this->never())->method('close'); + + $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock(); + $factory->expects($this->once())->method('createConnection')->willReturn(\React\Promise\resolve($base)); + + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); + $loop->expects($this->never())->method('addTimer'); + + $connection = new LazyConnection($factory, '', $loop); + + $ret = $connection->ping(); + $ret->then($this->expectCallableNever(), $this->expectCallableOnceWith($error)); + } + public function testQuitResolvesAndEmitsCloseImmediatelyWhenConnectionIsNotAlreadyPending() { $factory = $this->getMockBuilder('React\MySQL\Factory')->disableOriginalConstructor()->getMock();