16
16
17
17
package org .springframework .data .couchbase .cache ;
18
18
19
- import static com .couchbase .client .java .query .QueryScanConsistency .REQUEST_PLUS ;
20
- import static org .junit .Assert .assertNotNull ;
21
19
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
20
import static org .junit .jupiter .api .Assertions .assertNull ;
23
21
@@ -57,7 +55,7 @@ public void beforeEach() {
57
55
super .beforeEach ();
58
56
cache = CouchbaseCacheManager .create (couchbaseTemplate .getCouchbaseClientFactory ()).createCouchbaseCache ("myCache" ,
59
57
CouchbaseCacheConfiguration .defaultCacheConfig ());
60
- clear (cache );
58
+ cache . clear ();
61
59
ApplicationContext ac = new AnnotationConfigApplicationContext (Config .class );
62
60
cacheManager = ac .getBean (CouchbaseCacheManager .class );
63
61
userRepository = ac .getBean (UserRepository .class );
@@ -66,17 +64,11 @@ public void beforeEach() {
66
64
@ AfterEach
67
65
@ Override
68
66
public void afterEach () {
69
- clear (cache );
67
+ cache . clear ();
70
68
super .afterEach ();
71
69
}
72
70
73
- private void clear (CouchbaseCache c ) {
74
- couchbaseTemplate .getCouchbaseClientFactory ().getCluster ().query ("SELECT count(*) from `" + bucketName () + "`" ,
75
- QueryOptions .queryOptions ().scanConsistency (REQUEST_PLUS ));
76
- c .clear ();
77
- couchbaseTemplate .getCouchbaseClientFactory ().getCluster ().query ("SELECT count(*) from `" + bucketName () + "`" ,
78
- QueryOptions .queryOptions ().scanConsistency (REQUEST_PLUS ));
79
- }
71
+
80
72
81
73
@ Test
82
74
void cachePutGet () {
@@ -109,17 +101,19 @@ void cacheEvict() {
109
101
cache .put (user1 .getId (), user1 ); // put user1
110
102
cache .put (user2 .getId (), user2 ); // put user2
111
103
cache .evict (user1 .getId ()); // evict user1
104
+ assertNull (cache .get (user1 .getId ())); // get user1 -> not present
112
105
assertEquals (user2 , cache .get (user2 .getId ()).get ()); // get user2 -> present
113
106
}
114
107
115
108
@ Test
116
- void cacheHitMiss () {
109
+ void cacheClear () {
117
110
CacheUser user1 = new CacheUser (UUID .randomUUID ().toString (), "first1" , "last1" );
118
111
CacheUser user2 = new CacheUser (UUID .randomUUID ().toString (), "first2" , "last2" );
119
- assertNull (cache .get (user2 .getId ())); // get user2 -> cacheMiss
120
- cache .put (user1 .getId (), null ); // cache a null
121
- assertNotNull (cache .get (user1 .getId ())); // cacheHit null
122
- assertNull (cache .get (user1 .getId ()).get ()); // fetch cached null
112
+ cache .put (user1 .getId (), user1 ); // put user1
113
+ cache .put (user2 .getId (), user2 ); // put user2
114
+ cache .clear ();
115
+ assertNull (cache .get (user1 .getId ())); // get user1 -> not present
116
+ assertNull (cache .get (user2 .getId ())); // get user2 -> not present
123
117
}
124
118
125
119
@ Test
@@ -131,12 +125,6 @@ void cachePutIfAbsent() {
131
125
assertEquals (user1 , cache .get (user1 .getId ()).get ()); // user1.getId() is still user1
132
126
}
133
127
134
- @ Test // this test FAILS (local empty (i.e. fast) Couchbase installation)
135
- public void clearFail () {
136
- cache .put ("KEY" , "VALUE" ); // no delay between put and clear, entry will not be
137
- cache .clear (); // will not be indexed when clear() executes
138
- assertNotNull (cache .get ("KEY" )); // will still find entry, clear failed to delete
139
- }
140
128
141
129
@ Test // this WORKS
142
130
public void clearWithDelayOk () throws InterruptedException {
0 commit comments