1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.eviction; |
24 |
| |
25 |
| import junit.framework.Test; |
26 |
| import junit.framework.TestCase; |
27 |
| import junit.framework.TestSuite; |
28 |
| import org.jboss.cache.CacheImpl; |
29 |
| import org.jboss.cache.DefaultCacheFactory; |
30 |
| import org.jboss.cache.Fqn; |
31 |
| import org.jboss.cache.RegionManager; |
32 |
| import org.jboss.cache.config.EvictionConfig; |
33 |
| import org.jboss.cache.config.EvictionRegionConfig; |
34 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
35 |
| import org.jboss.cache.lock.IsolationLevel; |
36 |
| import org.jboss.cache.misc.TestingUtil; |
37 |
| import org.jboss.cache.xml.XmlHelper; |
38 |
| import org.w3c.dom.Element; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public class ProgrammaticLRUPolicyTest extends TestCase |
47 |
| { |
48 |
| CacheImpl cache_; |
49 |
| int wakeupIntervalMillis_ = 0; |
50 |
| |
51 |
3
| public ProgrammaticLRUPolicyTest(String s)
|
52 |
| { |
53 |
3
| super(s);
|
54 |
| } |
55 |
| |
56 |
3
| public void setUp() throws Exception
|
57 |
| { |
58 |
3
| super.setUp();
|
59 |
3
| initCaches();
|
60 |
3
| wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
61 |
3
| log("wakeupInterval is " + wakeupIntervalMillis_);
|
62 |
3
| if (wakeupIntervalMillis_ < 0)
|
63 |
| { |
64 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
|
65 |
| } |
66 |
| |
67 |
| } |
68 |
| |
69 |
3
| public void initCaches() throws Exception
|
70 |
| { |
71 |
3
| cache_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-lru-eviction-service.xml", false);
|
72 |
3
| cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
73 |
3
| cache_.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
|
74 |
| |
75 |
3
| cache_.create();
|
76 |
3
| cache_.start();
|
77 |
| } |
78 |
| |
79 |
3
| public void tearDown() throws Exception
|
80 |
| { |
81 |
3
| super.tearDown();
|
82 |
3
| cache_.stop();
|
83 |
| } |
84 |
| |
85 |
2
| private void addStringBasedRegion() throws Exception
|
86 |
| { |
87 |
| |
88 |
2
| String xml = "<region name=\"/dummy\">" +
|
89 |
| "<attribute name=\"maxNodes\">10000</attribute>" + |
90 |
| "<attribute name=\"timeToLiveSeconds\">4</attribute>" + |
91 |
| "</region>"; |
92 |
2
| Element element = XmlHelper.stringToElement(xml);
|
93 |
2
| RegionManager regionManager = cache_.getRegionManager();
|
94 |
2
| EvictionConfig topConfig = cache_.getConfiguration().getEvictionConfig();
|
95 |
2
| EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element, topConfig.getDefaultEvictionPolicyClass(), topConfig.getDefaultEventQueueSize());
|
96 |
2
| regionManager.setEvictionConfig(topConfig);
|
97 |
| |
98 |
2
| regionManager.getRegion("/programmatic", true).setEvictionPolicy(erc.getEvictionPolicyConfig());
|
99 |
| } |
100 |
| |
101 |
1
| public void testStringBasedFqnEviction() throws Exception
|
102 |
| { |
103 |
1
| addStringBasedRegion();
|
104 |
| |
105 |
1
| String rootStr = "/programmatic/";
|
106 |
1
| for (int i = 0; i < 10; i++)
|
107 |
| { |
108 |
10
| String str = rootStr + i;
|
109 |
10
| Fqn fqn = Fqn.fromString(str);
|
110 |
10
| cache_.put(fqn, str, str);
|
111 |
| } |
112 |
| |
113 |
1
| String val = (String) cache_.get(rootStr + "3", rootStr + "3");
|
114 |
1
| assertNotNull("DataNode should be empty ", val);
|
115 |
| |
116 |
1
| System.out.println(cache_.toString());
|
117 |
1
| TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
|
118 |
1
| System.out.println(cache_.toString());
|
119 |
1
| val = (String) cache_.get(rootStr + "3", rootStr + "3");
|
120 |
1
| assertNull("DataNode should be empty ", val);
|
121 |
| } |
122 |
| |
123 |
1
| private void addObjectBasedRegion() throws Exception
|
124 |
| { |
125 |
| |
126 |
1
| String xml = "<region name=\"/dummy\">" +
|
127 |
| "<attribute name=\"maxNodes\">10000</attribute>" + |
128 |
| "<attribute name=\"timeToLiveSeconds\">4</attribute>" + |
129 |
| "</region>"; |
130 |
1
| Element element = XmlHelper.stringToElement(xml);
|
131 |
1
| RegionManager regionManager = cache_.getRegionManager();
|
132 |
1
| EvictionConfig topEC = cache_.getConfiguration().getEvictionConfig();
|
133 |
1
| EvictionRegionConfig erc = XmlConfigurationParser.parseEvictionRegionConfig(element,
|
134 |
| topEC.getDefaultEvictionPolicyClass(), |
135 |
| topEC.getDefaultEventQueueSize()); |
136 |
| |
137 |
1
| Integer ii = 1;
|
138 |
1
| Fqn fqn = new Fqn(ii);
|
139 |
1
| regionManager.getRegion(fqn, true).setEvictionPolicy(erc.getEvictionPolicyConfig());
|
140 |
| } |
141 |
| |
142 |
1
| public void testObjectBasedFqnEviction1() throws Exception
|
143 |
| { |
144 |
1
| addStringBasedRegion();
|
145 |
| |
146 |
1
| String rootStr = "/programmatic/";
|
147 |
1
| for (int i = 0; i < 10; i++)
|
148 |
| { |
149 |
10
| String str = rootStr;
|
150 |
10
| Integer in = i;
|
151 |
10
| Fqn fqn = new Fqn(Fqn.fromString(str), in);
|
152 |
10
| try
|
153 |
| { |
154 |
10
| cache_.put(fqn, str, str);
|
155 |
| } |
156 |
| catch (Exception e) |
157 |
| { |
158 |
0
| fail("Failed to insert data" + e);
|
159 |
0
| e.printStackTrace();
|
160 |
| } |
161 |
| } |
162 |
| |
163 |
1
| Integer in = 3;
|
164 |
1
| Fqn fqn = new Fqn(Fqn.fromString(rootStr), in);
|
165 |
1
| try
|
166 |
| { |
167 |
1
| String val = (String) cache_.get(fqn, in);
|
168 |
1
| assertNull("DataNode should be empty ", val);
|
169 |
| } |
170 |
| catch (Exception e) |
171 |
| { |
172 |
0
| e.printStackTrace();
|
173 |
0
| fail("Failed to get" + e);
|
174 |
| } |
175 |
| |
176 |
1
| System.out.println(cache_.toString());
|
177 |
1
| TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
|
178 |
1
| System.out.println(cache_.toString());
|
179 |
| |
180 |
1
| try
|
181 |
| { |
182 |
1
| String val = (String) cache_.get(fqn, in);
|
183 |
1
| assertNull("DataNode should be empty ", val);
|
184 |
| } |
185 |
| catch (Exception e) |
186 |
| { |
187 |
0
| e.printStackTrace();
|
188 |
0
| fail("Failed to get" + e);
|
189 |
| } |
190 |
| } |
191 |
| |
192 |
1
| public void testObjectBasedFqnEviction2() throws Exception
|
193 |
| { |
194 |
1
| addObjectBasedRegion();
|
195 |
| |
196 |
1
| Integer ii = 1;
|
197 |
1
| Fqn rootfqn = new Fqn(ii);
|
198 |
1
| for (int i = 0; i < 10; i++)
|
199 |
| { |
200 |
10
| Integer in = i;
|
201 |
10
| Fqn fqn = new Fqn(rootfqn, in);
|
202 |
10
| try
|
203 |
| { |
204 |
10
| cache_.put(fqn, in, in);
|
205 |
| } |
206 |
| catch (Exception e) |
207 |
| { |
208 |
0
| fail("Failed to insert data" + e);
|
209 |
0
| e.printStackTrace();
|
210 |
| } |
211 |
| } |
212 |
| |
213 |
1
| try
|
214 |
| { |
215 |
1
| Integer in = 3;
|
216 |
1
| Fqn fqn = new Fqn(rootfqn, in);
|
217 |
1
| Object val = cache_.get(fqn, in);
|
218 |
1
| assertNotNull("DataNode should be empty ", val);
|
219 |
| } |
220 |
| catch (Exception e) |
221 |
| { |
222 |
0
| e.printStackTrace();
|
223 |
0
| fail("Failed to get" + e);
|
224 |
| } |
225 |
| |
226 |
1
| System.out.println(cache_.toString());
|
227 |
1
| TestingUtil.sleepThread(2 * wakeupIntervalMillis_ + 500);
|
228 |
1
| System.out.println(cache_.toString());
|
229 |
1
| try
|
230 |
| { |
231 |
1
| Integer in = 3;
|
232 |
1
| Fqn fqn = new Fqn(rootfqn, in);
|
233 |
1
| Object val = cache_.get(fqn, in);
|
234 |
1
| assertNull("DataNode should be empty ", val);
|
235 |
| } |
236 |
| catch (Exception e) |
237 |
| { |
238 |
0
| e.printStackTrace();
|
239 |
0
| fail("Failed to get" + e);
|
240 |
| } |
241 |
| } |
242 |
| |
243 |
3
| void log(String msg)
|
244 |
| { |
245 |
3
| System.out.println("-- " + msg);
|
246 |
| } |
247 |
| |
248 |
1
| public static Test suite()
|
249 |
| { |
250 |
1
| return new TestSuite(ProgrammaticLRUPolicyTest.class);
|
251 |
| } |
252 |
| |
253 |
0
| public static void main(String[] args)
|
254 |
| { |
255 |
0
| junit.textui.TestRunner.run(suite());
|
256 |
| } |
257 |
| |
258 |
| } |