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 f425925dd5
commit 1d6ee86fb1
2 changed files with 59 additions and 22 deletions

View File

@@ -16,15 +16,15 @@
package org.springframework.data.redis.connection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.test.annotation.IfProfileValue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.test.annotation.IfProfileValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Base test class for integration tests that execute each operation of a
@@ -151,6 +151,14 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
}
protected List<Object> getResults() {
return connection.closePipeline();
try {
//we give redis some time to keep up
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return connection.closePipeline();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 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.
@@ -15,14 +15,16 @@
*/
package org.springframework.data.redis.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
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;
@@ -34,14 +36,17 @@ 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 org.springframework.data.redis.support.atomic.RedisAtomicInteger;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
/**
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
*/
@RunWith(Parameterized.class)
public class BoundKeyOperationsTest {
private BoundKeyOperations<Object> keyOps;
private BoundKeyOperations keyOps;
private ObjectFactory<Object> objFactory;
private RedisTemplate template;
@@ -54,8 +59,7 @@ public class BoundKeyOperationsTest {
}
@After
public void stop() {
}
public void stop() {}
@AfterClass
public static void cleanUp() {
@@ -77,12 +81,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());
@@ -90,24 +92,51 @@ public class BoundKeyOperationsTest {
assertEquals(key, keyOps.getKey());
}
/**
* @see DATAREDIS-251
*/
@Test
public void testExpire() throws Exception {
assertEquals(Long.valueOf(-1), keyOps.getExpire());
populateBoundKey();
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
*/
@Test
public void testPersist() throws Exception {
assumeTrue(!ConnectionUtils.isJredis(template.getConnectionFactory()));
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);
}
}
}