diff --git a/src/LazyClient.php b/src/LazyClient.php index f901546..bfb2fef 100644 --- a/src/LazyClient.php +++ b/src/LazyClient.php @@ -201,7 +201,7 @@ public function idle() { --$this->pending; - if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed) { + if ($this->pending < 1 && $this->idlePeriod >= 0 && !$this->subscribed && !$this->psubscribed && $this->promise !== null) { $idleTimer =& $this->idleTimer; $promise =& $this->promise; $idleTimer = $this->loop->addTimer($this->idlePeriod, function () use (&$idleTimer, &$promise) { diff --git a/tests/LazyClientTest.php b/tests/LazyClientTest.php index bf3ceb6..1faf779 100644 --- a/tests/LazyClientTest.php +++ b/tests/LazyClientTest.php @@ -573,6 +573,33 @@ public function testUnsubscribeAfterSubscribeWillResolveWhenUnderlyingClientReso $promise->then($this->expectCallableOnceWith(array('unsubscribe', 'foo', 0))); } + public function testBlpopWillRejectWhenUnderlyingClientClosesWhileWaitingForResponse() + { + $closeHandler = null; + $deferred = new Deferred(); + $client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock(); + $client->expects($this->once())->method('__call')->with('blpop')->willReturn($deferred->promise()); + $client->expects($this->any())->method('on')->withConsecutive( + array('close', $this->callback(function ($arg) use (&$closeHandler) { + $closeHandler = $arg; + return true; + })) + ); + + $this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client)); + + $this->loop->expects($this->never())->method('addTimer'); + + $promise = $this->client->blpop('list'); + + $this->assertTrue(is_callable($closeHandler)); + $closeHandler(); + + $deferred->reject($e = new \RuntimeException()); + + $promise->then(null, $this->expectCallableOnceWith($e)); + } + public function createCallableMockWithOriginalConstructorDisabled($array) { if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {