DATAREDIS-782 - Polishing.
Add setIfPresent(…) with expiry to reactive API. Add since tags, minor tweaks to javadoc and tests. Original pull request: #320.
This commit is contained in:
@@ -18,7 +18,6 @@ package org.springframework.data.redis.core;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.Assume.*;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -142,16 +141,18 @@ public class DefaultReactiveValueOperationsIntegrationTests<K, V> {
|
||||
K key = keyFactory.instance();
|
||||
V value = valueFactory.instance();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))).expectNext(true)
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofSeconds(5))).expectNext(true)
|
||||
.expectComplete().verify();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value)).expectNext(false).verifyComplete();
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))).expectNext(false)
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofSeconds(5))).expectNext(false)
|
||||
.verifyComplete();
|
||||
|
||||
Mono<Boolean> mono = valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))
|
||||
.delaySubscription(Duration.ofMillis(500));
|
||||
StepVerifier.create(mono).expectNext(true).verifyComplete();
|
||||
StepVerifier.create(redisTemplate.getExpire(key)) //
|
||||
.assertNext(actual -> {
|
||||
|
||||
assertThat(actual).isBetween(Duration.ofMillis(1), Duration.ofSeconds(5));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-602, DATAREDIS-779
|
||||
@@ -170,6 +171,30 @@ public class DefaultReactiveValueOperationsIntegrationTests<K, V> {
|
||||
StepVerifier.create(valueOperations.get(key)).expectNext(laterValue).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-782
|
||||
public void setIfPresentWithExpiry() {
|
||||
|
||||
K key = keyFactory.instance();
|
||||
V value = valueFactory.instance();
|
||||
V laterValue = valueFactory.instance();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfPresent(key, value, Duration.ofSeconds(5))).expectNext(false)
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.set(key, value, Duration.ofSeconds(5))).expectNext(true).verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfPresent(key, laterValue, Duration.ofSeconds(5))).expectNext(true)
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.get(key)).expectNext(laterValue).verifyComplete();
|
||||
|
||||
StepVerifier.create(redisTemplate.getExpire(key)) //
|
||||
.assertNext(actual -> {
|
||||
|
||||
assertThat(actual).isBetween(Duration.ofMillis(1), Duration.ofSeconds(5));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-602
|
||||
public void multiSet() {
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.springframework.data.redis.SpinBarrier.*;
|
||||
@@ -47,6 +48,7 @@ import org.springframework.data.redis.RedisTestProfileValueSource;
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
* @author Jiahe Cai
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class DefaultValueOperationsTests<K, V> {
|
||||
@@ -237,13 +239,15 @@ public class DefaultValueOperationsTests<K, V> {
|
||||
|
||||
@Test // DATAREDIS-782
|
||||
public void testSetIfAbsentWithExpiration() {
|
||||
final K key1 = keyFactory.instance();
|
||||
K key = keyFactory.instance();
|
||||
V value1 = valueFactory.instance();
|
||||
V value2 = valueFactory.instance();
|
||||
assertTrue(valueOps.setIfAbsent(key1, value1, 500, TimeUnit.MILLISECONDS));
|
||||
assertFalse(valueOps.setIfAbsent(key1, value2));
|
||||
assertFalse(valueOps.setIfAbsent(key1, value2, 500, TimeUnit.MILLISECONDS));
|
||||
waitFor(() -> (valueOps.setIfAbsent(key1, value1, 500, TimeUnit.MILLISECONDS)), 500);
|
||||
assertTrue(valueOps.setIfAbsent(key, value1, 5, TimeUnit.SECONDS));
|
||||
assertFalse(valueOps.setIfAbsent(key, value2));
|
||||
assertFalse(valueOps.setIfAbsent(key, value2, 5, TimeUnit.SECONDS));
|
||||
Long expire = redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
|
||||
assertThat(expire, is(lessThan(TimeUnit.SECONDS.toMillis(6))));
|
||||
assertThat(expire, is(greaterThan(TimeUnit.MILLISECONDS.toMillis(1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user