KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > eviction > LRUPolicyUnitTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.cache.test.eviction;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.PropertyConfigurator;
14 import org.jboss.cache.TreeCache;
15 import org.jboss.cache.Node;
16
17 /**
18  * @author Ben Wang, Feb 11, 2004
19  */

20 public class LRUPolicyUnitTestCase extends TestCase
21 {
22    TreeCache cache_;
23    int wakeupIntervalMillis_ = 0;
24    final String JavaDoc ROOT_STR = "/test";
25    Throwable JavaDoc t1_ex, t2_ex;
26    final long DURATION = 10000;
27    boolean isTrue;
28
29    public LRUPolicyUnitTestCase(String JavaDoc s)
30    {
31       super(s);
32    }
33
34    public void setUp() throws Exception JavaDoc
35    {
36       super.setUp();
37       initCaches();
38       wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() * 1000;
39       log("wakeupInterval is " + wakeupIntervalMillis_);
40       if (wakeupIntervalMillis_ <= 0)
41          fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
42
43       t1_ex = t2_ex = null;
44       isTrue = true;
45    }
46
47    void initCaches() throws Exception JavaDoc
48    {
49 // _sleep(10000);
50
cache_ = new TreeCache();
51       PropertyConfigurator config = new PropertyConfigurator();
52       config.configure(cache_, "META-INF/local-eviction-service.xml"); // read in generic local xml
53
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
54       cache_.startService();
55    }
56
57    public void tearDown() throws Exception JavaDoc
58    {
59       super.tearDown();
60       cache_.stopService();
61    }
62
63    public void testEviction()
64    {
65       String JavaDoc rootStr = "/org/jboss/test/data/";
66       for (int i = 0; i < 10; i++) {
67          String JavaDoc str = rootStr + i;
68          Fqn fqn = Fqn.fromString(str);
69          try {
70             cache_.put(fqn, str, str);
71          } catch (Exception JavaDoc e) {
72             fail("Failed to insert data" + e);
73             e.printStackTrace();
74          }
75       }
76
77       _sleep(2 * wakeupIntervalMillis_);
78       try {
79          String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
80          assertNull("Node should be empty ", val);
81       } catch (Exception JavaDoc e) {
82          e.printStackTrace();
83       }
84    }
85
86    public void testNodeVisited()
87    {
88       String JavaDoc rootStr = "/org/jboss/test/data/";
89       for (int i = 0; i < 10; i++) {
90          String JavaDoc str = rootStr + i;
91          Fqn fqn = Fqn.fromString(str);
92          try {
93             cache_.put(fqn, str, str);
94          } catch (Exception JavaDoc e) {
95             fail("Failed to insert data" + e);
96             e.printStackTrace();
97          }
98       }
99
100       int period = (wakeupIntervalMillis_ / 2 + 1000);
101       log("period is " + period);
102       _sleep(period); // it really depends the eviction thread time.
103
String JavaDoc str = rootStr + "7";
104       Fqn fqn = Fqn.fromString(str);
105       try {
106          cache_.get(fqn, str); // just to keep it fresh
107
_sleep(period); // it really depends the eviction thread time.
108
cache_.get(fqn, str); // just to keep it fresh
109
_sleep(period); // it really depends the eviction thread time.
110
String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "3", rootStr + "3");
111          assertNull("Node should be empty ", val);
112          val = (String JavaDoc) cache_.get(rootStr + "7", rootStr + "7");
113          assertNotNull("Node should not be empty ", val);
114          _sleep(wakeupIntervalMillis_ * 2 + 2000);
115          val = (String JavaDoc) cache_.get(rootStr + "7", rootStr + "7");
116          assertNull("Node should be empty ", val);
117       } catch (Exception JavaDoc e) {
118          e.printStackTrace();
119       }
120    }
121
122    public void testNodeRemoved()
123    {
124       String JavaDoc rootStr = "/org/jboss/test/data/";
125       for (int i = 0; i < 10; i++) {
126          String JavaDoc str = rootStr + i + "/" + i;
127          Fqn fqn = Fqn.fromString(str);
128          try {
129             cache_.put(fqn, str, str);
130          } catch (Exception JavaDoc e) {
131             fail("Failed to insert data" + e);
132             e.printStackTrace();
133          }
134       }
135
136       int period = (wakeupIntervalMillis_ / 2 + 1000);
137       log("period is " + period);
138       _sleep(period); // it really depends the eviction thread time.
139
String JavaDoc str1 = rootStr + "7";
140       Fqn fqn1 = Fqn.fromString(str1);
141       String JavaDoc str2 = rootStr + "7/7";
142       Fqn fqn2 = Fqn.fromString(str2);
143       try {
144          cache_.get(fqn1, str1); // just to keep it fresh
145
cache_.get(fqn2, str2); // just to keep it fresh
146
_sleep(period); // it really depends the eviction thread time.
147
cache_.get(fqn1, str1); // just to keep it fresh
148
cache_.get(fqn2, str2); // just to keep it fresh
149
_sleep(period); // it really depends the eviction thread time.
150
String JavaDoc val = (String JavaDoc) cache_.get(rootStr + "7/7", rootStr + "7/7");
151          assertNotNull("Node should not be empty ", val);
152          cache_.remove(fqn1);
153          _sleep(wakeupIntervalMillis_ * 2 + 2000);
154          val = (String JavaDoc) cache_.get(rootStr + "7/7", rootStr + "7/7");
155          assertNull("Node should be empty ", val);
156       } catch (Exception JavaDoc e) {
157          e.printStackTrace();
158       }
159    }
160
161     /*
162    public void testConcurrentPutAndEvict() throws Exception
163    {
164       cache_.put(ROOT_STR+ "/test1", "value", new Integer(1));
165
166       for (int i = 0; i < 10; i++) {
167          new Thread()
168          {
169             public void run()
170             {
171                while (isTrue) {
172                   int i = (int) (Math.random() * 10000000);
173                   try {
174                      cache_.put(ROOT_STR+ "/test1/node" + i, "value", new Integer(i));
175                   } catch (Exception e) {
176                      e.printStackTrace();
177                      t1_ex = e;
178                   }
179                }
180             }
181          }.start();
182       }
183
184       int counter = 0;
185       while (true) {
186          counter++;
187          Node node = cache_.get(ROOT_STR+ "/test1");
188          int count = node == null || node.getChildren() == null ? 0 : node.getChildren().size();
189          log("Nodes: " + count);
190          Thread.sleep(1000);
191          if(t1_ex != null) {
192             fail("Exception generated in put " +t1_ex);
193          }
194          if(counter > 10) { // run for 10 seconds
195             isTrue = false;
196             Thread.sleep(1000);
197             break;
198          }
199       }
200    }
201     */

202
203    void _sleep(long msecs)
204    {
205       try {
206          Thread.sleep(msecs);
207       } catch (InterruptedException JavaDoc e) {
208          e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
209
}
210    }
211
212    void log(String JavaDoc msg)
213    {
214       System.out.println("-- " + msg);
215    }
216
217    public static Test suite()
218    {
219       return new TestSuite(LRUPolicyUnitTestCase.class);
220    }
221
222    public static void main(String JavaDoc[] args)
223    {
224       junit.textui.TestRunner.run(suite());
225    }
226
227 }
228
Popular Tags