|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Predis package. |
| 5 | + * |
| 6 | + * (c) 2009-2020 Daniele Alessandri |
| 7 | + * (c) 2021-2025 Till Krüss |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Predis\Command\Redis; |
| 14 | + |
| 15 | +use Predis\ClientInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @group commands |
| 19 | + * @group realm-stream |
| 20 | + */ |
| 21 | +class XCLAIM_Test extends PredisCommandTestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
| 26 | + protected function getExpectedCommand(): string |
| 27 | + { |
| 28 | + return 'Predis\Command\Redis\XCLAIM'; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + protected function getExpectedId(): string |
| 35 | + { |
| 36 | + return 'XCLAIM'; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider argumentsProvider |
| 41 | + * @group disconnected |
| 42 | + */ |
| 43 | + public function testFilterArguments(array $actualArguments, array $expectedArguments): void |
| 44 | + { |
| 45 | + $command = $this->getCommand(); |
| 46 | + $command->setArguments($actualArguments); |
| 47 | + |
| 48 | + $this->assertSame($expectedArguments, $command->getArguments()); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @group disconnected |
| 53 | + */ |
| 54 | + public function testParseResponse(): void |
| 55 | + { |
| 56 | + $command = $this->getCommand(); |
| 57 | + |
| 58 | + $raw = [['1-1', ['key1', 'val1']], ['2-1', ['key2', 'val2']]]; |
| 59 | + $expected = ['1-1' => ['key1' => 'val1'], '2-1' => ['key2' => 'val2']]; |
| 60 | + $this->assertSame($expected, $command->parseResponse($raw)); |
| 61 | + $this->assertSame($expected, $command->parseResp3Response($raw)); |
| 62 | + |
| 63 | + // JUSTID format |
| 64 | + $raw = ['1-1', '2-1']; |
| 65 | + $expected = ['1-1', '2-1']; |
| 66 | + $this->assertSame($expected, $command->parseResponse($raw)); |
| 67 | + $this->assertSame($expected, $command->parseResp3Response($raw)); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @group disconnected |
| 72 | + */ |
| 73 | + public function testPrefixKeys(): void |
| 74 | + { |
| 75 | + $arguments = ['stream', 'group', 'consumer', 0, 'id1']; |
| 76 | + $expected = ['prefix:stream', 'group', 'consumer', 0, 'id1']; |
| 77 | + |
| 78 | + $command = $this->getCommandWithArgumentsArray($arguments); |
| 79 | + $command->prefixKeys('prefix:'); |
| 80 | + |
| 81 | + $this->assertSame($expected, $command->getArguments()); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @group connected |
| 86 | + * @requiresRedisVersion >= 5.0.0 |
| 87 | + */ |
| 88 | + public function testClaim(): void |
| 89 | + { |
| 90 | + $redis = $this->getClient(); |
| 91 | + $this->testClaimWithClient($redis); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @group connected |
| 96 | + * @requiresRedisVersion >= 5.0.0 |
| 97 | + */ |
| 98 | + public function testClaimResp3(): void |
| 99 | + { |
| 100 | + $redis = $this->getResp3Client(); |
| 101 | + $this->testClaimWithClient($redis); |
| 102 | + } |
| 103 | + |
| 104 | + private function testClaimWithClient(ClientInterface $redis): void |
| 105 | + { |
| 106 | + $redis->xadd('stream', ['key0' => 'val0'], '0-1'); |
| 107 | + $redis->xadd('stream', ['key1' => 'val1'], '1-1'); |
| 108 | + $redis->xadd('stream', ['key2' => 'val2'], '2-1'); |
| 109 | + $redis->xadd('stream', ['key3' => 'val3'], '3-1'); |
| 110 | + |
| 111 | + $redis->xgroup->create('stream', 'group', '0'); |
| 112 | + |
| 113 | + $redis->xreadgroup('group', 'consumer1', 4, null, false, 'stream', '>'); |
| 114 | + |
| 115 | + // Claim one |
| 116 | + $claimed = $redis->xclaim('stream', 'group', 'consumer2', 0, '0-1'); |
| 117 | + $this->assertSame(['0-1' => ['key0' => 'val0']], $claimed); |
| 118 | + |
| 119 | + // Claim many |
| 120 | + $claimed = $redis->xclaim('stream', 'group', 'consumer2', 0, ['1-1', '2-1']); |
| 121 | + $this->assertSame(['1-1' => ['key1' => 'val1'], '2-1' => ['key2' => 'val2']], $claimed); |
| 122 | + |
| 123 | + // Claim deleted |
| 124 | + $redis->xdel('stream', '1-1'); |
| 125 | + $claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, ['0-1', '1-1', '2-1'], null, null, null, false, true); |
| 126 | + $this->assertSame(['0-1', '2-1'], $claimed); |
| 127 | + |
| 128 | + // Claim with all options |
| 129 | + $redis->xdel('stream', '1-1'); |
| 130 | + $claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, '3-1', 10, 100, 5, true, true, '3-1'); |
| 131 | + $this->assertSame(['3-1'], $claimed); |
| 132 | + |
| 133 | + // Claim unknown |
| 134 | + $claimed = $redis->xclaim('stream', 'group', 'consumer3', 0, ['4-1']); |
| 135 | + $this->assertSame([], $claimed); |
| 136 | + } |
| 137 | + |
| 138 | + public function argumentsProvider(): array |
| 139 | + { |
| 140 | + return [ |
| 141 | + 'with default arguments' => [ |
| 142 | + ['stream', 'group', 'consumer', 0, 'id1'], |
| 143 | + ['stream', 'group', 'consumer', 0, 'id1'], |
| 144 | + ], |
| 145 | + 'with array ids' => [ |
| 146 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2']], |
| 147 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2'], |
| 148 | + ], |
| 149 | + 'with IDLE modifier' => [ |
| 150 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], 10], |
| 151 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'IDLE', 10], |
| 152 | + ], |
| 153 | + 'with TIME modifier' => [ |
| 154 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, 12345], |
| 155 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'TIME', 12345], |
| 156 | + ], |
| 157 | + 'with RETRYCOUNT modifier' => [ |
| 158 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, 5], |
| 159 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'RETRYCOUNT', 5], |
| 160 | + ], |
| 161 | + 'with FORCE modifier' => [ |
| 162 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, true], |
| 163 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'FORCE'], |
| 164 | + ], |
| 165 | + 'with JUSTID modifier' => [ |
| 166 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, false, true], |
| 167 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'JUSTID'], |
| 168 | + ], |
| 169 | + 'with LASTID modifier' => [ |
| 170 | + ['stream', 'group', 'consumer', 0, ['id1', 'id2'], null, null, null, false, false, '1-1'], |
| 171 | + ['stream', 'group', 'consumer', 0, 'id1', 'id2', 'LASTID', '1-1'], |
| 172 | + ], |
| 173 | + 'with all arguments' => [ |
| 174 | + ['stream', 'group', 'consumer', 100, ['id1', 'id2'], 10, 12345, 5, true, true, '1-1'], |
| 175 | + ['stream', 'group', 'consumer', 100, 'id1', 'id2', 'IDLE', 10, 'TIME', 12345, 'RETRYCOUNT', 5, 'FORCE', 'JUSTID', 'LASTID', '1-1'], |
| 176 | + ], |
| 177 | + ]; |
| 178 | + } |
| 179 | +} |
0 commit comments