Skip to content

Commit a39fa70

Browse files
authored
Merge pull request #144 from clue-labs/unhandled-rejections
Update close handler to avoid unhandled promise rejections
2 parents 8cda17d + 741175d commit a39fa70

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

src/Io/Factory.php

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ public function createClient(string $uri): PromiseInterface
9191
// either close successful connection or cancel pending connection attempt
9292
$connecting->then(function (ConnectionInterface $connection) {
9393
$connection->close();
94+
}, function () {
95+
// ignore to avoid reporting unhandled rejection
9496
});
9597
assert(\method_exists($connecting, 'cancel'));
9698
$connecting->cancel();

src/RedisClient.php

+2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ public function close(): void
220220
if ($this->promise !== null) {
221221
$this->promise->then(function (StreamingClient $redis) {
222222
$redis->close();
223+
}, function () {
224+
// ignore to avoid reporting unhandled rejection
223225
});
224226
if ($this->promise !== null) {
225227
assert(\method_exists($this->promise, 'cancel'));

tests/FunctionalTest.php

-11
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,4 @@ public function testClose(): void
161161

162162
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
163163
}
164-
165-
public function testCloseLazy(): void
166-
{
167-
$redis = new RedisClient($this->uri, null, $this->loop);
168-
169-
$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
170-
171-
$redis->close();
172-
173-
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
174-
}
175164
}

tests/Io/FactoryStreamingClientTest.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,17 @@ public function testCtor(): void
5757
public function testWillConnectWithDefaultPort(): void
5858
{
5959
$this->connector->expects($this->once())->method('connect')->with('redis.example.com:6379')->willReturn(reject(new \RuntimeException()));
60-
$this->factory->createClient('redis.example.com');
60+
$promise = $this->factory->createClient('redis.example.com');
61+
62+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
6163
}
6264

6365
public function testWillConnectToLocalhost(): void
6466
{
6567
$this->connector->expects($this->once())->method('connect')->with('localhost:1337')->willReturn(reject(new \RuntimeException()));
66-
$this->factory->createClient('localhost:1337');
68+
$promise = $this->factory->createClient('localhost:1337');
69+
70+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
6771
}
6872

6973
public function testWillResolveIfConnectorResolves(): void

tests/RedisClientTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ public function testPingAfterPreviousFactoryRejectsUnderlyingClientWillCreateNew
161161
new Promise(function () { })
162162
);
163163

164-
$this->redis->ping();
164+
$promise = $this->redis->ping();
165+
166+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
167+
165168
$deferred->reject($error);
166169

167170
$this->redis->ping();
@@ -308,7 +311,10 @@ public function testCloseAfterPingWillEmitCloseWithoutErrorWhenUnderlyingClientC
308311
$this->redis->on('error', $this->expectCallableNever());
309312
$this->redis->on('close', $this->expectCallableOnce());
310313

311-
$this->redis->ping();
314+
$promise = $this->redis->ping();
315+
316+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
317+
312318
$this->redis->close();
313319
}
314320

0 commit comments

Comments
 (0)