1
2
3
4
5 package net.sf.ehcache;
6
7 import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 import static java.util.concurrent.TimeUnit.SECONDS;
9
10 import net.sf.ehcache.config.CacheConfiguration;
11 import net.sf.ehcache.config.Configuration;
12
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /***
19 * @author cdennis
20 */
21 public class DynamicCacheConfigurationTest extends AbstractCacheTest {
22
23 private static final Logger LOG = LoggerFactory.getLogger(DynamicCacheConfigurationTest.class.getName());
24
25 @Test
26 public void testTimeToIdleChange() throws InterruptedException {
27 Cache cache = new Cache("testTimeToIdleChange", 10, false, false, 0, 10);
28
29 manager.addCache(cache);
30
31 cache.put(new Element("key1", new Object()));
32 cache.put(new Element("key2", new Object()));
33
34 SECONDS.sleep(6);
35
36 cache.get("key2");
37
38 SECONDS.sleep(6);
39
40 Assert.assertNull(cache.get("key1"));
41 Assert.assertNotNull(cache.get("key2"));
42
43 cache.getCacheConfiguration().setTimeToIdleSeconds(20);
44
45 cache.put(new Element("key1", new Object()));
46
47 SECONDS.sleep(15);
48
49 Assert.assertNotNull(cache.get("key1"));
50 Assert.assertNotNull(cache.get("key2"));
51
52 SECONDS.sleep(25);
53
54 Assert.assertNull(cache.get("key1"));
55 Assert.assertNull(cache.get("key2"));
56
57 cache.getCacheConfiguration().setTimeToIdleSeconds(4);
58
59 cache.put(new Element("key1", new Object()));
60 cache.put(new Element("key2", new Object()));
61
62 SECONDS.sleep(8);
63
64 Assert.assertNull(cache.get("key1"));
65 Assert.assertNull(cache.get("key2"));
66 }
67
68 @Test
69 public void testTTLChange() throws InterruptedException {
70 Cache cache = new Cache("testTTLChange", 10, false, false, 10, 0);
71
72 manager.addCache(cache);
73
74 cache.put(new Element("key1", new Object()));
75
76 SECONDS.sleep(6);
77
78 Assert.assertNotNull(cache.get("key1"));
79 cache.put(new Element("key2", new Object()));
80
81 SECONDS.sleep(6);
82
83 Assert.assertNull(cache.get("key1"));
84 Assert.assertNotNull(cache.get("key2"));
85
86 cache.getCacheConfiguration().setTimeToLiveSeconds(20);
87
88 cache.put(new Element("key1", new Object()));
89
90 SECONDS.sleep(8);
91
92 Assert.assertNotNull(cache.get("key1"));
93 Assert.assertNotNull(cache.get("key2"));
94
95 SECONDS.sleep(8);
96
97 Assert.assertNotNull(cache.get("key1"));
98 Assert.assertNull(cache.get("key2"));
99
100 SECONDS.sleep(10);
101
102 Assert.assertNull(cache.get("key1"));
103
104 cache.getCacheConfiguration().setTimeToLiveSeconds(4);
105
106 cache.put(new Element("key1", new Object()));
107 cache.put(new Element("key2", new Object()));
108
109 SECONDS.sleep(8);
110
111 Assert.assertNull(cache.get("key1"));
112 Assert.assertNull(cache.get("key2"));
113 }
114
115 @Test
116 public void testTimeToIdleChangeWithCustomElements() throws InterruptedException {
117 Cache cache = new Cache("testTimeToIdleChangeWithCustomElements", 10, false, false, 0, 10);
118
119 manager.addCache(cache);
120
121 cache.put(new Element("default", new Object()));
122 cache.put(new Element("eternal", new Object(), true, 0, 0));
123 cache.put(new Element("short", new Object(), false, 1, 1));
124 cache.put(new Element("long", new Object(), false, 100, 100));
125
126 SECONDS.sleep(6);
127
128 Assert.assertNull(cache.get("short"));
129
130 SECONDS.sleep(6);
131
132 Assert.assertNull(cache.get("default"));
133 Assert.assertNotNull(cache.get("eternal"));
134 Assert.assertNull(cache.get("short"));
135 Assert.assertNotNull(cache.get("long"));
136
137 cache.getCacheConfiguration().setTimeToIdleSeconds(20);
138
139 cache.put(new Element("default", new Object()));
140 cache.put(new Element("short", new Object(), false, 1, 1));
141
142 SECONDS.sleep(15);
143
144 Assert.assertNotNull(cache.get("default"));
145 Assert.assertNotNull(cache.get("eternal"));
146 Assert.assertNull(cache.get("short"));
147 Assert.assertNotNull(cache.get("long"));
148
149 SECONDS.sleep(25);
150
151 Assert.assertNull(cache.get("default"));
152 Assert.assertNotNull(cache.get("eternal"));
153 Assert.assertNull(cache.get("short"));
154 Assert.assertNotNull(cache.get("long"));
155
156 cache.getCacheConfiguration().setTimeToIdleSeconds(4);
157
158 cache.put(new Element("default", new Object()));
159 cache.put(new Element("short", new Object(), false, 1, 1));
160
161 SECONDS.sleep(8);
162
163 Assert.assertNull(cache.get("default"));
164 Assert.assertNotNull(cache.get("eternal"));
165 Assert.assertNull(cache.get("short"));
166 Assert.assertNotNull(cache.get("long"));
167 }
168
169 @Test
170 public void testTTLChangeWithCustomElement() throws InterruptedException {
171 Cache cache = new Cache("testTTLChangeWithCustomElements", 10, false, false, 10, 0);
172
173 manager.addCache(cache);
174
175 cache.put(new Element("default", new Object()));
176 cache.put(new Element("eternal", new Object(), true, 0, 0));
177 cache.put(new Element("short", new Object(), false, 1, 1));
178 cache.put(new Element("long", new Object(), false, 100, 100));
179
180 SECONDS.sleep(6);
181
182 Assert.assertNotNull(cache.get("default"));
183 Assert.assertNotNull(cache.get("eternal"));
184 Assert.assertNull(cache.get("short"));
185 Assert.assertNotNull(cache.get("long"));
186
187 SECONDS.sleep(6);
188
189 Assert.assertNull(cache.get("default"));
190 Assert.assertNotNull(cache.get("eternal"));
191 Assert.assertNull(cache.get("short"));
192 Assert.assertNotNull(cache.get("long"));
193
194 cache.getCacheConfiguration().setTimeToLiveSeconds(20);
195
196 cache.put(new Element("default", new Object()));
197 cache.put(new Element("short", new Object(), false, 1, 1));
198
199 SECONDS.sleep(6);
200
201 Assert.assertNotNull(cache.get("default"));
202 Assert.assertNotNull(cache.get("eternal"));
203 Assert.assertNull(cache.get("short"));
204 Assert.assertNotNull(cache.get("long"));
205
206 SECONDS.sleep(6);
207
208 Assert.assertNotNull(cache.get("default"));
209 Assert.assertNotNull(cache.get("eternal"));
210 Assert.assertNull(cache.get("short"));
211 Assert.assertNotNull(cache.get("long"));
212
213 SECONDS.sleep(10);
214
215 Assert.assertNull(cache.get("default"));
216 Assert.assertNotNull(cache.get("eternal"));
217 Assert.assertNull(cache.get("short"));
218 Assert.assertNotNull(cache.get("long"));
219
220 cache.getCacheConfiguration().setTimeToLiveSeconds(4);
221
222 cache.put(new Element("default", new Object()));
223 cache.put(new Element("short", new Object(), false, 1, 1));
224
225 SECONDS.sleep(8);
226
227 Assert.assertNull(cache.get("default"));
228 Assert.assertNotNull(cache.get("eternal"));
229 Assert.assertNull(cache.get("short"));
230 Assert.assertNotNull(cache.get("long"));
231 }
232
233 @Test
234 public void testMemoryCapacityChange() {
235 Cache cache = new Cache("testMemoryCapacityChange", 10, false, true, 0, 0);
236 manager.addCache(cache);
237
238 for (int i = 0; i < 20; i++) {
239 cache.put(new Element("key" + i, new Object()));
240 Assert.assertTrue(cache.getSize() <= 10);
241 Assert.assertTrue(cache.getMemoryStoreSize() <= 10);
242 }
243
244 cache.getCacheConfiguration().setMaxElementsInMemory(20);
245
246 for (int i = 20; i < 40; i++) {
247 cache.put(new Element("key" + i, new Object()));
248 Assert.assertTrue(cache.getSize() <= 20);
249 Assert.assertTrue(cache.getSize() > 10);
250 Assert.assertTrue(cache.getMemoryStoreSize() <= 20);
251 Assert.assertTrue(cache.getMemoryStoreSize() > 10);
252 }
253
254 cache.getCacheConfiguration().setMaxElementsInMemory(5);
255
256 for (int i = 40; i < 60; i++) {
257 cache.put(new Element("key" + i, new Object()));
258 }
259
260 Assert.assertEquals(5, cache.getSize());
261 Assert.assertEquals(5, cache.getMemoryStoreSize());
262 }
263
264 @Test
265 public void testDiskCapacityChange() throws InterruptedException {
266 Cache cache = new Cache("testDiskCapacityChange", 10, true, true, 0, 0);
267 cache.getCacheConfiguration().setMaxElementsOnDisk(10);
268 manager.addCache(cache);
269
270 for (int i = 0; i < 40; i++) {
271 cache.put(new Element("key" + i, new byte[0]));
272 MILLISECONDS.sleep(400);
273 Assert.assertTrue(cache.getSize() <= 20);
274 Assert.assertTrue(cache.getMemoryStoreSize() <= 10);
275 Assert.assertTrue(cache.getDiskStoreSize() <= 10);
276 }
277
278 cache.getCacheConfiguration().setMaxElementsOnDisk(20);
279
280 for (int i = 40; i < 80; i++) {
281 cache.put(new Element("key" + i, new byte[0]));
282 MILLISECONDS.sleep(400);
283 Assert.assertTrue(cache.getSize() <= 30);
284 Assert.assertTrue(cache.getSize() > 10);
285 Assert.assertTrue(cache.getMemoryStoreSize() <= 10);
286 Assert.assertTrue(cache.getDiskStoreSize() <= 20);
287 Assert.assertTrue(cache.getDiskStoreSize() > 10);
288 }
289
290 cache.getCacheConfiguration().setMaxElementsOnDisk(5);
291
292 for (int i = 80; i < 120; i++) {
293 cache.put(new Element("key" + i, new byte[0]));
294 MILLISECONDS.sleep(400);
295 }
296
297 Assert.assertEquals(10, cache.getSize());
298 Assert.assertEquals(10, cache.getMemoryStoreSize());
299 Assert.assertEquals(5, cache.getDiskStoreSize());
300 }
301
302 @Test
303 public void testCacheWithFrozenConfig() {
304 Configuration managerConfig = new Configuration()
305 .dynamicConfig(false)
306 .defaultCache(new CacheConfiguration("definedCache1", 20))
307 .cache(new CacheConfiguration("definedCache", 10).eternal(true));
308
309 CacheManager manager = new CacheManager(managerConfig);
310
311 Cache defined = manager.getCache("definedCache");
312 try {
313 defined.getCacheConfiguration().setTimeToIdleSeconds(99);
314 Assert.fail();
315 } catch (CacheException e) {
316
317 }
318
319 try {
320 defined.setDisabled(true);
321 Assert.fail();
322 } catch (CacheException e) {
323
324 }
325
326 defined.put(new Element("key", "value"));
327 Assert.assertNotNull(defined.get("key"));
328
329 Cache programmatic = new Cache("programmatic", 10, false, true, 0, 0);
330 manager.addCache(programmatic);
331 try {
332 programmatic.getCacheConfiguration().setTimeToIdleSeconds(99);
333 Assert.fail();
334 } catch (CacheException e) {
335
336 }
337
338 try {
339 programmatic.setDisabled(true);
340 Assert.fail();
341 } catch (CacheException e) {
342
343 }
344
345 programmatic.put(new Element("key", "value"));
346 Assert.assertNotNull(programmatic.get("key"));
347 }
348
349 @Test
350 public void testConfiguringClonedCache() throws CloneNotSupportedException {
351 Cache cache = new Cache("testConfiguringClonedCache", 10, false, true, 0, 0);
352 Cache clone = cache.clone();
353 clone.setName("testConfiguringClonedCacheCloned");
354
355 manager.addCache(cache);
356 manager.addCache(clone);
357
358 Assert.assertEquals(10, cache.getCacheConfiguration().getMaxElementsInMemory());
359 Assert.assertEquals(10, clone.getCacheConfiguration().getMaxElementsInMemory());
360
361 for (int i = 0; i < 20; i++) {
362 cache.put(new Element("key" + i, new Object()));
363 Assert.assertTrue(cache.getSize() <= 10);
364 Assert.assertTrue(cache.getMemoryStoreSize() <= 10);
365 }
366
367 for (int i = 0; i < 20; i++) {
368 clone.put(new Element("key" + i, new Object()));
369 Assert.assertTrue(clone.getSize() <= 10);
370 Assert.assertTrue(clone.getMemoryStoreSize() <= 10);
371 }
372
373 cache.getCacheConfiguration().setMaxElementsInMemory(20);
374 clone.getCacheConfiguration().setMaxElementsInMemory(5);
375
376 for (int i = 20; i < 40; i++) {
377 cache.put(new Element("key" + i, new Object()));
378 Assert.assertTrue(cache.getSize() <= 20);
379 Assert.assertTrue(cache.getSize() > 10);
380 Assert.assertTrue(cache.getMemoryStoreSize() <= 20);
381 Assert.assertTrue(cache.getMemoryStoreSize() > 10);
382 }
383
384 for (int i = 20; i < 40; i++) {
385 clone.put(new Element("key" + i, new Object()));
386 }
387
388 Assert.assertEquals(5, clone.getSize());
389 Assert.assertEquals(5, clone.getMemoryStoreSize());
390
391 cache.getCacheConfiguration().setMaxElementsInMemory(5);
392 clone.getCacheConfiguration().setMaxElementsInMemory(20);
393
394 for (int i = 40; i < 60; i++) {
395 cache.put(new Element("key" + i, new Object()));
396 }
397
398 Assert.assertEquals(5, cache.getSize());
399 Assert.assertEquals(5, cache.getMemoryStoreSize());
400
401 for (int i = 40; i < 60; i++) {
402 clone.put(new Element("key" + i, new Object()));
403 Assert.assertTrue(clone.getSize() <= 20);
404 Assert.assertTrue(clone.getSize() > 5);
405 Assert.assertTrue(clone.getMemoryStoreSize() <= 20);
406 Assert.assertTrue(clone.getMemoryStoreSize() > 5);
407 }
408 }
409 }