Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Refine assertions usage.

Original pull request: #2985
Closes #2982
  • Loading branch information
mp911de committed Sep 10, 2024
1 parent b7f26fa commit 6eac1d1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection;

import static org.assertj.core.api.Assertions.*;
Expand All @@ -24,7 +39,7 @@ void pendingRecordsCommandRangeShouldThrowExceptionWhenRangeIsNull() {

PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);

assertThatThrownBy(() -> command.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> command.range(null, 10L));
}

@Test // GH-2982
Expand All @@ -36,6 +51,6 @@ void pendingRecordsCommandRangeShouldThrowExceptionWhenCountIsNegative() {
PendingRecordsCommand command = PendingRecordsCommand.pending(key, groupName);
Range<?> range = Range.closed("0", "10");

assertThatThrownBy(() -> command.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> command.range(range, -1L));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection;

import static org.assertj.core.api.Assertions.*;
Expand All @@ -16,21 +31,19 @@ class RedisStreamCommandsUnitTests {

@Test // GH-2982
void xPendingOptionsUnboundedShouldThrowExceptionWhenCountIsNegative() {

assertThatThrownBy(() -> XPendingOptions.unbounded(-1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.unbounded(-1L));
}

@Test // GH-2982
void xPendingOptionsRangeShouldThrowExceptionWhenRangeIsNull() {

assertThatThrownBy(() -> XPendingOptions.range(null, 10L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(null, 10L));
}

@Test // GH-2982
void xPendingOptionsRangeShouldThrowExceptionWhenCountIsNegative() {

Range<?> range = Range.closed("0", "10");

assertThatThrownBy(() -> XPendingOptions.range(range, -1L)).isInstanceOf(IllegalArgumentException.class);
assertThatIllegalArgumentException().isThrownBy(() -> XPendingOptions.range(range, -1L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ public class DefaultReactiveStreamOperationsIntegrationTests<K, HK, HV> {
return ReactiveOperationsTestParams.testParams();
}

/**
* @param redisTemplate
* @param keyFactory
* @param valueFactory
* @param label parameterized test label, no further use besides that.
*/
public DefaultReactiveStreamOperationsIntegrationTests(Fixture<K, HV> fixture) {

this.serializer = fixture.getSerializer();
Expand Down Expand Up @@ -208,27 +202,25 @@ void addMaxLenShouldLimitMessagesSize() {

RecordId messageId = streamOperations.add(key, Collections.singletonMap(hashKey, newValue), options).block();

streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {

assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual).hasSize(1);
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual).hasSize(1);

if (!(key instanceof byte[] || value instanceof byte[])) {
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
}
if (!(key instanceof byte[] || value instanceof byte[])) {
assertThat(actual.getValue()).containsEntry(hashKey, newValue);
}

})
.verifyComplete();
}).verifyComplete();
}

@ParameterizedRedisTest // GH-2915
void addMaxLenShouldLimitSimpleMessagesSize() {

assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));
&& !(serializer instanceof GenericJackson2JsonRedisSerializer)
&& !(serializer instanceof JdkSerializationRedisSerializer) && !(serializer instanceof OxmSerializer));

K key = keyFactory.instance();
HV value = valueFactory.instance();
Expand All @@ -241,31 +233,29 @@ void addMaxLenShouldLimitSimpleMessagesSize() {
RecordId messageId = streamOperations.add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();

streamOperations.range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
.consumeNextWith(actual -> {

assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual.getValue()).isEqualTo(newValue);
assertThat(actual.getId()).isEqualTo(messageId);
assertThat(actual.getStream()).isEqualTo(key);
assertThat(actual.getValue()).isEqualTo(newValue);

})
.expectNextCount(0)
.verifyComplete();
}).expectNextCount(0).verifyComplete();
}

@ParameterizedRedisTest // GH-2915
void addMaxLenShouldLimitSimpleMessageWithRawSerializerSize() {

assumeTrue(!(serializer instanceof Jackson2JsonRedisSerializer)
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));
&& !(serializer instanceof GenericJackson2JsonRedisSerializer));

SerializationPair<K> keySerializer = redisTemplate.getSerializationContext().getKeySerializationPair();

RedisSerializationContext<K, String> serializationContext = RedisSerializationContext
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();
.<K, String> newSerializationContext(StringRedisSerializer.UTF_8).key(keySerializer)
.hashValue(SerializationPair.raw()).hashKey(SerializationPair.raw()).build();

ReactiveRedisTemplate<K, String> raw = new ReactiveRedisTemplate<>(redisTemplate.getConnectionFactory(),
serializationContext);
serializationContext);

K key = keyFactory.instance();
Person value = new PersonObjectFactory().instance();
Expand All @@ -275,18 +265,17 @@ void addMaxLenShouldLimitSimpleMessageWithRawSerializerSize() {
Person newValue = new PersonObjectFactory().instance();
XAddOptions options = XAddOptions.maxlen(1).approximateTrimming(false);

RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options).block();
RecordId messageId = raw.opsForStream().add(StreamRecords.objectBacked(newValue).withStreamKey(key), options)
.block();

raw.opsForStream().range((Class<HV>) value.getClass(), key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(it -> {
.consumeNextWith(it -> {

assertThat(it.getId()).isEqualTo(messageId);
assertThat(it.getStream()).isEqualTo(key);
assertThat(it.getValue()).isEqualTo(newValue);
assertThat(it.getId()).isEqualTo(messageId);
assertThat(it.getStream()).isEqualTo(key);
assertThat(it.getValue()).isEqualTo(newValue);

})
.expectNextCount(0)
.verifyComplete();
}).expectNextCount(0).verifyComplete();
}

