From 1d6ee86fb18bdbe9899d1f7425e8463126fbd629 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Wed, 15 Jan 2014 19:36:22 +0100 Subject: [PATCH] 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 --- ...actConnectionPipelineIntegrationTests.java | 20 ++++-- .../redis/support/BoundKeyOperationsTest.java | 61 ++++++++++++++----- 2 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java index 40acab2ee..e5d8d7d38 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionPipelineIntegrationTests.java @@ -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 getResults() { - return connection.closePipeline(); + + try { + //we give redis some time to keep up + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + return connection.closePipeline(); } } diff --git a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java index 29075e3fe..3651ba8cb 100644 --- a/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java +++ b/src/test/java/org/springframework/data/redis/support/BoundKeyOperationsTest.java @@ -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 keyOps; + private BoundKeyOperations keyOps; private ObjectFactory 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()); } -} \ No newline at end of file + + @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); + } + } +}