Skip to content

Commit

Permalink
Merge pull request #144 from clue-labs/unhandled-rejections
Browse files Browse the repository at this point in the history
Update close handler to avoid unhandled promise rejections
  • Loading branch information
SimonFrings authored Jul 10, 2023
2 parents 8cda17d + 741175d commit a39fa70
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
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

0 comments on commit a39fa70

Please sign in to comment.