Refactor connection tests with waitFor method

- Support more general test conditions that
require period checking until timeout occurs
This commit is contained in:
Jennifer Hickey
2013-06-12 12:26:53 -07:00
parent cc33219d5a
commit 80ac1af812
3 changed files with 29 additions and 14 deletions

View File

@@ -103,7 +103,7 @@ public abstract class AbstractConnectionIntegrationTests {
public void testExpire() throws Exception {
connection.set("exp", "true");
assertTrue(connection.expire("exp", 1));
assertFalse(exists("exp", 3000l));
assertTrue(waitFor(new KeyExpired("exp"), 3000l));
}
@Test
@@ -111,7 +111,7 @@ public abstract class AbstractConnectionIntegrationTests {
public void testExpireAt() throws Exception {
connection.set("exp2", "true");
assertTrue(connection.expireAt("exp2", System.currentTimeMillis() / 1000 + 1));
assertFalse(exists("exp2", 3000l));
assertTrue(waitFor(new KeyExpired("exp2"), 3000l));
}
@Test
@@ -130,7 +130,7 @@ public abstract class AbstractConnectionIntegrationTests {
public void testSetEx() throws Exception {
connection.setEx("expy", 1l, "yep");
assertEquals("yep", connection.get("expy"));
assertFalse(exists("expy", 3000l));
assertTrue(waitFor(new KeyExpired("expy"), 3000l));
}
@Test
@@ -1212,12 +1212,12 @@ public abstract class AbstractConnectionIntegrationTests {
assertEquals(expected, actual);
}
protected boolean exists(String key, long timeout) {
boolean exists = true;
protected boolean waitFor(TestCondition condition, long timeout) {
boolean passes = false;
for (long currentTime = System.currentTimeMillis(); System.currentTimeMillis()
- currentTime < timeout;) {
if (!connection.exists(key)) {
exists = false;
if (condition.passes()) {
passes = true;
break;
}
try {
@@ -1225,6 +1225,22 @@ public abstract class AbstractConnectionIntegrationTests {
} catch (InterruptedException e) {
}
}
return exists;
return passes;
}
protected interface TestCondition {
public boolean passes();
}
protected class KeyExpired implements TestCondition {
private String key;
public KeyExpired(String key) {
this.key = key;
}
public boolean passes() {
return (!connection.exists(key));
}
}
}

View File

@@ -17,7 +17,6 @@
package org.springframework.data.redis.connection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -122,7 +121,7 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
connection.set("exp", "true");
actual.add(connection.expire("exp", 1));
verifyResults(Arrays.asList(new Object[] { 1l }), actual);
assertFalse(exists("exp", 2500l));
assertTrue(waitFor(new KeyExpired("exp"), 2500l));
}
@Test
@@ -131,7 +130,7 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
connection.set("exp2", "true");
actual.add(connection.expireAt("exp2", System.currentTimeMillis() / 1000 + 1));
verifyResults(Arrays.asList(new Object[] { 1l }), actual);
assertFalse(exists("exp2", 2500l));
assertTrue(waitFor(new KeyExpired("exp2"), 2500l));
}
@Test
@@ -151,7 +150,7 @@ abstract public class AbstractConnectionPipelineIntegrationTests extends
connection.setEx("expy", 1l, "yep");
actual.add(connection.get("expy"));
verifyResults(Arrays.asList(new Object[] { "yep" }), actual);
assertFalse(exists("expy", 2500l));
assertTrue(waitFor(new KeyExpired("expy"), 2500l));
}
@Test

View File

@@ -280,7 +280,7 @@ public class LettuceConnectionPipelineIntegrationTests extends
connection.set("exp2", "true");
actual.add(connection.expireAt("exp2", System.currentTimeMillis() / 1000 + 1));
verifyResults(Arrays.asList(new Object[] { true}), actual);
assertFalse(exists("exp2", 2500l));
assertTrue(waitFor(new KeyExpired("exp2"), 2500l));
}
@Test
@@ -289,7 +289,7 @@ public class LettuceConnectionPipelineIntegrationTests extends
connection.set("exp", "true");
actual.add(connection.expire("exp", 1));
verifyResults(Arrays.asList(new Object[] { true }), actual);
assertFalse(exists("exp", 2500));
assertTrue(waitFor(new KeyExpired("exp"), 2500));
}
@SuppressWarnings({ "rawtypes", "unchecked" })