Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix closing lazy connection from within rejection handler #88

Merged
merged 1 commit into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
6 changes: 4 additions & 2 deletions src/LazyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions tests/LazyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down