DATAREDIS-251 - Fix tests to work against Redis 2.8.x.

Since Redis 2.8.x keyOps#getExpire() returns -2 for empty collections (RedisList/RedisSet/RedisMap). 
We're now adding a dummy entry to keep the test version agnostic. Previously getExpire() returned -1.
We also have to set the RedisAtomic(Integer|Long) to a value other than 0 to get  the value stored appropriately.

For reference see the discussion in http://redis.io/commands/ttl
This commit is contained in:
Thomas Darimont
2014-01-15 19:36:22 +01:00
parent 168713aae1
commit 2870df0cdc

View File

@@ -15,6 +15,15 @@
*/
package org.springframework.data.redis.support;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
@@ -26,15 +35,8 @@ import org.springframework.data.redis.ObjectFactory;
import org.springframework.data.redis.connection.ConnectionUtils;
import org.springframework.data.redis.core.BoundKeyOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
/**
* @author Costin Leau
@@ -43,7 +45,7 @@ import static org.junit.Assume.assumeTrue;
*/
@RunWith(Parameterized.class)
public class BoundKeyOperationsTest {
private BoundKeyOperations<Object> keyOps;
private BoundKeyOperations keyOps;
private ObjectFactory<Object> objFactory;
private RedisTemplate template;
@@ -56,8 +58,7 @@ public class BoundKeyOperationsTest {
}
@After
public void stop() {
}
public void stop() {}
@AfterClass
public static void cleanUp() {
@@ -79,12 +80,10 @@ public class BoundKeyOperationsTest {
// at start of test run and underlying key wiped out by other tests
try {
keyOps.getClass().getMethod("set", int.class).invoke(keyOps, 0);
}catch(NoSuchMethodException e) {
}
} catch (NoSuchMethodException e) {}
try {
keyOps.getClass().getMethod("set", long.class).invoke(keyOps, 0l);
}catch(NoSuchMethodException e) {
}
} catch (NoSuchMethodException e) {}
Object newName = objFactory.instance();
keyOps.rename(newName);
assertEquals(newName, keyOps.getKey());
@@ -92,44 +91,51 @@ public class BoundKeyOperationsTest {
assertEquals(key, keyOps.getKey());
}
/**
* @see DATAREDIS-251
*/
/**
* @see DATAREDIS-251
*/
@Test
public void testExpire() throws Exception {
if(keyOps instanceof List || keyOps instanceof Set){
((Collection) keyOps).add("dummy");
}else if(keyOps instanceof Map){
((Map)keyOps).put("dummy","dummy");
}
populateBoundKey();
assertEquals(Long.valueOf(-1), keyOps.getExpire());
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire());
if (keyOps.expire(10, TimeUnit.SECONDS)) {
long expire = keyOps.getExpire().longValue();
assertTrue(expire <= 10 && expire > 5);
}
}
/**
* @see DATAREDIS-251
*/
/**
* @see DATAREDIS-251
*/
@Test
public void testPersist() throws Exception {
assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory()));
if(keyOps instanceof List || keyOps instanceof Set){
((Collection) keyOps).add("dummy");
}else if(keyOps instanceof Map){
((Map)keyOps).put("dummy","dummy");
}
populateBoundKey();
keyOps.persist();
assertEquals(Long.valueOf(-1), keyOps.getExpire());
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), Long.valueOf(-1), keyOps.getExpire());
if (keyOps.expire(10, TimeUnit.SECONDS)) {
assertTrue(keyOps.getExpire().longValue() > 0);
}
keyOps.persist();
assertEquals(-1, keyOps.getExpire().longValue());
assertEquals(keyOps.getClass().getName() + " -> " + keyOps.getKey(), -1, keyOps.getExpire().longValue());
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private void populateBoundKey() {
if (keyOps instanceof List || keyOps instanceof Set) {
((Collection) keyOps).add("dummy");
} else if (keyOps instanceof Map) {
((Map) keyOps).put("dummy", "dummy");
} else if (keyOps instanceof RedisAtomicInteger) {
((RedisAtomicInteger) keyOps).set(42);
} else if (keyOps instanceof RedisAtomicLong) {
((RedisAtomicLong) keyOps).set(42L);
}
}
}