1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.cache;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21
22 import org.apache.ibatis.cache.decorators.LoggingCache;
23 import org.apache.ibatis.cache.decorators.ScheduledCache;
24 import org.apache.ibatis.cache.impl.PerpetualCache;
25 import org.junit.jupiter.api.Test;
26
27 class ScheduledCacheTest {
28
29 @Test
30 void shouldDemonstrateHowAllObjectsAreFlushedAfterBasedOnTime() throws Exception {
31 Cache cache = new PerpetualCache("DefaultCache");
32 cache = new ScheduledCache(cache);
33 ((ScheduledCache) cache).setClearInterval(2500);
34 cache = new LoggingCache(cache);
35 for (int i = 0; i < 100; i++) {
36 cache.putObject(i, i);
37 assertEquals(i, cache.getObject(i));
38 }
39 Thread.sleep(5000);
40 assertEquals(0, cache.getSize());
41 }
42
43 @Test
44 void shouldRemoveItemOnDemand() {
45 Cache cache = new PerpetualCache("DefaultCache");
46 cache = new ScheduledCache(cache);
47 ((ScheduledCache) cache).setClearInterval(60000);
48 cache = new LoggingCache(cache);
49 cache.putObject(0, 0);
50 assertNotNull(cache.getObject(0));
51 cache.removeObject(0);
52 assertNull(cache.getObject(0));
53 }
54
55 @Test
56 void shouldFlushAllItemsOnDemand() {
57 Cache cache = new PerpetualCache("DefaultCache");
58 cache = new ScheduledCache(cache);
59 ((ScheduledCache) cache).setClearInterval(60000);
60 cache = new LoggingCache(cache);
61 for (int i = 0; i < 5; i++) {
62 cache.putObject(i, i);
63 }
64 assertNotNull(cache.getObject(0));
65 assertNotNull(cache.getObject(4));
66 cache.clear();
67 assertNull(cache.getObject(0));
68 assertNull(cache.getObject(4));
69 }
70
71 }