KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > MRUPolicyTest


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.cache.eviction;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.factories.XmlConfigurationParser;
12 import org.jboss.cache.lock.IsolationLevel;
13 import org.jboss.cache.misc.TestingUtil;
14
15 /**
16  * Unit tests for MRUPolicy.
17  *
18  * @author Daniel Huang (dhuang@jboss.org)
19  * @version $Revision: 1.12 $
20  */

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