diff --git a/examples/subscribe.php b/examples/subscribe.php index 87695ff..3dedae8 100644 --- a/examples/subscribe.php +++ b/examples/subscribe.php @@ -12,7 +12,8 @@ $client = $factory->createLazyClient('localhost'); $client->subscribe($channel)->then(function () { echo 'Now subscribed to channel ' . PHP_EOL; -}, function (Exception $e) { +}, function (Exception $e) use ($client) { + $client->close(); echo 'Unable to subscribe: ' . $e->getMessage() . PHP_EOL; }); diff --git a/src/LazyClient.php b/src/LazyClient.php index 17f80c0..f901546 100644 --- a/src/LazyClient.php +++ b/src/LazyClient.php @@ -166,8 +166,10 @@ public function close() $this->promise->then(function (Client $client) { $client->close(); }); - $this->promise->cancel(); - $this->promise = null; + if ($this->promise !== null) { + $this->promise->cancel(); + $this->promise = null; + } } if ($this->idleTimer !== null) { diff --git a/tests/LazyClientTest.php b/tests/LazyClientTest.php index 0d374d8..2038d38 100644 --- a/tests/LazyClientTest.php +++ b/tests/LazyClientTest.php @@ -310,6 +310,29 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved() $this->client->close(); } + public function testCloseAfterPingRejectsWillEmitClose() + { + $deferred = new Deferred(); + $client = $this->getMockBuilder('Clue\React\Redis\StreamingClient')->disableOriginalConstructor()->setMethods(array('__call', 'close'))->getMock(); + $client->expects($this->once())->method('__call')->willReturn($deferred->promise()); + $client->expects($this->once())->method('close')->willReturnCallback(function () use ($client) { + $client->emit('close'); + }); + + $this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client)); + + $timer = $this->getMockBuilder('React\EventLoop\TimerInterface')->getMock(); + $this->loop->expects($this->once())->method('addTimer')->willReturn($timer); + $this->loop->expects($this->once())->method('cancelTimer')->with($timer); + + $ref = $this->client; + $ref->ping()->then(null, function () use ($ref, $client) { + $ref->close(); + }); + $ref->on('close', $this->expectCallableOnce()); + $deferred->reject(new \RuntimeException()); + } + public function testEndWillCloseClientIfUnderlyingConnectionIsNotPending() { $this->client->on('close', $this->expectCallableOnce());