Skip to content

Commit

Permalink
Merge pull request #152 from clue-labs/v2-promise-v3
Browse files Browse the repository at this point in the history
[2.x] Forward compatibility with Promise v3
  • Loading branch information
SimonFrings committed Jan 4, 2024
2 parents 39e156d + 0b9f524 commit 5371af5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
"clue/redis-protocol": "0.3.*",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/event-loop": "^1.2",
"react/promise": "^2.0 || ^1.1",
"react/promise-timer": "^1.8",
"react/socket": "^1.9"
"react/promise": "^3 || ^2.0 || ^1.1",
"react/promise-timer": "^1.9",
"react/socket": "^1.12"
},
"require-dev": {
"clue/block-react": "^1.1",
"clue/block-react": "^1.5",
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36"
},
"autoload": {
Expand Down
2 changes: 2 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function createClient($uri)
// either close successful connection or cancel pending connection attempt
$connecting->then(function (ConnectionInterface $connection) {
$connection->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
$connecting->cancel();
});
Expand Down
2 changes: 2 additions & 0 deletions src/LazyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public function close()
if ($this->promise !== null) {
$this->promise->then(function (Client $redis) {
$redis->close();
}, function () {
// ignore to avoid reporting unhandled rejection
});
if ($this->promise !== null) {
$this->promise->cancel();
Expand Down
6 changes: 3 additions & 3 deletions tests/FactoryLazyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()

public function testWillConnectWithDefaultPort()
{
$this->connector->expects($this->never())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\reject(new \RuntimeException()));
$this->connector->expects($this->never())->method('connect');
$this->factory->createLazyClient('redis.example.com');
}

public function testWillConnectToLocalhost()
{
$this->connector->expects($this->never())->method('connect')->with('localhost:1337')->willReturn(Promise\reject(new \RuntimeException()));
$this->connector->expects($this->never())->method('connect');
$this->factory->createLazyClient('localhost:1337');
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public function testWillWriteSelectCommandIfRedisUnixUriContainsDbQueryParameter

public function testWillRejectIfConnectorRejects()
{
$this->connector->expects($this->never())->method('connect')->with('127.0.0.1:2')->willReturn(Promise\reject(new \RuntimeException()));
$this->connector->expects($this->never())->method('connect');
$redis = $this->factory->createLazyClient('redis://127.0.0.1:2');

$this->assertInstanceOf('Clue\React\Redis\Client', $redis);
Expand Down
8 changes: 6 additions & 2 deletions tests/FactoryStreamingClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ public function testCtor()
public function testWillConnectWithDefaultPort()
{
$this->connector->expects($this->once())->method('connect')->with('redis.example.com:6379')->willReturn(Promise\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()
{
$this->connector->expects($this->once())->method('connect')->with('localhost:1337')->willReturn(Promise\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()
Expand Down
32 changes: 19 additions & 13 deletions tests/LazyClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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 @@ -213,7 +216,7 @@ public function testPingAfterPingWillNotStartIdleTimerWhenFirstPingResolves()

$this->redis->ping();
$this->redis->ping();
$deferred->resolve();
$deferred->resolve(null);
}

public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStartsAfterFirstResolves()
Expand All @@ -232,15 +235,15 @@ public function testPingAfterPingWillStartAndCancelIdleTimerWhenSecondPingStarts
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);

$this->redis->ping();
$deferred->resolve();
$deferred->resolve(null);
$this->redis->ping();
}

public function testPingFollowedByIdleTimerWillCloseUnderlyingConnectionWithoutCloseEvent()
{
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('close')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
$client->expects($this->once())->method('close');

$this->factory->expects($this->once())->method('createClient')->willReturn(\React\Promise\resolve($client));

Expand Down Expand Up @@ -295,14 +298,17 @@ 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();
}

public function testCloseAfterPingWillCloseUnderlyingClientConnectionWhenAlreadyResolved()
{
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
$client->expects($this->once())->method('close');

$deferred = new Deferred();
Expand All @@ -327,7 +333,7 @@ public function testCloseAfterPingWillCancelIdleTimerWhenPingIsAlreadyResolved()
$this->loop->expects($this->once())->method('cancelTimer')->with($timer);

$this->redis->ping();
$deferred->resolve();
$deferred->resolve(null);
$this->redis->close();
}

Expand Down Expand Up @@ -404,7 +410,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
$error = new \RuntimeException();

$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));

$deferred = new Deferred();
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());
Expand All @@ -419,7 +425,7 @@ public function testEmitsNoErrorEventWhenUnderlyingClientEmitsError()
public function testEmitsNoCloseEventWhenUnderlyingClientEmitsClose()
{
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));

$deferred = new Deferred();
$this->factory->expects($this->once())->method('createClient')->willReturn($deferred->promise());
Expand Down Expand Up @@ -453,7 +459,7 @@ public function testEmitsNoCloseEventButWillCancelIdleTimerWhenUnderlyingConnect
$this->redis->on('close', $this->expectCallableNever());

$this->redis->ping();
$deferred->resolve();
$deferred->resolve(null);

$this->assertTrue(is_callable($closeHandler));
$closeHandler();
Expand All @@ -463,7 +469,7 @@ public function testEmitsMessageEventWhenUnderlyingClientEmitsMessageForPubSubCh
{
$messageHandler = null;
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->once())->method('__call')->willReturn(\React\Promise\resolve(null));
$client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$messageHandler) {
if ($event === 'message') {
$messageHandler = $callback;
Expand All @@ -485,7 +491,7 @@ public function testEmitsUnsubscribeAndPunsubscribeEventsWhenUnderlyingClientClo
{
$allHandler = null;
$client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
$client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve());
$client->expects($this->exactly(6))->method('__call')->willReturn(\React\Promise\resolve(null));
$client->expects($this->any())->method('on')->willReturnCallback(function ($event, $callback) use (&$allHandler) {
if (!isset($allHandler[$event])) {
$allHandler[$event] = $callback;
Expand Down

0 comments on commit 5371af5

Please sign in to comment.