KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > eviction > LRUPolicyTest


1 package org.jboss.cache.tests.eviction;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.PropertyConfigurator;
8 import org.jboss.cache.TreeCache;
9 import org.jboss.cache.lock.IsolationLevel;
10
11 /**
12  * @author Ben Wang, Feb 11, 2004
13  */

14 public class LRUPolicyTest extends TestCase
15 {
16    TreeCache cache_;
17    int wakeupIntervalMillis_ = 0;
18    final String JavaDoc ROOT_STR = "/test";
19    Throwable JavaDoc t1_ex, t2_ex;
20    final long DURATION = 10000;
21    boolean isTrue;
22
23    public LRUPolicyTest(String JavaDoc s)
24    {
25       super(s);
26    }
27
28    public void setUp() throws Exception JavaDoc
29    {
30       super.setUp();
31       initCaches();
32       wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000;
33       log("wakeupInterval is " + wakeupIntervalMillis_);
34       if (wakeupIntervalMillis_ < 0)
35          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
36
37       t1_ex = t2_ex = null;
38       isTrue = true;
39    }
40
41    void initCaches() throws Exception JavaDoc
42    {
43       cache_ = new TreeCache();
44       PropertyConfigurator config = new PropertyConfigurator();
45       config.configure(cache_, "META-INF/local-eviction-service.xml"); // read in generic local xml
46
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
47       cache_.setIsolationLevel(IsolationLevel.SERIALIZABLE);
48       cache_.startService();
49    }
50
51    public void tearDown() throws Exception JavaDoc
52    {
53       super.tearDown();
54       cache_.stopService();
55    }
56
57    public void testEviction()
58    {
59       String JavaDoc rootStr = "/org/jboss/test/data/";
60       for (int i = 0; i < 10; i++) {
61          String JavaDoc str = rootStr + i;
62          Fqn fqn = Fqn.fromString(str);
63          try {
64             cache_.put(fqn, str, str);
65          } catch (Exception JavaDoc e) {
66             fail("Failed to insert data" + e);
67             e.printStackTrace();
68          }
69       }
70
71       _sleep(2 * wakeupIntervalMillis_);
72       try {
73          String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
74          assertNull("Node should be empty ", val);
75       } catch (Exception JavaDoc e) {
76          e.printStackTrace();
77       }
78    }
79
80    public void testNodeVisited()
81    {
82       String JavaDoc rootStr = "/org/jboss/test/data/";
83       for (int i = 0; i < 10; i++) {
84          String JavaDoc str = rootStr + i;
85          Fqn fqn = Fqn.fromString(str);
86          try {
87             cache_.put(fqn, str, str);
88          } catch (Exception JavaDoc e) {
89             fail("Failed to insert data" + e);
90             e.printStackTrace();
91          }
92       }
93       System.out.println("cache is:\n" + cache_.toString(true));
94
95       int period = (wakeupIntervalMillis_ / 2 + 1000);
96       log("sleeping for " + period + "ms");
97       _sleep(period); // it really depends the eviction thread time.
98
String JavaDoc str = rootStr + "7";
99       Fqn fqn = Fqn.fromString(str);
100       try {
101          cache_.get(fqn, str); // just to keep it fresh
102
System.out.println("-- sleeping for " + period + "ms");
103          _sleep(period); // it really depends the eviction thread time.
104
cache_.get(fqn, str); // just to keep it fresh
105
System.out.println("-- sleeping for " + period + "ms");
106          _sleep(period); // it really depends the eviction thread time.
107
String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
108          System.out.println("-- val=" + val);
109          assertNull("Node should be empty ", val);
110          val = (String JavaDoc) cache_.get(rootStr + "7", rootStr + "7");
111          System.out.println("-- val=" + val);
112          assertNotNull("Node should not be empty ", val);
113          System.out.println("-- sleeping for " + (wakeupIntervalMillis_ * 2 + 2000) + "ms");
114          _sleep(wakeupIntervalMillis_ * 2 + 2000);
115          val = (String JavaDoc) cache_.get(rootStr + "7", rootStr + "7");
116          System.out.println("-- val=" + val);
117          assertNull("Node should be empty ", val);
118       } catch (Exception JavaDoc e) {
119          e.printStackTrace();
120       }
121    }
122
123    public void testNodeRemoved()
124    {
125       String JavaDoc rootStr = "/org/jboss/test/data/";
126       for (int i = 0; i < 10; i++) {
127          String JavaDoc str = rootStr + i + "/" + i;
128          Fqn fqn = Fqn.fromString(str);
129          try {
130             cache_.put(fqn, str, str);
131          } catch (Exception JavaDoc e) {
132             fail("Failed to insert data" + e);
133             e.printStackTrace();
134          }
135       }
136
137       int period = (wakeupIntervalMillis_ / 2 + 1000);
138       log("period is " + period);
139       _sleep(period); // it really depends the eviction thread time.
140
String JavaDoc str1 = rootStr + "7";
141       Fqn fqn1 = Fqn.fromString(str1);
142       String JavaDoc str2 = rootStr + "7/7";
143       Fqn fqn2 = Fqn.fromString(str2);
144       try {
145          cache_.get(fqn1, str1); // just to keep it fresh
146
cache_.get(fqn2, str2); // just to keep it fresh
147
_sleep(period); // it really depends the eviction thread time.
148
cache_.get(fqn1, str1); // just to keep it fresh
149
cache_.get(fqn2, str2); // just to keep it fresh
150
_sleep(period); // it really depends the eviction thread time.
151
String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "7/7", rootStr + "7/7");
152          assertNotNull("Node should not be empty ", val);
153          cache_.remove(fqn1);
154          _sleep(wakeupIntervalMillis_ * 2 + 2000);
155          val = (String JavaDoc) cache_.get(rootStr + "7/7", rootStr + "7/7");
156          assertNull("Node should be empty ", val);
157       } catch (Exception JavaDoc e) {
158          e.printStackTrace();
159       }
160    }
161
162
163    class MyPutter extends Thread JavaDoc {
164
165       public MyPutter(String JavaDoc name) {
166          super(name);
167       }
168
169       public void run()
170       {
171          int i=0;
172          final String JavaDoc myName=ROOT_STR + "/test1/node" + getName();
173          while (isTrue) {
174             try {
175                cache_.put(myName + i++, "value", new Integer JavaDoc(i));
176                sleep(1);
177             }
178             catch (Exception JavaDoc e) {
179                e.printStackTrace();
180                if(t1_ex != null)
181                   t1_ex = e;
182             }
183          }
184       }
185    }
186
187
188    public void testConcurrentPutAndEvict() throws Exception JavaDoc
189    {
190       cache_.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
191       cache_.put(ROOT_STR+ "/concurrentPutAndEvict", "value", new Integer JavaDoc(1));
192
193       for (int i = 0; i < 10; i++) {
194          new MyPutter("Putter" + i).start();
195       }
196
197       int counter = 0;
198       while (true) {
199          counter++;
200          if(t1_ex != null) {
201             fail("Exception generated in put() " +t1_ex);
202          }
203          log("nodes/locks: " + cache_.getNumberOfNodes() + "/" + cache_.getNumberOfLocksHeld());
204          _sleep(1000);
205          if(counter > 10) { // run for 10 seconds
206
isTrue = false;
207             break;
208          }
209       }
210    }
211
212    void _sleep(long msecs)
213    {
214       try {
215          Thread.sleep(msecs);
216       } catch (InterruptedException JavaDoc e) {
217          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
218
}
219    }
220
221    void log(String JavaDoc msg)
222    {
223       System.out.println("-- " + msg);
224    }
225
226    public static Test suite()
227    {
228       return new TestSuite(LRUPolicyTest.class);
229    }
230
231    public static void main(String JavaDoc[] args)
232    {
233       junit.textui.TestRunner.run(suite());
234    }
235
236 }
237
Popular Tags