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