Skip to content

Update close handler to avoid unhandled promise rejections #144

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

Merged
merged 1 commit into from
Jul 10, 2023
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
2 changes: 2 additions & 0 deletions src/Io/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function createClient(string $uri): PromiseInterface
// either close successful connection or cancel pending connection attempt
$connecting->then(function (ConnectionInterface $connection) {
$connection->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
assert(\method_exists($connecting, 'cancel'));
$connecting->cancel();
Expand Down
2 changes: 2 additions & 0 deletions src/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public function close(): void
if ($this->promise !== null) {
$this->promise->then(function (StreamingClient $redis) {
$redis->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
assert(\method_exists($this->promise, 'cancel'));
Expand Down
11 changes: 0 additions & 11 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,4 @@ public function testClose(): void

$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
}

public function testCloseLazy(): void
{
$redis = new RedisClient($this->uri, null, $this->loop);

$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());

$redis->close();

$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
}
}
8 changes: 6 additions & 2 deletions tests/Io/FactoryStreamingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ public function testCtor(): void
public function testWillConnectWithDefaultPort(): void
{
$this->connector->expects($this->once())->method('connect')->with('redis.example.com:6379')->willReturn(reject(new \RuntimeException()));
$this->factory->createClient('redis.example.com');
$promise = $this->factory->createClient('redis.example.com');

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}

public function testWillConnectToLocalhost(): void
{
$this->connector->expects($this->once())->method('connect')->with('localhost:1337')->willReturn(reject(new \RuntimeException()));
$this->factory->createClient('localhost:1337');
$promise = $this->factory->createClient('localhost:1337');

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
}

public function testWillResolveIfConnectorResolves(): void
Expand Down
10 changes: 8 additions & 2 deletions tests/RedisClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew
new Promise(function () { })
);

$this->redis->ping();
$promise = $this->redis->ping();

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$deferred->reject($error);

$this->redis->ping();
Expand Down Expand Up @@ -308,7 +311,10 @@ public function testCloseAfterPingWillEmitCloseWithoutErrorWhenUnderlyingClientC
$this->redis->on('error', $this->expectCallableNever());
$this->redis->on('close', $this->expectCallableOnce());

$this->redis->ping();
$promise = $this->redis->ping();

$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection

$this->redis->close();
}

Expand Down