1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.cache.tests; |
21 |
|
|
22 |
|
import org.junit.Assert; |
23 |
|
import org.junit.Test; |
24 |
|
import org.xwiki.cache.Cache; |
25 |
|
import org.xwiki.cache.CacheFactory; |
26 |
|
import org.xwiki.cache.config.CacheConfiguration; |
27 |
|
import org.xwiki.cache.config.LRUCacheConfiguration; |
28 |
|
import org.xwiki.cache.eviction.EntryEvictionConfiguration; |
29 |
|
import org.xwiki.cache.eviction.LRUEvictionConfiguration; |
30 |
|
import org.xwiki.cache.tests.CacheEntryListenerTest.EventType; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
|
|
|
| 88.8% |
Uncovered Elements: 12 (107) |
Complexity: 13 |
Complexity Density: 0.15 |
|
39 |
|
public abstract class AbstractEvictionGenericTestCache extends AbstractGenericTestCache |
40 |
|
{ |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
private boolean supportEvictionEvent; |
45 |
|
|
46 |
|
|
47 |
|
@param |
48 |
|
@param |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
50 |
11 |
protected AbstractEvictionGenericTestCache(String roleHint, boolean supportEvictionEvent)... |
51 |
|
{ |
52 |
11 |
super(roleHint); |
53 |
|
|
54 |
11 |
this.supportEvictionEvent = supportEvictionEvent; |
55 |
|
} |
56 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
57 |
0 |
protected void customizeEviction(EntryEvictionConfiguration eviction)... |
58 |
|
{ |
59 |
|
|
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@throws |
69 |
|
|
|
|
| 88.5% |
Uncovered Elements: 3 (26) |
Complexity: 3 |
Complexity Density: 0.14 |
4-
|
|
70 |
1 |
@Test... |
71 |
|
public void testCreateAndDestroyCacheLRUMaxEntries() throws Exception |
72 |
|
{ |
73 |
1 |
CacheFactory factory = getCacheFactory(); |
74 |
|
|
75 |
1 |
CacheConfiguration conf = new CacheConfiguration(); |
76 |
1 |
LRUEvictionConfiguration lec = new LRUEvictionConfiguration(); |
77 |
1 |
lec.setMaxEntries(1); |
78 |
1 |
customizeEviction(lec); |
79 |
1 |
conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec); |
80 |
|
|
81 |
1 |
Cache<Object> cache = factory.newCache(conf); |
82 |
|
|
83 |
1 |
Assert.assertNotNull(cache); |
84 |
|
|
85 |
1 |
CacheEntryListenerTest eventListener; |
86 |
1 |
if (this.supportEvictionEvent) { |
87 |
1 |
eventListener = new CacheEntryListenerTest(); |
88 |
1 |
cache.addCacheEntryListener(eventListener); |
89 |
|
} else { |
90 |
0 |
eventListener = null; |
91 |
|
} |
92 |
|
|
93 |
1 |
cache.set(KEY, VALUE); |
94 |
|
|
95 |
1 |
Assert.assertEquals(VALUE, cache.get(KEY)); |
96 |
|
|
97 |
1 |
cache.set(KEY2, VALUE2); |
98 |
|
|
99 |
1 |
if (eventListener != null) { |
100 |
1 |
Assert.assertTrue("No value has been evicted from the cache", |
101 |
|
eventListener.waitForEntryEvent(EventType.REMOVE)); |
102 |
1 |
Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue()); |
103 |
|
} |
104 |
|
|
105 |
1 |
Assert.assertNull(cache.get(KEY)); |
106 |
1 |
Assert.assertEquals(VALUE2, cache.get(KEY2)); |
107 |
|
|
108 |
1 |
cache.dispose(); |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@throws |
115 |
|
|
|
|
| 87.5% |
Uncovered Elements: 3 (24) |
Complexity: 3 |
Complexity Density: 0.15 |
4-
|
|
116 |
1 |
@Test... |
117 |
|
public void testCreateAndDestroyCacheLRUMAxIdle() throws Exception |
118 |
|
{ |
119 |
1 |
CacheFactory factory = getCacheFactory(); |
120 |
|
|
121 |
1 |
CacheConfiguration conf = new CacheConfiguration(); |
122 |
1 |
LRUEvictionConfiguration lec = new LRUEvictionConfiguration(); |
123 |
1 |
lec.setMaxIdle(1); |
124 |
1 |
customizeEviction(lec); |
125 |
1 |
conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec); |
126 |
|
|
127 |
1 |
Cache<Object> cache = factory.newCache(conf); |
128 |
|
|
129 |
1 |
Assert.assertNotNull(cache); |
130 |
|
|
131 |
1 |
CacheEntryListenerTest eventListener; |
132 |
1 |
if (this.supportEvictionEvent) { |
133 |
1 |
eventListener = new CacheEntryListenerTest(); |
134 |
1 |
cache.addCacheEntryListener(eventListener); |
135 |
|
} else { |
136 |
0 |
eventListener = null; |
137 |
|
} |
138 |
|
|
139 |
1 |
cache.set(KEY, VALUE); |
140 |
|
|
141 |
1 |
Assert.assertEquals(VALUE, cache.get(KEY)); |
142 |
|
|
143 |
1 |
if (eventListener != null) { |
144 |
1 |
Assert.assertTrue("No value has expired from the cache after provided max idle time", |
145 |
|
eventListener.waitForEntryEvent(EventType.REMOVE)); |
146 |
1 |
Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue()); |
147 |
|
} |
148 |
|
|
149 |
1 |
Assert.assertNull(cache.get(KEY)); |
150 |
|
|
151 |
1 |
cache.dispose(); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@throws |
158 |
|
|
|
|
| 87.5% |
Uncovered Elements: 3 (24) |
Complexity: 3 |
Complexity Density: 0.15 |
4-
|
|
159 |
1 |
@Test... |
160 |
|
public void testCreateAndDestroyCacheLRULifespan() throws Exception |
161 |
|
{ |
162 |
1 |
CacheFactory factory = getCacheFactory(); |
163 |
|
|
164 |
1 |
CacheConfiguration conf = new CacheConfiguration(); |
165 |
1 |
LRUEvictionConfiguration lec = new LRUEvictionConfiguration(); |
166 |
1 |
lec.setLifespan(1); |
167 |
1 |
customizeEviction(lec); |
168 |
1 |
conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec); |
169 |
|
|
170 |
1 |
Cache<Object> cache = factory.newCache(conf); |
171 |
|
|
172 |
1 |
Assert.assertNotNull(cache); |
173 |
|
|
174 |
1 |
CacheEntryListenerTest eventListener; |
175 |
1 |
if (this.supportEvictionEvent) { |
176 |
1 |
eventListener = new CacheEntryListenerTest(); |
177 |
1 |
cache.addCacheEntryListener(eventListener); |
178 |
|
} else { |
179 |
0 |
eventListener = null; |
180 |
|
} |
181 |
|
|
182 |
1 |
cache.set(KEY, VALUE); |
183 |
|
|
184 |
1 |
Assert.assertEquals(VALUE, cache.get(KEY)); |
185 |
|
|
186 |
1 |
if (eventListener != null) { |
187 |
1 |
Assert.assertTrue("No value has expired from the cache after provide lifespan", |
188 |
|
eventListener.waitForEntryEvent(EventType.REMOVE)); |
189 |
1 |
Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue()); |
190 |
|
} |
191 |
|
|
192 |
1 |
Assert.assertNull(cache.get(KEY)); |
193 |
|
|
194 |
1 |
cache.dispose(); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
@throws |
201 |
|
|
|
|
| 92% |
Uncovered Elements: 2 (25) |
Complexity: 2 |
Complexity Density: 0.09 |
4-
|
|
202 |
1 |
@Test... |
203 |
|
public void testCreateAndDestroyCacheLRUAll() throws Exception |
204 |
|
{ |
205 |
1 |
CacheFactory factory = getCacheFactory(); |
206 |
|
|
207 |
1 |
LRUCacheConfiguration conf = new LRUCacheConfiguration(); |
208 |
1 |
LRUEvictionConfiguration lec = conf.getLRUEvictionConfiguration(); |
209 |
1 |
lec.setMaxEntries(1); |
210 |
1 |
lec.setMaxIdle(1); |
211 |
1 |
lec.setLifespan(1); |
212 |
1 |
customizeEviction(lec); |
213 |
|
|
214 |
1 |
Cache<Object> cache = factory.newCache(conf); |
215 |
|
|
216 |
1 |
Assert.assertNotNull(cache); |
217 |
|
|
218 |
1 |
CacheEntryListenerTest eventListener; |
219 |
1 |
if (this.supportEvictionEvent) { |
220 |
1 |
eventListener = new CacheEntryListenerTest(); |
221 |
1 |
cache.addCacheEntryListener(eventListener); |
222 |
|
} else { |
223 |
0 |
eventListener = null; |
224 |
|
} |
225 |
|
|
226 |
1 |
cache.set(KEY, VALUE); |
227 |
|
|
228 |
1 |
Assert.assertEquals(VALUE, cache.get(KEY)); |
229 |
|
|
230 |
1 |
cache.set(KEY2, VALUE2); |
231 |
|
|
232 |
1 |
Assert.assertNull(cache.get(KEY)); |
233 |
1 |
Assert.assertEquals(VALUE2, cache.get(KEY2)); |
234 |
|
|
235 |
1 |
Thread.sleep(1100); |
236 |
|
|
237 |
1 |
Assert.assertNull(cache.get(KEY)); |
238 |
1 |
Assert.assertNull(cache.get(KEY2)); |
239 |
|
|
240 |
1 |
cache.dispose(); |
241 |
|
} |
242 |
|
} |