diff --git a/src/main/java/io/lettuce/core/RedisCommandBuilder.java b/src/main/java/io/lettuce/core/RedisCommandBuilder.java index 09257e34a5..a1197c1f07 100644 --- a/src/main/java/io/lettuce/core/RedisCommandBuilder.java +++ b/src/main/java/io/lettuce/core/RedisCommandBuilder.java @@ -2567,20 +2567,20 @@ public Command xtrim(K key, boolean approximateTrimming, long count) private static String getLowerValue(Range range) { - if (range.getLower().equals(Boundary.unbounded())) { - return "-"; - } + Boundary boundary = range.getLower(); - return range.getLower().getValue(); + return boundary.equals(Boundary.unbounded()) ? "-" : getRange(boundary); } private static String getUpperValue(Range range) { - if (range.getUpper().equals(Boundary.unbounded())) { - return "+"; - } + Boundary boundary = range.getUpper(); + + return boundary.equals(Boundary.unbounded()) ? "+" : getRange(boundary); + } - return range.getUpper().getValue(); + private static String getRange(Boundary boundary) { + return !boundary.isIncluding() ? "(" + boundary.getValue() : boundary.getValue(); } public Command>> xread(XReadArgs xReadArgs, StreamOffset[] streams) { diff --git a/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java index a1f8d7e549..dfc97fa8cb 100644 --- a/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/StreamCommandIntegrationTests.java @@ -180,6 +180,20 @@ void xrange() { assertThat(range.get(0).getBody()).isEqualTo(expectedBody); } + @Test + @EnabledOnCommand("XAUTOCLAIM") // Redis 6.2 + void xrangeRanges() { + + String id1 = redis.xadd(key, Collections.singletonMap("key", "value")); + String id2 = redis.xadd(key, Collections.singletonMap("key", "value")); + String id3 = redis.xadd(key, Collections.singletonMap("key", "value")); + + assertThat(redis.xrange(key, Range.unbounded())).hasSize(3); + assertThat(redis.xrange(key, Range.from(Range.Boundary.including(id1), Range.Boundary.excluding(id3)))).hasSize(2); + assertThat(redis.xrange(key, Range.from(Range.Boundary.excluding(id1), Range.Boundary.excluding(id3)))).hasSize(1); + assertThat(redis.xrange(key, Range.from(Range.Boundary.excluding(id1), Range.Boundary.including(id3)))).hasSize(2); + } + @Test void xrevrange() { @@ -410,6 +424,23 @@ void xpending() { assertThat(message.getRedeliveryCount()).isEqualTo(1); } + @Test + @EnabledOnCommand("XAUTOCLAIM") // Redis 6.2 + void xpendingRanges() { + + redis.xgroupCreate(StreamOffset.latest(key), "group", XGroupCreateArgs.Builder.mkstream()); + String id1 = redis.xadd(key, Collections.singletonMap("key", "value")); + String id2 = redis.xadd(key, Collections.singletonMap("key", "value")); + + redis.xreadgroup(Consumer.from("group", "consumer1"), StreamOffset.lastConsumed(key)); + + assertThat(redis.xpending(key, "group", Range.unbounded(), Limit.from(10))).hasSize(2); + assertThat(redis.xpending(key, "group", Range.from(Range.Boundary.including(id1), Range.Boundary.excluding(id2)), + Limit.from(10))).hasSize(1); + assertThat(redis.xpending(key, "group", Range.from(Range.Boundary.including(id1), Range.Boundary.including(id2)), + Limit.from(10))).hasSize(2); + } + @Test void xpendingWithoutMessages() {