|
35 | 35 | import org.springframework.data.redis.connection.Limit;
|
36 | 36 | import org.springframework.data.redis.connection.RedisConnection;
|
37 | 37 | import org.springframework.data.redis.connection.RedisConnectionFactory;
|
| 38 | +import org.springframework.data.redis.connection.RedisStreamCommands.XAddOptions; |
38 | 39 | import org.springframework.data.redis.connection.stream.Consumer;
|
39 | 40 | import org.springframework.data.redis.connection.stream.MapRecord;
|
40 | 41 | import org.springframework.data.redis.connection.stream.ReadOffset;
|
|
61 | 62 | * @author Mark Paluch
|
62 | 63 | * @author Christoph Strobl
|
63 | 64 | * @author Marcin Zielinski
|
| 65 | + * @author jinkshower |
64 | 66 | */
|
65 | 67 | @MethodSource("testParams")
|
66 | 68 | @SuppressWarnings("unchecked")
|
@@ -192,6 +194,102 @@ void addShouldAddReadSimpleMessageWithRawSerializer() {
|
192 | 194 | .verifyComplete();
|
193 | 195 | }
|
194 | 196 |
|
| 197 | + @ParameterizedRedisTest // GH-2915 |
| 198 | + void addShouldAddMessageWithOptions() { |
| 199 | + |
| 200 | + K key = keyFactory.instance(); |
| 201 | + HK hashKey = hashKeyFactory.instance(); |
| 202 | + HV value = valueFactory.instance(); |
| 203 | + |
| 204 | + streamOperations.add(key, Collections.singletonMap(hashKey, value)).block(); |
| 205 | + |
| 206 | + HV newValue = valueFactory.instance(); |
| 207 | + XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false); |
| 208 | + |
| 209 | + RecordId messageId = streamOperations.add(key, Collections.singletonMap(hashKey, newValue), options).block(); |
| 210 | + |
| 211 | + streamOperations.range(key, Range.unbounded()) // |
| 212 | + .as(StepVerifier::create) // |
| 213 | + .consumeNextWith(actual -> { |
| 214 | + |
| 215 | + assertThat(actual.getId()).isEqualTo(messageId); |
| 216 | + assertThat(actual.getStream()).isEqualTo(key); |
| 217 | + assertThat(actual).hasSize(1); |
| 218 | + |
| 219 | + if (!(key instanceof byte[] || value instanceof byte[])) { |
| 220 | + assertThat(actual.getValue()).containsEntry(hashKey, newValue); |
| 221 | + } |
| 222 | + |
| 223 | + }) // |
| 224 | + .verifyComplete(); |
| 225 | + } |
| 226 | + |
| 227 | + @ParameterizedRedisTest // GH-2915 |
| 228 | + void addShouldAddReadSimpleMessageWithOptions() { |
| 229 | + |
| 230 | + assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer) |
| 231 | + && !(serializer instanceof GenericJackson2JsonRedisSerializer) |
| 232 | + && !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer)); |
| 233 | + |
| 234 | + K key = keyFactory.instance(); |
| 235 | + HV value = valueFactory.instance(); |
| 236 | + |
| 237 | + streamOperations.add(StreamRecords.objectBacked(value).withStreamKey(key)).block(); |
| 238 | + |
| 239 | + HV newValue = valueFactory.instance(); |
| 240 | + XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false); |
| 241 | + |
| 242 | + RecordId messageId = streamOperations.add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block(); |
| 243 | + |
| 244 | + streamOperations.range((Class<HV>) value.getClass(), key, Range.unbounded()) |
| 245 | + .as(StepVerifier::create) // |
| 246 | + .consumeNextWith(actual -> { |
| 247 | + assertThat(actual.getId()).isEqualTo(messageId); |
| 248 | + assertThat(actual.getStream()).isEqualTo(key); |
| 249 | + |
| 250 | + assertThat(actual.getValue()).isEqualTo(newValue); |
| 251 | + }) // |
| 252 | + .expectNextCount(0) |
| 253 | + .verifyComplete(); |
| 254 | + } |
| 255 | + |
| 256 | + @ParameterizedRedisTest // GH-2915 |
| 257 | + void addShouldAddReadSimpleMessageWithRawSerializerWithOptions() { |
| 258 | + |
| 259 | + assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer) |
| 260 | + && !(serializer instanceof GenericJackson2JsonRedisSerializer)); |
| 261 | + |
| 262 | + SerializationPair<K> keySerializer = redisTemplate.getSerializationContext().getKeySerializationPair(); |
| 263 | + |
| 264 | + RedisSerializationContext<K, String> serializationContext = RedisSerializationContext |
| 265 | + .<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer) |
| 266 | + .hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build(); |
| 267 | + |
| 268 | + ReactiveRedisTemplate<K, String> raw = new ReactiveRedisTemplate<>(redisTemplate.getConnectionFactory(), |
| 269 | + serializationContext); |
| 270 | + |
| 271 | + K key = keyFactory.instance(); |
| 272 | + Person value = new PersonObjectFactory().instance(); |
| 273 | + |
| 274 | + raw.opsForStream().add(StreamRecords.objectBacked(value).withStreamKey(key)).block(); |
| 275 | + |
| 276 | + Person newValue = new PersonObjectFactory().instance(); |
| 277 | + XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false); |
| 278 | + |
| 279 | + RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block(); |
| 280 | + |
| 281 | + raw.opsForStream().range((Class<HV>) value.getClass(), key, Range.unbounded()) |
| 282 | + .as(StepVerifier::create) // |
| 283 | + .consumeNextWith(it -> { |
| 284 | + |
| 285 | + assertThat(it.getId()).isEqualTo(messageId); |
| 286 | + assertThat(it.getStream()).isEqualTo(key); |
| 287 | + assertThat(it.getValue()).isEqualTo(newValue); |
| 288 | + }) // |
| 289 | + .expectNextCount(0) |
| 290 | + .verifyComplete(); |
| 291 | + } |
| 292 | + |
195 | 293 | @ParameterizedRedisTest // DATAREDIS-864
|
196 | 294 | void rangeShouldReportMessages() {
|
197 | 295 |
|
|
0 commit comments