1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.eviction; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jboss.cache.CacheImpl; |
11 |
| import org.jboss.cache.DefaultCacheFactory; |
12 |
| import org.jboss.cache.Fqn; |
13 |
| import org.jboss.cache.Node; |
14 |
| import org.jboss.cache.NodeSPI; |
15 |
| import org.jboss.cache.lock.IsolationLevel; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class ElementSizePolicyTest extends TestCase |
22 |
| { |
23 |
| CacheImpl cache; |
24 |
| int wakeupIntervalMillis = 0; |
25 |
| final String ROOT_STR = "/test"; |
26 |
| Throwable t1_ex, t2_ex; |
27 |
| final long DURATION = 10000; |
28 |
| boolean isTrue; |
29 |
| |
30 |
3
| public ElementSizePolicyTest(String s)
|
31 |
| { |
32 |
3
| super(s);
|
33 |
| } |
34 |
| |
35 |
3
| public void setUp() throws Exception
|
36 |
| { |
37 |
3
| super.setUp();
|
38 |
3
| initCaches();
|
39 |
3
| wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
40 |
3
| log("wakeupInterval is " + wakeupIntervalMillis);
|
41 |
3
| if (wakeupIntervalMillis < 0)
|
42 |
| { |
43 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);
|
44 |
| } |
45 |
| |
46 |
3
| t1_ex = t2_ex = null;
|
47 |
3
| isTrue = true;
|
48 |
| } |
49 |
| |
50 |
3
| void initCaches() throws Exception
|
51 |
| { |
52 |
3
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-elementsize-eviction-service.xml", false);
|
53 |
3
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
54 |
3
| cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
|
55 |
3
| cache.start();
|
56 |
| } |
57 |
| |
58 |
3
| public void tearDown() throws Exception
|
59 |
| { |
60 |
3
| super.tearDown();
|
61 |
3
| cache.stop();
|
62 |
| } |
63 |
| |
64 |
1
| public void testEviction() throws Exception
|
65 |
| { |
66 |
1
| String rootStr = "/org/jboss/test/data/";
|
67 |
1
| for (int i = 0; i < 10; i++)
|
68 |
| { |
69 |
10
| String str = rootStr + i;
|
70 |
10
| Fqn fqn = Fqn.fromString(str);
|
71 |
10
| try
|
72 |
| { |
73 |
10
| cache.put(fqn, str, str);
|
74 |
10
| if (i % 2 == 0)
|
75 |
| { |
76 |
5
| for (int k = 0; k < i; k++)
|
77 |
| { |
78 |
20
| cache.put(fqn, new Integer(k), Integer.toString(k));
|
79 |
| } |
80 |
| } |
81 |
| } |
82 |
| catch (Exception e) |
83 |
| { |
84 |
0
| fail("Failed to insert data" + e);
|
85 |
0
| e.printStackTrace();
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
1
| System.out.println(cache.toString(true));
|
90 |
1
| _sleep(wakeupIntervalMillis + 500);
|
91 |
1
| System.out.println(cache.toString(true));
|
92 |
| |
93 |
1
| for (int i = 0; i < 10; i++)
|
94 |
| { |
95 |
10
| Node node = cache.get("/org/jboss/test/data/" + Integer.toString(i));
|
96 |
10
| System.out.println(node);
|
97 |
10
| if (i % 2 == 0)
|
98 |
| { |
99 |
5
| if (i < 6)
|
100 |
| { |
101 |
3
| int numElements = ((NodeSPI) node).getDataDirect().size();
|
102 |
3
| assertEquals(i + 1, numElements);
|
103 |
| } |
104 |
| else |
105 |
| { |
106 |
2
| assertNull(node);
|
107 |
| } |
108 |
| } |
109 |
| else |
110 |
| { |
111 |
5
| assertEquals(1, ((NodeSPI) node).getDataDirect().size());
|
112 |
| } |
113 |
| } |
114 |
| } |
115 |
| |
116 |
1
| public void testEviction2() throws Exception
|
117 |
| { |
118 |
1
| String rootStr = "/org/jboss/data/";
|
119 |
1
| for (int i = 0; i < 20; i++)
|
120 |
| { |
121 |
20
| String str = rootStr + Integer.toString(i);
|
122 |
20
| Fqn fqn = Fqn.fromString(str);
|
123 |
20
| cache.put(fqn, new Integer(i), str);
|
124 |
20
| for (int k = 0; k < i; k++)
|
125 |
| { |
126 |
190
| cache.put(fqn, new Integer(k), str);
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
1
| System.out.println(cache.toString(true));
|
131 |
1
| _sleep(wakeupIntervalMillis + 500);
|
132 |
1
| System.out.println(cache.toString(true));
|
133 |
| |
134 |
1
| System.out.println("*******");
|
135 |
1
| System.out.println(cache.printLockInfo());
|
136 |
| |
137 |
1
| for (int i = 0; i < 20; i++)
|
138 |
| { |
139 |
20
| String str = rootStr + Integer.toString(i);
|
140 |
20
| Fqn fqn = Fqn.fromString(str);
|
141 |
20
| Node node = cache.get(fqn);
|
142 |
20
| System.out.println(i + " " + node);
|
143 |
20
| if (i > 9)
|
144 |
| { |
145 |
10
| assertNull("Testing at " + i, node);
|
146 |
| } |
147 |
| else |
148 |
| { |
149 |
10
| assertEquals(1 + i, node.getData().size());
|
150 |
| } |
151 |
| } |
152 |
| |
153 |
1
| for (int i = 0; i < 17; i++)
|
154 |
| { |
155 |
17
| cache.put("/org/jboss/data/" + Integer.toString(3), 100 + i, "value");
|
156 |
| } |
157 |
| |
158 |
1
| Node node = cache.get("/org/jboss/data/" + Integer.toString(3));
|
159 |
1
| assertEquals(21, node.getData().size());
|
160 |
1
| _sleep(wakeupIntervalMillis + 500);
|
161 |
| |
162 |
1
| assertNull(cache.get("/org/jboss/data/" + Integer.toString(3)));
|
163 |
| } |
164 |
| |
165 |
| class MyPutter extends Thread |
166 |
| { |
167 |
10
| public MyPutter(String name)
|
168 |
| { |
169 |
10
| super(name);
|
170 |
| } |
171 |
| |
172 |
10
| public void run()
|
173 |
| { |
174 |
10
| int i = 0;
|
175 |
10
| final String myName = ROOT_STR + "/test1/node" + getName();
|
176 |
10
| while (isTrue)
|
177 |
| { |
178 |
12974
| try
|
179 |
| { |
180 |
12974
| cache.put(myName + i++, "value", i);
|
181 |
12974
| sleep(1);
|
182 |
| } |
183 |
| catch (Throwable e) |
184 |
| { |
185 |
0
| e.printStackTrace();
|
186 |
0
| if (t1_ex == null)
|
187 |
| { |
188 |
0
| t1_ex = e;
|
189 |
| } |
190 |
| } |
191 |
| } |
192 |
| } |
193 |
| } |
194 |
| |
195 |
| |
196 |
1
| public void testConcurrentPutAndEvict() throws Exception
|
197 |
| { |
198 |
1
| cache.stop();
|
199 |
1
| cache.destroy();
|
200 |
1
| cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
|
201 |
| |
202 |
1
| cache.start();
|
203 |
1
| cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1);
|
204 |
| |
205 |
1
| for (int i = 0; i < 10; i++)
|
206 |
| { |
207 |
10
| new MyPutter("Putter" + i).start();
|
208 |
| } |
209 |
| |
210 |
1
| int counter = 0;
|
211 |
1
| while (true)
|
212 |
| { |
213 |
11
| counter++;
|
214 |
11
| if (t1_ex != null)
|
215 |
| { |
216 |
0
| fail("Exception generated in put() " + t1_ex);
|
217 |
| } |
218 |
11
| log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
|
219 |
11
| _sleep(1000);
|
220 |
11
| if (counter > 10)
|
221 |
| { |
222 |
1
| isTrue = false;
|
223 |
1
| break;
|
224 |
| } |
225 |
| } |
226 |
| } |
227 |
| |
228 |
14
| void _sleep(long msecs)
|
229 |
| { |
230 |
14
| try
|
231 |
| { |
232 |
14
| Thread.sleep(msecs);
|
233 |
| } |
234 |
| catch (InterruptedException e) |
235 |
| { |
236 |
0
| e.printStackTrace();
|
237 |
| } |
238 |
| } |
239 |
| |
240 |
14
| void log(String msg)
|
241 |
| { |
242 |
14
| System.out.println("-- " + msg);
|
243 |
| } |
244 |
| |
245 |
| } |