3
3
import org .junit .Before ;
4
4
import org .junit .Rule ;
5
5
import org .junit .Test ;
6
+ import org .rnorth .ducttape .unreliables .Unreliables ;
6
7
import org .testcontainers .containers .GenericContainer ;
7
8
import redis .clients .jedis .Jedis ;
8
9
9
10
import java .util .Optional ;
11
+ import java .util .concurrent .TimeUnit ;
10
12
11
13
import static org .rnorth .visibleassertions .VisibleAssertions .*;
12
14
@@ -17,7 +19,8 @@ public class RedisJacksonBackedCacheTest {
17
19
18
20
@ Rule
19
21
public GenericContainer redis = new GenericContainer ("redis:3.0.6" )
20
- .withExposedPorts (6379 );
22
+ .withExposedPorts (6379 )
23
+ .waitingFor (new RedisWaitStrategy ());
21
24
private Cache cache ;
22
25
23
26
@ Before
@@ -33,10 +36,10 @@ public void testFindingAnInsertedValue() {
33
36
Optional <String > foundObject = cache .get ("foo" , String .class );
34
37
35
38
assertTrue ("When an object in the cache is retrieved, it can be found" ,
36
- foundObject .isPresent ());
39
+ foundObject .isPresent ());
37
40
assertEquals ("When we put a String in to the cache and retrieve it, the value is the same" ,
38
- "FOO" ,
39
- foundObject .get ());
41
+ "FOO" ,
42
+ foundObject .get ());
40
43
}
41
44
42
45
@ Test
@@ -46,4 +49,16 @@ public void testNotFindingAValueThatWasNotInserted() {
46
49
assertFalse ("When an object that's not in the cache is retrieved, nothing is found" ,
47
50
foundObject .isPresent ());
48
51
}
52
+
53
+ private class RedisWaitStrategy extends GenericContainer .AbstractWaitStrategy {
54
+ @ Override
55
+ protected void waitUntilReady () {
56
+ //noinspection LoopStatementThatDoesntLoop
57
+ Unreliables .retryUntilSuccess (10 , TimeUnit .SECONDS , () ->
58
+ {
59
+ Jedis jedis = new Jedis (redis .getContainerIpAddress (), redis .getMappedPort (6379 ));
60
+ return jedis .ping ();
61
+ });
62
+ }
63
+ }
49
64
}
0 commit comments