Skip to content

Commit

Permalink
Add custom Redis wait strategy as an example and to mitigate #160
Browse files Browse the repository at this point in the history
  • Loading branch information
rnorth committed Jul 3, 2016
1 parent 9c57f9f commit 64d3451
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;

import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static org.rnorth.visibleassertions.VisibleAssertions.*;

Expand All @@ -17,7 +19,8 @@ public class RedisJacksonBackedCacheTest {

@Rule
public GenericContainer redis = new GenericContainer("redis:3.0.6")
.withExposedPorts(6379);
.withExposedPorts(6379)
.waitingFor(new RedisWaitStrategy());
private Cache cache;

@Before
Expand All @@ -33,10 +36,10 @@ public void testFindingAnInsertedValue() {
Optional<String> foundObject = cache.get("foo", String.class);

assertTrue("When an object in the cache is retrieved, it can be found",
foundObject.isPresent());
foundObject.isPresent());
assertEquals("When we put a String in to the cache and retrieve it, the value is the same",
"FOO",
foundObject.get());
"FOO",
foundObject.get());
}

@Test
Expand All @@ -46,4 +49,16 @@ public void testNotFindingAValueThatWasNotInserted() {
assertFalse("When an object that's not in the cache is retrieved, nothing is found",
foundObject.isPresent());
}

private class RedisWaitStrategy extends GenericContainer.AbstractWaitStrategy {
@Override
protected void waitUntilReady() {
//noinspection LoopStatementThatDoesntLoop
Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () ->
{
Jedis jedis = new Jedis(redis.getContainerIpAddress(), redis.getMappedPort(6379));
return jedis.ping();
});
}
}
}

0 comments on commit 64d3451

Please sign in to comment.