DATAREDIS-611 - Add overloads accepting Duration and Instant.

We revised our imperative Template API by accepting Duration and Instant types in addition to methods accepting timeout/TimeUnit respective Date.

Original Pull Request: #501
This commit is contained in:
Mark Paluch
2019-11-19 10:17:07 +01:00
committed by Christoph Strobl
parent fd85fb4eb5
commit 31d4730222
7 changed files with 259 additions and 24 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.redis.core;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -34,6 +35,7 @@ import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.redis.ConnectionFactoryTracker;
import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.data.redis.StringObjectFactory;
/**
* Integration test of {@link DefaultListOperations}
@@ -129,10 +131,41 @@ public class DefaultListOperationsTests<K, V> {
assertThat(listOps.range(key, 0, -1)).contains(v3, v2, v1);
}
@Test
public void testLeftPopDuration() {
// 1 ms timeout gets upgraded to 1 sec timeout at the moment
assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true"));
assumeTrue(valueFactory instanceof StringObjectFactory);
K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
assertThat(listOps.leftPop(key, Duration.ofSeconds(1))).isNull();
listOps.rightPushAll(key, v1, v2);
assertThat(listOps.leftPop(key, Duration.ofSeconds(1))).isEqualTo(v1);
}
@Test
public void testRightPopDuration() {
// 1 ms timeout gets upgraded to 1 sec timeout at the moment
assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true"));
assumeTrue(valueFactory instanceof StringObjectFactory);
K key = keyFactory.instance();
V v1 = valueFactory.instance();
V v2 = valueFactory.instance();
assertThat(listOps.rightPop(key, Duration.ofSeconds(1))).isNull();
listOps.rightPushAll(key, v1, v2);
assertThat(listOps.rightPop(key, Duration.ofSeconds(1))).isEqualTo(v2);
}
@Test
public void testRightPopAndLeftPushTimeout() {
// 1 ms timeout gets upgraded to 1 sec timeout at the moment
assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true"));
assumeTrue(valueFactory instanceof StringObjectFactory);
K key = keyFactory.instance();
K key2 = keyFactory.instance();
@@ -143,6 +176,21 @@ public class DefaultListOperationsTests<K, V> {
assertThat(listOps.rightPopAndLeftPush(key, key2, 1, TimeUnit.MILLISECONDS)).isEqualTo(v1);
}
@Test // DATAREDIS-611
public void testRightPopAndLeftPushDuration() {
// 1 ms timeout gets upgraded to 1 sec timeout at the moment
assumeTrue(RedisTestProfileValueSource.matches("runLongTests", "true"));
assumeTrue(valueFactory instanceof StringObjectFactory);
K key = keyFactory.instance();
K key2 = keyFactory.instance();
V v1 = valueFactory.instance();
assertThat(listOps.rightPopAndLeftPush(key, key2, Duration.ofMillis(1))).isNull();
listOps.leftPush(key, v1);
assertThat(listOps.rightPopAndLeftPush(key, key2, Duration.ofMillis(1))).isEqualTo(v1);
}
@Test
public void testRightPopAndLeftPush() {

View File

@@ -20,6 +20,9 @@ import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.Assume.*;
import static org.springframework.data.redis.SpinBarrier.*;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -526,9 +529,6 @@ public class RedisTemplateTests<K, V> {
@Test // DATAREDIS-526
public void testGetExpireMillis() {
assumeTrue(redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory
|| redisTemplate.getConnectionFactory() instanceof LettuceConnectionFactory);
K key = keyFactory.instance();
redisTemplate.boundValueOps(key).set(valueFactory.instance());
redisTemplate.expire(key, 1, TimeUnit.DAYS);
@@ -539,6 +539,19 @@ public class RedisTemplateTests<K, V> {
assertThat(ttl).isLessThan(25L);
}
@Test // DATAREDIS-611
public void testGetExpireDuration() {
K key = keyFactory.instance();
redisTemplate.boundValueOps(key).set(valueFactory.instance());
redisTemplate.expire(key, Duration.ofDays(1));
Long ttl = redisTemplate.getExpire(key, TimeUnit.HOURS);
assertThat(ttl).isGreaterThanOrEqualTo(23L);
assertThat(ttl).isLessThan(25L);
}
@Test // DATAREDIS-526
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testGetExpireMillisUsingTransactions() {
@@ -592,24 +605,6 @@ public class RedisTemplateTests<K, V> {
assertThat(((Long) result.get(2))).isLessThan(25L);
}
@Test
public void testGetExpireMillisNotSupported() {
assumeTrue(redisTemplate.getConnectionFactory() instanceof JedisConnectionFactory);
K key1 = keyFactory.instance();
V value1 = valueFactory.instance();
assumeTrue(key1 instanceof String && value1 instanceof String);
StringRedisTemplate template2 = new StringRedisTemplate(redisTemplate.getConnectionFactory());
template2.boundValueOps((String) key1).set((String) value1);
template2.expire((String) key1, 5, TimeUnit.SECONDS);
long expire = template2.getExpire((String) key1, TimeUnit.MILLISECONDS);
// we should still get expire in milliseconds if requested
assertThat(expire > 1000 && expire <= 5000).isTrue();
}
@Test
public void testExpireAt() {
K key1 = keyFactory.instance();
@@ -619,6 +614,15 @@ public class RedisTemplateTests<K, V> {
waitFor(() -> (!redisTemplate.hasKey(key1)), 5L);
}
@Test // DATAREDIS-611
public void testExpireAtInstant() {
K key1 = keyFactory.instance();
V value1 = valueFactory.instance();
redisTemplate.boundValueOps(key1).set(value1);
redisTemplate.expireAt(key1, Instant.now().plus(5, ChronoUnit.MILLIS));
waitFor(() -> (!redisTemplate.hasKey(key1)), 5L);
}
@Test
public void testExpireAtMillisNotSupported() {