From 579169d5308141cb3e7fab90f0aaf9a9a16ee653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 8 Dec 2023 16:32:50 +0100 Subject: [PATCH 1/3] Avoid referencing unneeded explicit loop instance --- composer.json | 8 +++++- tests/FunctionalTest.php | 54 ++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/composer.json b/composer.json index c1414b3..5601a46 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "react/event-loop": "^1.2", "react/promise": "^3 || ^2.0 || ^1.1", "react/promise-timer": "^1.10", - "react/socket": "^1.12" + "react/socket": "dev-cancel-happy as 1.15.0" }, "require-dev": { "clue/block-react": "^1.5", @@ -29,5 +29,11 @@ }, "autoload-dev": { "psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" } + }, + "repositories": { + "clue": { + "type": "vcs", + "url": "https://github.com/clue-labs/socket" + } } } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index be36eda..d66e80b 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -3,15 +3,14 @@ namespace Clue\Tests\React\Redis; use Clue\React\Redis\RedisClient; -use React\EventLoop\StreamSelectLoop; +use React\EventLoop\Loop; use React\Promise\Deferred; use React\Promise\PromiseInterface; use function Clue\React\Block\await; +use function React\Promise\Timer\timeout; class FunctionalTest extends TestCase { - /** @var StreamSelectLoop */ - private $loop; /** @var string */ private $uri; @@ -22,30 +21,28 @@ public function setUp(): void if ($this->uri === '') { $this->markTestSkipped('No REDIS_URI environment variable given'); } - - $this->loop = new StreamSelectLoop(); } public function testPing(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $promise = $redis->ping(); $this->assertInstanceOf(PromiseInterface::class, $promise); - $ret = await($promise, $this->loop); + $ret = await($promise); $this->assertEquals('PONG', $ret); } public function testPingLazy(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $promise = $redis->ping(); $this->assertInstanceOf(PromiseInterface::class, $promise); - $ret = await($promise, $this->loop); + $ret = await($promise); $this->assertEquals('PONG', $ret); } @@ -55,11 +52,11 @@ public function testPingLazy(): void */ public function testPingLazyWillNotBlockLoop(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->ping(); - $this->loop->run(); + Loop::run(); } /** @@ -67,58 +64,58 @@ public function testPingLazyWillNotBlockLoop(): void */ public function testLazyClientWithoutCommandsWillNotBlockLoop(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); - $this->loop->run(); + Loop::run(); unset($redis); } public function testMgetIsNotInterpretedAsSubMessage(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->mset('message', 'message', 'channel', 'channel', 'payload', 'payload'); $promise = $redis->mget('message', 'channel', 'payload')->then($this->expectCallableOnce()); $redis->on('message', $this->expectCallableNever()); - await($promise, $this->loop); + await($promise); } public function testPipeline(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->set('a', 1)->then($this->expectCallableOnceWith('OK')); $redis->incr('a')->then($this->expectCallableOnceWith(2)); $redis->incr('a')->then($this->expectCallableOnceWith(3)); $promise = $redis->get('a')->then($this->expectCallableOnceWith('3')); - await($promise, $this->loop); + await($promise); } public function testInvalidCommand(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $promise = $redis->doesnotexist(1, 2, 3); $this->expectException(\Exception::class); - await($promise, $this->loop); + await($promise); } public function testMultiExecEmpty(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->multi()->then($this->expectCallableOnceWith('OK')); $promise = $redis->exec()->then($this->expectCallableOnceWith([])); - await($promise, $this->loop); + await($promise); } public function testMultiExecQueuedExecHasValues(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->multi()->then($this->expectCallableOnceWith('OK')); $redis->set('b', 10)->then($this->expectCallableOnceWith('QUEUED')); @@ -127,13 +124,13 @@ public function testMultiExecQueuedExecHasValues(): void $redis->ttl('b')->then($this->expectCallableOnceWith('QUEUED')); $promise = $redis->exec()->then($this->expectCallableOnceWith(['OK', 1, 12, 20])); - await($promise, $this->loop); + await($promise); } public function testPubSub(): void { - $consumer = new RedisClient($this->uri, null, $this->loop); - $producer = new RedisClient($this->uri, null, $this->loop); + $consumer = new RedisClient($this->uri); + $producer = new RedisClient($this->uri); $channel = 'channel:test:' . mt_rand(); @@ -148,12 +145,15 @@ public function testPubSub(): void })->then($this->expectCallableOnce()); // expect "message" event to take no longer than 0.1s - await($deferred->promise(), $this->loop, 0.1); + + await(timeout($deferred->promise(), 0.1)); + + await($consumer->unsubscribe($channel)); } public function testClose(): void { - $redis = new RedisClient($this->uri, null, $this->loop); + $redis = new RedisClient($this->uri); $redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce()); From 9cae74a00a6597068b89701105be4201441aa4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 10 Dec 2023 17:58:50 +0100 Subject: [PATCH 2/3] Update to use reactphp/async instead of clue/reactphp-block --- composer.json | 4 ++-- tests/FunctionalTest.php | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 5601a46..f0eda34 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,9 @@ "react/socket": "dev-cancel-happy as 1.15.0" }, "require-dev": { - "clue/block-react": "^1.5", "phpstan/phpstan": "1.10.15 || 1.4.10", - "phpunit/phpunit": "^9.6 || ^7.5" + "phpunit/phpunit": "^9.6 || ^7.5", + "react/async": "^4.2 || ^3.2" }, "autoload": { "psr-4": { "Clue\\React\\Redis\\": "src/" } diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index d66e80b..767c530 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -6,7 +6,7 @@ use React\EventLoop\Loop; use React\Promise\Deferred; use React\Promise\PromiseInterface; -use function Clue\React\Block\await; +use function React\Async\await; use function React\Promise\Timer\timeout; class FunctionalTest extends TestCase @@ -27,8 +27,8 @@ public function testPing(): void { $redis = new RedisClient($this->uri); + /** @var PromiseInterface */ $promise = $redis->ping(); - $this->assertInstanceOf(PromiseInterface::class, $promise); $ret = await($promise); @@ -39,8 +39,8 @@ public function testPingLazy(): void { $redis = new RedisClient($this->uri); + /** @var PromiseInterface */ $promise = $redis->ping(); - $this->assertInstanceOf(PromiseInterface::class, $promise); $ret = await($promise); @@ -77,6 +77,7 @@ public function testMgetIsNotInterpretedAsSubMessage(): void $redis->mset('message', 'message', 'channel', 'channel', 'payload', 'payload'); + /** @var PromiseInterface */ $promise = $redis->mget('message', 'channel', 'payload')->then($this->expectCallableOnce()); $redis->on('message', $this->expectCallableNever()); @@ -90,6 +91,8 @@ public function testPipeline(): void $redis->set('a', 1)->then($this->expectCallableOnceWith('OK')); $redis->incr('a')->then($this->expectCallableOnceWith(2)); $redis->incr('a')->then($this->expectCallableOnceWith(3)); + + /** @var PromiseInterface */ $promise = $redis->get('a')->then($this->expectCallableOnceWith('3')); await($promise); @@ -98,6 +101,8 @@ public function testPipeline(): void public function testInvalidCommand(): void { $redis = new RedisClient($this->uri); + + /** @var PromiseInterface */ $promise = $redis->doesnotexist(1, 2, 3); $this->expectException(\Exception::class); @@ -108,6 +113,8 @@ public function testMultiExecEmpty(): void { $redis = new RedisClient($this->uri); $redis->multi()->then($this->expectCallableOnceWith('OK')); + + /** @var PromiseInterface */ $promise = $redis->exec()->then($this->expectCallableOnceWith([])); await($promise); @@ -122,6 +129,8 @@ public function testMultiExecQueuedExecHasValues(): void $redis->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED')); $redis->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED')); $redis->ttl('b')->then($this->expectCallableOnceWith('QUEUED')); + + /** @var PromiseInterface */ $promise = $redis->exec()->then($this->expectCallableOnceWith(['OK', 1, 12, 20])); await($promise); @@ -135,6 +144,7 @@ public function testPubSub(): void $channel = 'channel:test:' . mt_rand(); // consumer receives a single message + /** @var Deferred */ $deferred = new Deferred(); $consumer->on('message', $this->expectCallableOnce()); $consumer->on('message', [$deferred, 'resolve']); @@ -148,7 +158,9 @@ public function testPubSub(): void await(timeout($deferred->promise(), 0.1)); - await($consumer->unsubscribe($channel)); + /** @var PromiseInterface */ + $promise = $consumer->unsubscribe($channel); + await($promise); } public function testClose(): void From d1b2e0fd6b88b3a9127bb5d591b38aae215d104b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 15 Dec 2023 23:15:08 +0100 Subject: [PATCH 3/3] Update to stable Socket v1.15.0 release --- composer.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/composer.json b/composer.json index f0eda34..72642e9 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "react/event-loop": "^1.2", "react/promise": "^3 || ^2.0 || ^1.1", "react/promise-timer": "^1.10", - "react/socket": "dev-cancel-happy as 1.15.0" + "react/socket": "^1.15" }, "require-dev": { "phpstan/phpstan": "1.10.15 || 1.4.10", @@ -29,11 +29,5 @@ }, "autoload-dev": { "psr-4": { "Clue\\Tests\\React\\Redis\\": "tests/" } - }, - "repositories": { - "clue": { - "type": "vcs", - "url": "https://github.com/clue-labs/socket" - } } }