1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2025 Till Krüss
*
* 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-stream
*/
class XCFGSET_Test extends PredisCommandTestCase
{
/**
* {@inheritdoc}
*/
protected function getExpectedCommand(): string
{
return 'Predis\Command\Redis\XCFGSET';
}
/**
* {@inheritdoc}
*/
protected function getExpectedId(): string
{
return 'XCFGSET';
}
/**
* @dataProvider argumentsProvider
* @group disconnected
*/
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
{
$this->assertSame('OK', $this->getCommand()->parseResponse('OK'));
}
/**
* @group disconnected
*/
public function testPrefixKeys(): void
{
$arguments = ['stream', 100, 1000];
$expected = ['prefix:stream', 'IDMP-DURATION', 100, 'IDMP-MAXSIZE', 1000];
$command = $this->getCommandWithArgumentsArray($arguments);
$command->prefixKeys('prefix:');
$this->assertSame($expected, $command->getArguments());
}
/**
* @group connected
* @requiresRedisVersion >= 8.5.0
*/
public function testConfigureStreamIdempotencyParameters(): void
{
$redis = $this->getClient();
// Create a stream first
$redis->xadd('stream', ['field' => 'value']);
// Configure IDMP parameters
$response = $redis->xcfgset('stream', 100, 1000);
$this->assertEquals('OK', $response);
}
/**
* @group connected
* @requiresRedisVersion >= 8.5.0
*/
public function testIIDsAreFlushedAfterDuration(): void
{
$redis = $this->getClient();
// Configure stream with very short IDMP-DURATION (2 seconds)
$redis->xadd('stream', ['field' => 'value']);
$redis->xcfgset('stream', 1, 1000);
// Add message with IID using IDMP
$id1 = $redis->xadd('stream', ['field' => 'value1'], '*', ['idmp' => ['producer1', 'iid-1']]);
// Try to add duplicate immediately - should return same ID (idempotent)
$id2 = $redis->xadd('stream', ['field' => 'value2'], '*', ['idmp' => ['producer1', 'iid-1']]);
$this->assertSame($id1, $id2);
// Wait for duration to expire (2 seconds + buffer)
sleep(2);
// Now the same IID should create a new entry (IID was flushed)
$id3 = $redis->xadd('stream', ['field' => 'value3'], '*', ['idmp' => ['producer1', 'iid-1']]);
$this->assertNotSame($id1, $id3);
}
/**
* @group connected
* @requiresRedisVersion >= 8.5.0
*/
public function testIIDsAreFlushedWhenExceedingMaxsize(): void
{
$redis = $this->getClient();
// Configure stream with small IDMP-MAXSIZE (3 IIDs)
$redis->xadd('stream', ['field' => 'value']);
$redis->xcfgset('stream', 100, 3);
// Add 3 messages with different IIDs (fills the IDMP map)
$id1 = $redis->xadd('stream', ['field' => 'value1'], '*', ['idmp' => ['producer1', 'iid-1']]);
$id2 = $redis->xadd('stream', ['field' => 'value2'], '*', ['idmp' => ['producer1', 'iid-2']]);
$id3 = $redis->xadd('stream', ['field' => 'value3'], '*', ['idmp' => ['producer1', 'iid-3']]);
// Try to add duplicate of iid-1 - should return same ID (idempotent)
$id1Dup = $redis->xadd('stream', ['field' => 'duplicate'], '*', ['idmp' => ['producer1', 'iid-1']]);
$this->assertSame($id1, $id1Dup);
// Add a 4th unique IID - this should cause oldest IID (iid-1) to be flushed
$id4 = $redis->xadd('stream', ['field' => 'value4'], '*', ['idmp' => ['producer1', 'iid-4']]);
$this->assertNotNull($id4);
// Now iid-1 should create a new entry (it was flushed due to maxsize)
$id1New = $redis->xadd('stream', ['field' => 'value5'], '*', ['idmp' => ['producer1', 'iid-1']]);
$this->assertNotSame($id1, $id1New);
}
public function argumentsProvider(): array
{
return [
'with key only' => [
['stream'],
['stream'],
],
'with IDMP-DURATION only' => [
['stream', 100],
['stream', 'IDMP-DURATION', 100],
],
'with IDMP-MAXSIZE only' => [
['stream', null, 1000],
['stream', 'IDMP-MAXSIZE', 1000],
],
'with both IDMP-DURATION and IDMP-MAXSIZE' => [
['stream', 100, 1000],
['stream', 'IDMP-DURATION', 100, 'IDMP-MAXSIZE', 1000],
],
];
}
}
|