From 47896f1be78fcbdd5a2b84e4c088b8585aa21f62 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Mon, 16 Jan 2023 18:46:59 +0200 Subject: [PATCH] Extended core support by implementing BLMPOP command (#1015) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added support for LMPOP command * Added support for BLMPOP command Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss --- src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/BLMPOP.php | 15 +++ tests/Predis/Command/Redis/BLMPOP_Test.php | 146 +++++++++++++++++++++ tests/Predis/Command/Redis/LMPOP_Test.php | 2 - 5 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 src/Command/Redis/BLMPOP.php create mode 100644 tests/Predis/Command/Redis/BLMPOP_Test.php diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 49a341ec..a08e77a8 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -44,6 +44,7 @@ use Predis\Command\CommandInterface; * @method $this bitop($operation, $destkey, $key) * @method $this bitfield($key, $subcommand, ...$subcommandArg) * @method $this bitpos($key, $bit, $start = null, $end = null) + * @method $this blmpop(int $timeout, array $keys, string $modifier = 'left', int $count = 1) * @method $this bzpopmax(array $keys, int $timeout) * @method $this bzpopmin(array $keys, int $timeout) * @method $this bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 39339477..84ff643a 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -53,6 +53,7 @@ use Predis\Response\Status; * @method int bitop($operation, $destkey, $key) * @method array|null bitfield(string $key, $subcommand, ...$subcommandArg) * @method int bitpos(string $key, $bit, $start = null, $end = null) + * @method array blmpop(int $timeout, array $keys, string $modifier = 'left', int $count = 1) * @method array bzpopmax(array $keys, int $timeout) * @method array bzpopmin(array $keys, int $timeout) * @method array bzmpop(int $timeout, array $keys, string $modifier = 'min', int $count = 1) diff --git a/src/Command/Redis/BLMPOP.php b/src/Command/Redis/BLMPOP.php new file mode 100644 index 00000000..cd165336 --- /dev/null +++ b/src/Command/Redis/BLMPOP.php @@ -0,0 +1,15 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Predis\Command\Redis; + +/** + * @group commands + * @group realm-list + */ +class BLMPOP_Test extends PredisCommandTestCase +{ + /** + * {@inheritdoc} + */ + protected function getExpectedCommand(): string + { + return BLMPOP::class; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedId(): string + { + return 'BLMPOP'; + } + + /** + * @group disconnected + * @dataProvider argumentsProvider + */ + public function testFilterArguments(array $actualArguments, array $expectedArguments): void + { + $command = $this->getCommand(); + $command->setArguments($actualArguments); + + $this->assertSame($expectedArguments, $command->getArguments()); + } + + /** + * @group disconnected + */ + public function testParseResponse(): void + { + $raw = ['key', ['elem1', 'elem2']]; + $expected = ['key' => ['elem1', 'elem2']]; + + $command = $this->getCommand(); + + $this->assertSame($expected, $command->parseResponse($raw)); + } + + /** + * @group connected + * @dataProvider listProvider + * @param int $timeout + * @param array $listArguments + * @param string $key + * @param string $modifier + * @param int $count + * @param array $expectedResponse + * @param array $expectedModifiedList + * @return void + * @requiresRedisVersion >= 7.0.0 + */ + public function testPopElementsFromGivenList( + int $timeout, + array $listArguments, + string $key, + string $modifier, + int $count, + array $expectedResponse, + array $expectedModifiedList + ): void { + $redis = $this->getClient(); + + $redis->lpush(...$listArguments); + $actualResponse = $redis->blmpop($timeout, ['key1', $key], $modifier, $count); + + $this->assertSame($expectedResponse, $actualResponse); + $this->assertSame($expectedModifiedList, $redis->lrange($key, 0, -1)); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + [1, ['key']], + [1, 1, 'key', 'LEFT'] + ], + 'with LEFT/RIGHT argument' => [ + [1, ['key'], 'right'], + [1, 1, 'key', 'RIGHT'] + ], + 'with COUNT argument' => [ + [1, ['key'], 'left', 2], + [1, 1, 'key', 'LEFT', 'COUNT', 2] + ], + 'with all arguments' => [ + [1, ['key1', 'key2'], 'right', 2], + [1, 2, 'key1', 'key2', 'RIGHT', 'COUNT', 2] + ] + ]; + } + + public function listProvider(): array + { + return [ + 'pops single element - left' => [ + 1, + ['key', 'elem1', 'elem2', 'elem3'], + 'key', + 'left', + 1, + ['key' => ['elem3']], + ['elem2', 'elem1'] + ], + 'pops single element - right' => [ + 1, + ['key', 'elem1', 'elem2', 'elem3'], + 'key', + 'right', + 1, + ['key' => ['elem1']], + ['elem3', 'elem2'] + ], + 'pops multiple elements' => [ + 1, + ['key', 'elem1', 'elem2', 'elem3'], + 'key', + 'right', + 2, + ['key' => ['elem1', 'elem2']], + ['elem3'], + ], + ]; + } +} diff --git a/tests/Predis/Command/Redis/LMPOP_Test.php b/tests/Predis/Command/Redis/LMPOP_Test.php index 2ed2f50d..d83b8fa5 100644 --- a/tests/Predis/Command/Redis/LMPOP_Test.php +++ b/tests/Predis/Command/Redis/LMPOP_Test.php @@ -11,8 +11,6 @@ namespace Predis\Command\Redis; -use Predis\Response\ServerException; - /** * @group commands * @group realm-list