@ParameterizedRedisTest // GH-2915
Expand All @@ -303,17 +292,13 @@ void addMinIdShouldEvictLowerIdMessages() {

RecordId messageId2 = streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();

streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId1);
assertThat(actual.getStream()).isEqualTo(key);
})
.consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId2);
assertThat(actual.getStream()).isEqualTo(key);
})
.expectNextCount(0)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId1);
assertThat(actual.getStream()).isEqualTo(key);
}).consumeNextWith(actual -> {
assertThat(actual.getId()).isEqualTo(messageId2);
assertThat(actual.getStream()).isEqualTo(key);
}).expectNextCount(0).verifyComplete();
}

@ParameterizedRedisTest // GH-2915
Expand All @@ -327,13 +312,9 @@ void addMakeNoStreamShouldNotCreateStreamWhenNoStreamExists() {

streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();

streamOperations.size(key).as(StepVerifier::create)
.expectNext(0L)
.verifyComplete();
streamOperations.size(key).as(StepVerifier::create).expectNext(0L).verifyComplete();

streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.expectNextCount(0L)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(0L).verifyComplete();
}

@ParameterizedRedisTest // GH-2915
Expand All @@ -349,13 +330,9 @@ void addMakeNoStreamShouldCreateStreamWhenStreamExists() {

streamOperations.add(key, Collections.singletonMap(hashKey, value), options).block();

streamOperations.size(key).as(StepVerifier::create)
.expectNext(2L)
.verifyComplete();
streamOperations.size(key).as(StepVerifier::create).expectNext(2L).verifyComplete();

streamOperations.range(key, Range.unbounded()).as(StepVerifier::create)
.expectNextCount(2L)
.verifyComplete();
streamOperations.range(key, Range.unbounded()).as(StepVerifier::create).expectNextCount(2L).verifyComplete();
}

@ParameterizedRedisTest // DATAREDIS-864
Expand Down Expand Up @@ -525,7 +502,6 @@ void pendingShouldReadMessageDetails() {
assertThat(pending.get(0).getConsumerName()).isEqualTo("my-consumer");
assertThat(pending.get(0).getTotalDeliveryCount()).isOne();
}).verifyComplete();

}

@ParameterizedRedisTest // GH-2465
Expand All @@ -550,6 +526,5 @@ void claimShouldReadMessageDetails() {
assertThat(claimed.getValue()).isEqualTo(content);
assertThat(claimed.getId()).isEqualTo(messageId);
}).verifyComplete();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@
import org.springframework.data.redis.connection.jedis.extension.JedisConnectionFactoryExtension;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.extension.LettuceConnectionFactoryExtension;
import org.springframework.data.redis.connection.stream.*;
import org.springframework.data.redis.connection.stream.Consumer;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.PendingMessages;
import org.springframework.data.redis.connection.stream.PendingMessagesSummary;
import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.connection.stream.StreamReadOptions;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.test.condition.EnabledOnCommand;
import org.springframework.data.redis.test.condition.EnabledOnRedisDriver;
import org.springframework.data.redis.test.condition.EnabledOnRedisVersion;
Expand Down Expand Up @@ -67,7 +76,7 @@ public class DefaultStreamOperationsIntegrationTests<K, HK, HV> {
private final StreamOperations<K, HK, HV> streamOps;

public DefaultStreamOperationsIntegrationTests(RedisTemplate<K, ?> redisTemplate, ObjectFactory<K> keyFactory,
ObjectFactory<?> objectFactory) {
ObjectFactory<?> objectFactory) {

this.redisTemplate = redisTemplate;
this.connectionFactory = redisTemplate.getRequiredConnectionFactory();
Expand All @@ -83,15 +92,15 @@ public static Collection<Object[]> testParams() {
params.addAll(AbstractOperationsTestParams
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));

if(RedisDetector.isClusterAvailable()) {
if (RedisDetector.isClusterAvailable()) {
params.addAll(AbstractOperationsTestParams
.testParams(JedisConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
}

params.addAll(AbstractOperationsTestParams
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisStanalone.class)));

if(RedisDetector.isClusterAvailable()) {
if (RedisDetector.isClusterAvailable()) {
params.addAll(AbstractOperationsTestParams
.testParams(LettuceConnectionFactoryExtension.getConnectionFactory(RedisCluster.class)));
}
Expand Down Expand Up @@ -456,7 +465,8 @@ void readShouldReadSimpleMessage() {
RecordId messageId1 = streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));
streamOps.add(StreamRecords.objectBacked(value).withStreamKey(key));

List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(), StreamOffset.create(key, ReadOffset.from("0-0")));
List<ObjectRecord<K, HV>> messages = streamOps.read((Class<HV>) value.getClass(),
StreamOffset.create(key, ReadOffset.from("0-0")));

assertThat(messages).hasSize(2);

Expand Down Expand Up @@ -535,8 +545,7 @@ void pendingShouldReadMessageSummary() {
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");

streamOps.read(Consumer.from("my-group", "my-consumer"),
StreamOffset.create(key, ReadOffset.lastConsumed()));
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));

PendingMessagesSummary pending = streamOps.pending(key, "my-group");

Expand All @@ -554,8 +563,7 @@ void pendingShouldReadMessageDetails() {
RecordId messageId = streamOps.add(key, Collections.singletonMap(hashKey, value));
streamOps.createGroup(key, ReadOffset.from("0-0"), "my-group");

streamOps.read(Consumer.from("my-group", "my-consumer"),
StreamOffset.create(key, ReadOffset.lastConsumed()));
streamOps.read(Consumer.from("my-group", "my-consumer"), StreamOffset.create(key, ReadOffset.lastConsumed()));

PendingMessages pending = streamOps.pending(key, "my-group", Range.unbounded(), 10L);

Expand Down

0 comments on commit 6eac1d1

Please sign in to comment.