-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix XAddOptions maxlen handling and XPendingOptions validation #2985
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,7 +96,7 @@ public StreamReadOptions block(Duration timeout) { | |
*/ | ||
public StreamReadOptions count(long count) { | ||
|
||
Assert.isTrue(count > 0, "Count must be greater or equal to zero"); | ||
Assert.isTrue(count > 0, "Count must be greater than zero"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix description to represent assertion |
||
|
||
return new StreamReadOptions(block, count, noack); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.springframework.data.redis.connection; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.data.domain.Range; | ||
import org.springframework.data.redis.connection.ReactiveStreamCommands.PendingRecordsCommand; | ||
|
||
/** | ||
* Unit tests for {@link ReactiveStreamCommands}. | ||
* | ||
* @author jinkshower | ||
*/ | ||
class ReactiveStreamCommandsUnitTests { | ||
|
||
@Test // GH-2982 | ||
void pendingRecordsCommandRangeShouldThrowExceptionWhenRangeIsNull() { | ||
|
||
ByteBuffer key = ByteBuffer.wrap("my-stream".getBytes()); | ||
String groupName = "my-group"; | ||
|
||
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName); | ||
|
||
assertThatThrownBy(() -> command.range(null, 10L)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test // GH-2982 | ||
void pendingRecordsCommandRangeShouldThrowExceptionWhenCountIsNegative() { | ||
|
||
ByteBuffer key = ByteBuffer.wrap("my-stream".getBytes()); | ||
String groupName = "my-group"; | ||
|
||
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName); | ||
Range<?> range = Range.closed("0", "10"); | ||
|
||
assertThatThrownBy(() -> command.range(range, -1L)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.springframework.data.redis.connection; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.data.domain.Range; | ||
import org.springframework.data.redis.connection.RedisStreamCommands.XPendingOptions; | ||
|
||
/** | ||
* Unit tests for {@link RedisStreamCommands}. | ||
* | ||
* @author jinkshower | ||
*/ | ||
class RedisStreamCommandsUnitTests { | ||
|
||
@Test // GH-2982 | ||
void xPendingOptionsUnboundedShouldThrowExceptionWhenCountIsNegative() { | ||
|
||
assertThatThrownBy(() -> XPendingOptions.unbounded(-1L)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test // GH-2982 | ||
void xPendingOptionsRangeShouldThrowExceptionWhenRangeIsNull() { | ||
|
||
assertThatThrownBy(() -> XPendingOptions.range(null, 10L)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@Test // GH-2982 | ||
void xPendingOptionsRangeShouldThrowExceptionWhenCountIsNegative() { | ||
|
||
Range<?> range = Range.closed("0", "10"); | ||
|
||
assertThatThrownBy(() -> XPendingOptions.range(range, -1L)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,6 +206,19 @@ void addMaxLenShouldLimitSimpleMessagesSize() { | |
assertThat(message.getValue()).isEqualTo(newValue); | ||
} | ||
|
||
@ParameterizedRedisTest // GH-2982 | ||
void addNegativeMaxlenShouldThrowException() { | ||
|
||
K key = keyFactory.instance(); | ||
HV value = hashValueFactory.instance(); | ||
|
||
XAddOptions options = XAddOptions.maxlen(-1).approximateTrimming(false); | ||
|
||
assertThatThrownBy(() -> streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key), options)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing Redis/Driver behavior exceeds the scope of what we want to test. We will remove these tests during the merge. |
||
|
||
assertThat(streamOps.range(key, Range.unbounded())).isEmpty(); | ||
} | ||
|
||
@ParameterizedRedisTest // GH-2915 | ||
void addMinIdShouldEvictLowerIdMessages() { | ||
|
||
|
@@ -565,6 +578,19 @@ void pendingShouldReadMessageDetails() { | |
assertThat(pending.get(0).getTotalDeliveryCount()).isOne(); | ||
} | ||
|
||
@ParameterizedRedisTest // GH-2982 | ||
void pendingNegativeCountShouldThrowException() { | ||
K key = keyFactory.instance(); | ||
HK hashKey = hashKeyFactory.instance(); | ||
HV value = hashValueFactory.instance(); | ||
|
||
streamOps.add(key, Collections.singletonMap(hashKey, value)); | ||
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group"); | ||
|
||
assertThatThrownBy(() -> streamOps.pending(key, "my-group", Range.unbounded(), -1L)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
|
||
@ParameterizedRedisTest // GH-2465 | ||
void claimShouldReadMessageDetails() { | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For context, Lettuce and Jedis treat
maxlen
edge case differently.XAddArgs
in Lettuce assertsmaxlen > 0
whereas Jedis doesn't check onmaxlen
so it follows default redis behaviour which assertsmaxlen >=0
.My best choice was just let the user know. So I didn't put any assertion on
maxlen
. If we validate against negative values (maxlen < 0
), it could be confusing sincemaxlen
is validated differently across multiple layers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fine to resort to driver defaults.