|
1 | 1 | package redis.clients.jedis.commands.commandobjects; |
2 | 2 |
|
| 3 | + |
| 4 | +import io.redis.test.annotations.SinceRedisVersion; |
| 5 | + |
| 6 | +import static io.redis.test.utils.RedisVersion.V8_4_RC1_STRING; |
3 | 7 | import static org.hamcrest.MatcherAssert.assertThat; |
4 | 8 | import static org.hamcrest.Matchers.empty; |
5 | 9 | import static org.hamcrest.Matchers.equalTo; |
@@ -1102,4 +1106,121 @@ public void testXReadGroupAsMap() { |
1102 | 1106 | assertThat(xreadGroupConsumer2.get(streamKey).get(0).getID(), equalTo(secondMessageId)); |
1103 | 1107 | assertThat(xreadGroupConsumer2.get(streamKey).get(0).getFields(), equalTo(messageBody)); |
1104 | 1108 | } |
| 1109 | + |
| 1110 | + |
| 1111 | + @Test |
| 1112 | + @SinceRedisVersion(V8_4_RC1_STRING) |
| 1113 | + public void xreadGroupWithClaimReturnsPendingThenNewEntries_commandObjects() throws InterruptedException { |
| 1114 | + String key = "co-claim-stream"; |
| 1115 | + String group = "co-claim-group"; |
| 1116 | + String consumer = "c1"; |
| 1117 | + |
| 1118 | + exec(commandObjects.del(key)); |
| 1119 | + exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); |
| 1120 | + |
| 1121 | + // Make 3 entries pending |
| 1122 | + exec(commandObjects.xadd(key, new StreamEntryID("1-0"), Collections.singletonMap("f", "v"))); |
| 1123 | + exec(commandObjects.xadd(key, new StreamEntryID("2-0"), Collections.singletonMap("f", "v"))); |
| 1124 | + exec(commandObjects.xadd(key, new StreamEntryID("3-0"), Collections.singletonMap("f", "v"))); |
| 1125 | + |
| 1126 | + Map<String, StreamEntryID> stream = Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); |
| 1127 | + exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(3), stream)); |
| 1128 | + |
| 1129 | + Thread.sleep(60); |
| 1130 | + |
| 1131 | + // Add two fresh entries |
| 1132 | + exec(commandObjects.xadd(key, new StreamEntryID("4-0"), Collections.singletonMap("f", "v"))); |
| 1133 | + exec(commandObjects.xadd(key, new StreamEntryID("5-0"), Collections.singletonMap("f", "v"))); |
| 1134 | + |
| 1135 | + List<Map.Entry<String, List<StreamEntry>>> messages = exec( |
| 1136 | + commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(5).claim(50), stream)); |
| 1137 | + |
| 1138 | + assertThat(messages, hasSize(1)); |
| 1139 | + List<StreamEntry> entries = messages.get(0).getValue(); |
| 1140 | + org.junit.jupiter.api.Assertions.assertEquals(5, entries.size()); |
| 1141 | + |
| 1142 | + for (int i = 0; i < 3; i++) { |
| 1143 | + org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getIdleTime()); |
| 1144 | + org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getDeliveredTimes()); |
| 1145 | + } |
| 1146 | + for (int i = 3; i < 5; i++) { |
| 1147 | + org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getIdleTime()); |
| 1148 | + org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getDeliveredTimes()); |
| 1149 | + } |
| 1150 | + } |
| 1151 | + |
| 1152 | + @Test |
| 1153 | + @SinceRedisVersion(V8_4_RC1_STRING) |
| 1154 | + public void xreadGroupWithClaimNoEligiblePendingReturnsOnlyNewEntries_commandObjects() { |
| 1155 | + String key = "co-claim-noeligible"; |
| 1156 | + String group = "co-claim-group"; |
| 1157 | + String consumer = "c1"; |
| 1158 | + |
| 1159 | + exec(commandObjects.del(key)); |
| 1160 | + exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); |
| 1161 | + |
| 1162 | + // Put 2 entries in PEL |
| 1163 | + exec(commandObjects.xadd(key, new StreamEntryID("1-0"), Collections.singletonMap("f", "v"))); |
| 1164 | + exec(commandObjects.xadd(key, new StreamEntryID("2-0"), Collections.singletonMap("f", "v"))); |
| 1165 | + Map<String, StreamEntryID> stream = Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); |
| 1166 | + exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(2), stream)); |
| 1167 | + |
| 1168 | + // Add two new entries that should be returned |
| 1169 | + exec(commandObjects.xadd(key, new StreamEntryID("3-0"), Collections.singletonMap("f", "v"))); |
| 1170 | + exec(commandObjects.xadd(key, new StreamEntryID("4-0"), Collections.singletonMap("f", "v"))); |
| 1171 | + |
| 1172 | + List<Map.Entry<String, List<StreamEntry>>> messages = exec( |
| 1173 | + commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(4).claim(500), stream)); |
| 1174 | + |
| 1175 | + assertThat(messages, hasSize(1)); |
| 1176 | + List<StreamEntry> entries = messages.get(0).getValue(); |
| 1177 | + org.junit.jupiter.api.Assertions.assertEquals(2, entries.size()); |
| 1178 | + for (StreamEntry e : entries) { |
| 1179 | + org.junit.jupiter.api.Assertions.assertNull(e.getIdleTime()); |
| 1180 | + org.junit.jupiter.api.Assertions.assertNull(e.getDeliveredTimes()); |
| 1181 | + } |
| 1182 | + } |
| 1183 | + |
| 1184 | + @Test |
| 1185 | + @SinceRedisVersion(V8_4_RC1_STRING) |
| 1186 | + public void xreadGroupWithClaimAndNoAckDoesNotAddNewEntriesToPEL_commandObjects() throws InterruptedException { |
| 1187 | + String key = "co-claim-noack"; |
| 1188 | + String group = "co-claim-group"; |
| 1189 | + String consumer = "c1"; |
| 1190 | + |
| 1191 | + exec(commandObjects.del(key)); |
| 1192 | + exec(commandObjects.xgroupCreate(key, group, StreamEntryID.XGROUP_LAST_ENTRY, true)); |
| 1193 | + |
| 1194 | + // Make 3 entries pending |
| 1195 | + exec(commandObjects.xadd(key, new StreamEntryID("1-0"), java.util.Collections.singletonMap("f", "v"))); |
| 1196 | + exec(commandObjects.xadd(key, new StreamEntryID("2-0"), java.util.Collections.singletonMap("f", "v"))); |
| 1197 | + exec(commandObjects.xadd(key, new StreamEntryID("3-0"), java.util.Collections.singletonMap("f", "v"))); |
| 1198 | + java.util.Map<String, StreamEntryID> stream = java.util.Collections.singletonMap(key, StreamEntryID.XREADGROUP_UNDELIVERED_ENTRY); |
| 1199 | + exec(commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(3), stream)); |
| 1200 | + |
| 1201 | + // Wait then add fresh entries |
| 1202 | + Thread.sleep(60); |
| 1203 | + exec(commandObjects.xadd(key, new StreamEntryID("4-0"), java.util.Collections.singletonMap("f", "v"))); |
| 1204 | + exec(commandObjects.xadd(key, new StreamEntryID("5-0"), java.util.Collections.singletonMap("f", "v"))); |
| 1205 | + |
| 1206 | + // Read with CLAIM and NOACK |
| 1207 | + java.util.List<java.util.Map.Entry<String, java.util.List<StreamEntry>>> messages = exec( |
| 1208 | + commandObjects.xreadGroup(group, consumer, new XReadGroupParams().count(5).claim(50).noAck(), stream)); |
| 1209 | + |
| 1210 | + assertThat(messages, hasSize(1)); |
| 1211 | + java.util.List<StreamEntry> entries = messages.get(0).getValue(); |
| 1212 | + org.junit.jupiter.api.Assertions.assertEquals(5, entries.size()); |
| 1213 | + for (int i = 0; i < 3; i++) { |
| 1214 | + org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getIdleTime()); |
| 1215 | + org.junit.jupiter.api.Assertions.assertNotNull(entries.get(i).getDeliveredTimes()); |
| 1216 | + } |
| 1217 | + for (int i = 3; i < 5; i++) { |
| 1218 | + org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getIdleTime()); |
| 1219 | + org.junit.jupiter.api.Assertions.assertNull(entries.get(i).getDeliveredTimes()); |
| 1220 | + } |
| 1221 | + |
| 1222 | + Long acked = exec(commandObjects.xack(key, group, new StreamEntryID("4-0"), new StreamEntryID("5-0"))); |
| 1223 | + org.junit.jupiter.api.Assertions.assertEquals(0L, acked); |
| 1224 | + } |
| 1225 | + |
1105 | 1226 | } |
0 commit comments