21
21
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
22
import static org .junit .jupiter .api .Assertions .assertNull ;
23
23
24
+ import java .util .List ;
24
25
import java .util .UUID ;
25
26
27
+ import org .junit .jupiter .api .AfterEach ;
26
28
import org .junit .jupiter .api .BeforeEach ;
27
29
import org .junit .jupiter .api .Test ;
30
+ import org .springframework .beans .factory .annotation .Autowired ;
31
+ import org .springframework .context .ApplicationContext ;
32
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
33
+ import org .springframework .data .couchbase .domain .Config ;
34
+ import org .springframework .data .couchbase .domain .User ;
35
+ import org .springframework .data .couchbase .domain .UserRepository ;
28
36
import org .springframework .data .couchbase .util .ClusterType ;
29
37
import org .springframework .data .couchbase .util .IgnoreWhen ;
30
38
import org .springframework .data .couchbase .util .JavaIntegrationTests ;
40
48
class CouchbaseCacheIntegrationTests extends JavaIntegrationTests {
41
49
42
50
volatile CouchbaseCache cache ;
51
+ @ Autowired CouchbaseCacheManager cacheManager ; // autowired not working
52
+ @ Autowired UserRepository userRepository ; // autowired not working
43
53
44
54
@ BeforeEach
45
55
@ Override
@@ -48,6 +58,16 @@ public void beforeEach() {
48
58
cache = CouchbaseCacheManager .create (couchbaseTemplate .getCouchbaseClientFactory ()).createCouchbaseCache ("myCache" ,
49
59
CouchbaseCacheConfiguration .defaultCacheConfig ());
50
60
clear (cache );
61
+ ApplicationContext ac = new AnnotationConfigApplicationContext (Config .class );
62
+ cacheManager = ac .getBean (CouchbaseCacheManager .class );
63
+ userRepository = ac .getBean (UserRepository .class );
64
+ }
65
+
66
+ @ AfterEach
67
+ @ Override
68
+ public void afterEach () {
69
+ clear (cache );
70
+ super .afterEach ();
51
71
}
52
72
53
73
private void clear (CouchbaseCache c ) {
@@ -69,6 +89,19 @@ void cachePutGet() {
69
89
assertEquals (user2 , cache .get (user2 .getId ()).get ()); // get user2
70
90
}
71
91
92
+ @ Test
93
+ void cacheable () {
94
+ User user = new User ("cache_92" , "Dave" , "Wilson" );
95
+ cacheManager .getCache ("mySpringCache" ).clear ();
96
+ userRepository .save (user );
97
+ long t0 = System .currentTimeMillis ();
98
+ List <User > users = userRepository .getByFirstname (user .getFirstname ());
99
+ assert (System .currentTimeMillis () - t0 > 1000 * 5 );
100
+ t0 = System .currentTimeMillis ();
101
+ users = userRepository .getByFirstname (user .getFirstname ());
102
+ assert (System .currentTimeMillis () - t0 < 100 );
103
+ }
104
+
72
105
@ Test
73
106
void cacheEvict () {
74
107
CacheUser user1 = new CacheUser (UUID .randomUUID ().toString (), "first1" , "last1" );
@@ -98,4 +131,22 @@ void cachePutIfAbsent() {
98
131
assertEquals (user1 , cache .get (user1 .getId ()).get ()); // user1.getId() is still user1
99
132
}
100
133
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
+
141
+ @ Test // this WORKS
142
+ public void clearWithDelayOk () throws InterruptedException {
143
+ cache .put ("KEY" , "VALUE" );
144
+ Thread .sleep (50 ); // give main index time to update
145
+ cache .clear ();
146
+ assertNull (cache .get ("KEY" ));
147
+ }
148
+
149
+ @ Test
150
+ public void noOpt () {}
151
+
101
152
}
0 commit comments