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.lock.IsolationLevel; |
13 |
| import org.jboss.cache.misc.TestingUtil; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class MRUPolicyTest 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 void setUp() throws Exception
|
31 |
| { |
32 |
3
| super.setUp();
|
33 |
3
| System.out.println("Test " + getName() + "\n");
|
34 |
3
| initCaches();
|
35 |
3
| wakeupIntervalMillis = cache.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
36 |
3
| log("wakeupInterval is " + wakeupIntervalMillis);
|
37 |
3
| if (wakeupIntervalMillis < 0)
|
38 |
| { |
39 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis);
|
40 |
| } |
41 |
| |
42 |
3
| t1_ex = t2_ex = null;
|
43 |
3
| isTrue = true;
|
44 |
| } |
45 |
| |
46 |
3
| public void tearDown() throws Exception
|
47 |
| { |
48 |
3
| super.tearDown();
|
49 |
3
| cache.stop();
|
50 |
| } |
51 |
| |
52 |
3
| private void initCaches() throws Exception
|
53 |
| { |
54 |
3
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/local-mru-eviction-service.xml", false);
|
55 |
3
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
56 |
3
| cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
|
57 |
3
| cache.start();
|
58 |
| } |
59 |
| |
60 |
1
| public void testEviction() throws Exception
|
61 |
| { |
62 |
1
| cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a");
|
63 |
1
| cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b");
|
64 |
1
| cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c");
|
65 |
1
| cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d");
|
66 |
1
| cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e");
|
67 |
| |
68 |
1
| TestingUtil.sleepThread(wakeupIntervalMillis + 500);
|
69 |
| |
70 |
1
| System.out.println(cache.toString(true));
|
71 |
| |
72 |
1
| cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f");
|
73 |
1
| cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g");
|
74 |
1
| cache.put("/org/jboss/test/data/h", "/org/jboss/test/data/h", "/org/jboss/test/data/h");
|
75 |
1
| assertNotNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a"));
|
76 |
1
| assertNotNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b"));
|
77 |
| |
78 |
1
| TestingUtil.sleepThread(wakeupIntervalMillis + 500);
|
79 |
| |
80 |
1
| System.out.println(cache.toString(true));
|
81 |
| |
82 |
1
| assertNull(cache.get("/org/jboss/test/data/a", "/org/jboss/test/data/a"));
|
83 |
1
| assertNull(cache.get("/org/jboss/test/data/b", "/org/jboss/test/data/b"));
|
84 |
| } |
85 |
| |
86 |
1
| public void testNodeRemoved() throws Exception
|
87 |
| { |
88 |
1
| cache.put("/org/jboss/test/data/a", "/org/jboss/test/data/a", "/org/jboss/test/data/a");
|
89 |
1
| cache.put("/org/jboss/test/data/b", "/org/jboss/test/data/b", "/org/jboss/test/data/b");
|
90 |
1
| cache.put("/org/jboss/test/data/c", "/org/jboss/test/data/c", "/org/jboss/test/data/c");
|
91 |
1
| cache.put("/org/jboss/test/data/d", "/org/jboss/test/data/d", "/org/jboss/test/data/d");
|
92 |
1
| cache.put("/org/jboss/test/data/e", "/org/jboss/test/data/e", "/org/jboss/test/data/e");
|
93 |
| |
94 |
1
| TestingUtil.sleepThread(wakeupIntervalMillis + 500);
|
95 |
| |
96 |
1
| cache.remove("/org/jboss/test/data/d");
|
97 |
1
| cache.remove("/org/jboss/test/data/e");
|
98 |
1
| cache.put("/org/jboss/test/data/f", "/org/jboss/test/data/f", "/org/jboss/test/data/f");
|
99 |
1
| cache.put("/org/jboss/test/data/g", "/org/jboss/test/data/g", "/org/jboss/test/data/g");
|
100 |
| |
101 |
1
| TestingUtil.sleepThread(wakeupIntervalMillis + 500);
|
102 |
| |
103 |
1
| assertNull(cache.get("/org/jboss/test/data/d", "/org/jboss/test/data/d"));
|
104 |
1
| assertNull(cache.get("/org/jboss/test/data/e", "/org/jboss/test/data/e"));
|
105 |
| |
106 |
1
| assertNotNull(cache.get("/org/jboss/test/data/f", "/org/jboss/test/data/f"));
|
107 |
1
| assertNotNull(cache.get("/org/jboss/test/data/g", "/org/jboss/test/data/g"));
|
108 |
| } |
109 |
| |
110 |
| class MyPutter extends Thread |
111 |
| { |
112 |
| |
113 |
10
| public MyPutter(String name)
|
114 |
| { |
115 |
10
| super(name);
|
116 |
| } |
117 |
| |
118 |
10
| public void run()
|
119 |
| { |
120 |
10
| int i = 0;
|
121 |
10
| final String myName = ROOT_STR + "/test1/node" + getName();
|
122 |
10
| while (isTrue)
|
123 |
| { |
124 |
12645
| try
|
125 |
| { |
126 |
12645
| cache.put(myName + i++, "value", i);
|
127 |
12645
| TestingUtil.sleepThread(1);
|
128 |
| } |
129 |
| catch (Throwable e) |
130 |
| { |
131 |
0
| e.printStackTrace();
|
132 |
0
| if (t1_ex == null)
|
133 |
| { |
134 |
0
| t1_ex = e;
|
135 |
| } |
136 |
| } |
137 |
| } |
138 |
| } |
139 |
| } |
140 |
| |
141 |
| |
142 |
1
| public void testConcurrentPutAndEvict() throws Exception
|
143 |
| { |
144 |
1
| cache.stop();
|
145 |
1
| cache.destroy();
|
146 |
1
| cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
|
147 |
1
| cache.start();
|
148 |
1
| cache.put(ROOT_STR + "/concurrentPutAndEvict", "value", 1);
|
149 |
| |
150 |
1
| for (int i = 0; i < 10; i++)
|
151 |
| { |
152 |
10
| new MyPutter("Putter" + i).start();
|
153 |
| } |
154 |
| |
155 |
1
| int counter = 0;
|
156 |
1
| while (true)
|
157 |
| { |
158 |
11
| counter++;
|
159 |
11
| if (t1_ex != null)
|
160 |
| { |
161 |
0
| fail("Exception generated in put() " + t1_ex);
|
162 |
| } |
163 |
11
| log("nodes/locks: " + cache.getNumberOfNodes() + "/" + cache.getNumberOfLocksHeld());
|
164 |
11
| TestingUtil.sleepThread(1000);
|
165 |
11
| if (counter > 10)
|
166 |
| { |
167 |
1
| isTrue = false;
|
168 |
1
| break;
|
169 |
| } |
170 |
| } |
171 |
| } |
172 |
| |
173 |
14
| private void log(String s)
|
174 |
| { |
175 |
14
| System.out.println(s);
|
176 |
| } |
177 |
| } |