1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.eviction; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.cache.CacheImpl; |
13 |
| import org.jboss.cache.DefaultCacheFactory; |
14 |
| import org.jboss.cache.Fqn; |
15 |
| import org.jboss.cache.Region; |
16 |
| import org.jboss.cache.config.Configuration; |
17 |
| import org.jboss.cache.config.EvictionConfig; |
18 |
| import org.jboss.cache.misc.TestingUtil; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| public class ExpirationPolicyTest extends TestCase |
27 |
| { |
28 |
| private static final Log log = LogFactory.getLog(ExpirationPolicyTest.class); |
29 |
| |
30 |
| private CacheImpl cache; |
31 |
| |
32 |
| Fqn fqn1 = Fqn.fromString("/node/1"); |
33 |
| Fqn fqn2 = Fqn.fromString("/node/2"); |
34 |
| Fqn fqn3 = Fqn.fromString("/node/3"); |
35 |
| Fqn fqn4 = Fqn.fromString("/node/4"); |
36 |
| |
37 |
| Long future; |
38 |
| Long past; |
39 |
| |
40 |
3
| public void setUp() throws Exception
|
41 |
| { |
42 |
3
| super.setUp();
|
43 |
3
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
44 |
3
| Configuration conf = cache.getConfiguration();
|
45 |
3
| EvictionConfig econf = new EvictionConfig(ExpirationPolicy.class.getName());
|
46 |
3
| econf.setWakeupIntervalSeconds(1);
|
47 |
3
| conf.setEvictionConfig(econf);
|
48 |
3
| cache.setConfiguration(conf);
|
49 |
3
| cache.start();
|
50 |
| |
51 |
3
| future = System.currentTimeMillis() + 4000;
|
52 |
3
| past = System.currentTimeMillis() - 2000;
|
53 |
| } |
54 |
| |
55 |
3
| public void tearDown() throws Exception
|
56 |
| { |
57 |
3
| super.tearDown();
|
58 |
3
| cache.stop();
|
59 |
| } |
60 |
| |
61 |
1
| public void testEviction() throws Exception
|
62 |
| { |
63 |
1
| cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future);
|
64 |
1
| cache.put(fqn2, ExpirationConfiguration.EXPIRATION_KEY, past);
|
65 |
1
| cache.put(fqn3, ExpirationConfiguration.EXPIRATION_KEY, future);
|
66 |
1
| cache.put(fqn4, "foo", "bar");
|
67 |
1
| TestingUtil.sleepThread(2000);
|
68 |
1
| assertNotNull(cache.get(fqn1));
|
69 |
1
| assertNull(cache.get(fqn2));
|
70 |
1
| assertNotNull(cache.get(fqn3));
|
71 |
1
| assertNotNull(cache.get(fqn4));
|
72 |
| |
73 |
1
| log.info("should remove 1 and 3 now");
|
74 |
1
| TestingUtil.sleepThread(3000);
|
75 |
1
| assertNull(cache.get(fqn1));
|
76 |
1
| assertNull(cache.get(fqn3));
|
77 |
| } |
78 |
| |
79 |
1
| public void testUpdate() throws Exception
|
80 |
| { |
81 |
1
| log.info("update 1 from future to past");
|
82 |
1
| cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future);
|
83 |
1
| assertNotNull(cache.get(fqn1));
|
84 |
1
| TestingUtil.sleepThread(1500);
|
85 |
1
| cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, past);
|
86 |
1
| TestingUtil.sleepThread(1500);
|
87 |
1
| assertNull(cache.get(fqn1));
|
88 |
1
| cache.remove(Fqn.ROOT);
|
89 |
| |
90 |
| } |
91 |
| |
92 |
1
| public void testMaxNodes() throws Exception
|
93 |
| { |
94 |
1
| log.info("set max nodes to 2, expire soonest to expire first");
|
95 |
1
| EvictionPolicyConfig epc = cache.getRegionManager().getAllRegions(Region.Type.EVICTION).get(0).getEvictionPolicyConfig();
|
96 |
1
| ExpirationConfiguration ec = (ExpirationConfiguration) epc;
|
97 |
1
| ec.setMaxNodes(2);
|
98 |
1
| Long future2 = future + 500;
|
99 |
1
| cache.put(fqn1, ExpirationConfiguration.EXPIRATION_KEY, future2);
|
100 |
1
| cache.put(fqn2, ExpirationConfiguration.EXPIRATION_KEY, future2);
|
101 |
1
| cache.put(fqn3, ExpirationConfiguration.EXPIRATION_KEY, future);
|
102 |
1
| cache.put(fqn4, ExpirationConfiguration.EXPIRATION_KEY, past);
|
103 |
1
| assertEquals(5, cache.getNumberOfNodes());
|
104 |
1
| Thread.sleep(2000);
|
105 |
1
| assertNotNull(cache.get(fqn1));
|
106 |
1
| assertNotNull(cache.get(fqn2));
|
107 |
1
| assertNull(cache.get(fqn3));
|
108 |
1
| assertNull(cache.get(fqn4));
|
109 |
| } |
110 |
| |
111 |
| } |