1
1
package redis .clients .jedis ;
2
2
3
+ import static org .hamcrest .MatcherAssert .assertThat ;
3
4
import static org .junit .Assert .assertEquals ;
4
5
import static org .junit .Assert .assertNull ;
5
6
7
+ import org .hamcrest .Matchers ;
6
8
import org .junit .After ;
7
9
import org .junit .Before ;
8
10
import org .junit .Test ;
@@ -17,7 +19,7 @@ public class JedisClientSideCacheTest {
17
19
18
20
@ Before
19
21
public void setUp () throws Exception {
20
- jedis = new Jedis (hnp , DefaultJedisClientConfig .builder ().timeoutMillis ( 500 ). password ("foobared" ).build ());
22
+ jedis = new Jedis (hnp , DefaultJedisClientConfig .builder ().password ("foobared" ).build ());
21
23
jedis .flushAll ();
22
24
}
23
25
@@ -26,45 +28,83 @@ public void tearDown() throws Exception {
26
28
jedis .close ();
27
29
}
28
30
29
- private static final JedisClientConfig configForCache = DefaultJedisClientConfig .builder ()
30
- .resp3 ().socketTimeoutMillis (20 ).password ("foobared" ).build ();
31
+ private static final JedisClientConfig clientConfig = DefaultJedisClientConfig .builder ().resp3 ().password ("foobared" ).build ();
31
32
32
33
@ Test
33
34
public void simple () {
34
- try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , configForCache )) {
35
+ try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , clientConfig )) {
35
36
jedis .set ("foo" , "bar" );
36
37
assertEquals ("bar" , jCache .get ("foo" ));
37
38
jedis .del ("foo" );
38
- assertNull (jCache .get ("foo" ));
39
+ assertThat (jCache .get ("foo" ), Matchers . oneOf ( "bar" , null )); // ?
39
40
}
40
41
}
41
42
42
43
@ Test
43
- public void simpleMock () {
44
+ public void simpleMoreAndMock () {
44
45
ClientSideCache cache = Mockito .mock (ClientSideCache .class );
45
- try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , configForCache , cache )) {
46
+ Mockito .when (cache .getValue ("foo" )).thenReturn (null , "bar" , null );
47
+
48
+ try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , clientConfig , cache )) {
46
49
jedis .set ("foo" , "bar" );
50
+
47
51
assertEquals ("bar" , jCache .get ("foo" ));
52
+
48
53
jedis .del ("foo" );
54
+
55
+ assertEquals ("bar" , jCache .get ("foo" ));
56
+
57
+ // there should be an invalid pending; any connection command will make it read
58
+ jCache .ping ();
59
+
49
60
assertNull (jCache .get ("foo" ));
50
61
}
51
62
52
63
InOrder inOrder = Mockito .inOrder (cache );
53
- inOrder .verify (cache ).invalidateKeys (Mockito .notNull ());
54
64
inOrder .verify (cache ).getValue ("foo" );
55
65
inOrder .verify (cache ).setKey ("foo" , "bar" );
66
+ inOrder .verify (cache ).getValue ("foo" );
56
67
inOrder .verify (cache ).invalidateKeys (Mockito .notNull ());
57
68
inOrder .verify (cache ).getValue ("foo" );
58
69
inOrder .verifyNoMoreInteractions ();
59
70
}
60
71
61
72
@ Test
62
- public void flushall () {
63
- try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , configForCache )) {
73
+ public void flushAll () {
74
+ try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , clientConfig )) {
75
+ jedis .set ("foo" , "bar" );
76
+ assertEquals ("bar" , jCache .get ("foo" ));
77
+ jedis .flushAll ();
78
+ assertThat (jCache .get ("foo" ), Matchers .oneOf ("bar" , null )); // ?
79
+ }
80
+ }
81
+
82
+ @ Test
83
+ public void flushAllMoreAndMock () {
84
+ ClientSideCache cache = Mockito .mock (ClientSideCache .class );
85
+ Mockito .when (cache .getValue ("foo" )).thenReturn (null , "bar" , null );
86
+
87
+ try (JedisClientSideCache jCache = new JedisClientSideCache (hnp , clientConfig , cache )) {
64
88
jedis .set ("foo" , "bar" );
89
+
65
90
assertEquals ("bar" , jCache .get ("foo" ));
91
+
66
92
jedis .flushAll ();
93
+
94
+ assertEquals ("bar" , jCache .get ("foo" ));
95
+
96
+ // there should be an invalid pending; any connection command will make it read
97
+ jCache .ping ();
98
+
67
99
assertNull (jCache .get ("foo" ));
68
100
}
101
+
102
+ InOrder inOrder = Mockito .inOrder (cache );
103
+ inOrder .verify (cache ).getValue ("foo" );
104
+ inOrder .verify (cache ).setKey ("foo" , "bar" );
105
+ inOrder .verify (cache ).getValue ("foo" );
106
+ inOrder .verify (cache ).invalidateKeys (Mockito .isNull ());
107
+ inOrder .verify (cache ).getValue ("foo" );
108
+ inOrder .verifyNoMoreInteractions ();
69
109
}
70
110
}
0 commit comments