KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > standAloneAop > LocalConcurrentTestCase


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

8
9 package org.jboss.test.cache.test.standAloneAop;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.CacheException;
15 import org.jboss.cache.PropertyConfigurator;
16 import org.jboss.cache.TreeCache;
17 import org.jboss.cache.aop.TreeCacheAop;
18 import org.jboss.cache.lock.IsolationLevel;
19 import org.jboss.cache.lock.LockStrategyFactory;
20 import org.jboss.cache.transaction.DummyTransactionManager;
21
22 import javax.naming.Context JavaDoc;
23 import javax.transaction.*;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Random JavaDoc;
27
28
29 /**
30  * Local mode stress test for TreeCacheAop. Test getObject, putObject and removeObject under load
31  * and concurrency.
32  *
33  * @version $Revision: 1.1 $
34  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> December 2004
35  */

36 public class LocalConcurrentTestCase extends TestCase
37 {
38    static TreeCacheAop cache_;
39    int cachingMode_ = TreeCache.LOCAL;
40    final static Properties JavaDoc p_;
41    String JavaDoc oldFactory_ = null;
42    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
43    static ArrayList JavaDoc nodeList_;
44    static final int depth_ = 3;
45    static final int children_ = 3;
46    DummyTransactionManager tm_;
47    static final int MAX_LOOP = 100;
48    static final int SLEEP_TIME = 50;
49    static Exception JavaDoc thread_ex=null;
50
51    static
52    {
53       p_ = new Properties JavaDoc();
54       p_.put(Context.INITIAL_CONTEXT_FACTORY,
55             "org.jboss.cache.transaction.DummyContextFactory");
56    }
57
58    public LocalConcurrentTestCase(String JavaDoc name)
59    {
60       super(name);
61    }
62
63    public void setUp() throws Exception JavaDoc
64    {
65       super.setUp();
66
67       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
68       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
69
70       DummyTransactionManager.getInstance();
71       initCaches(TreeCache.LOCAL);
72       nodeList_ = nodeGen(depth_, children_);
73       tm_ = new DummyTransactionManager();
74
75       log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
76    }
77
78    public void tearDown() throws Exception JavaDoc
79    {
80       super.tearDown();
81       thread_ex=null;
82       DummyTransactionManager.destroy();
83       destroyCaches();
84
85       if (oldFactory_ != null) {
86          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
87          oldFactory_ = null;
88       }
89
90    }
91
92    void initCaches(int caching_mode) throws Exception JavaDoc
93    {
94       cachingMode_ = caching_mode;
95       cache_ = new TreeCacheAop();
96       PropertyConfigurator config = new PropertyConfigurator();
97       config.configure(cache_, "META-INF/local-aop-eviction-service.xml"); // read in generic local xml
98
cache_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
99       cache_.createService();
100       cache_.startService();
101    }
102
103    void destroyCaches() throws Exception JavaDoc
104    {
105       cache_.stopService();
106       cache_ = null;
107    }
108
109    protected void setLevelRW()
110    {
111       log("set lock level to RWUpgrade ...");
112       LockStrategyFactory.setIsolationLevel(IsolationLevel.REPEATABLE_READ);
113    }
114
115    protected void setLevelSerial()
116    {
117       log("set lock level to SimpleLock ...");
118       LockStrategyFactory.setIsolationLevel(IsolationLevel.SERIALIZABLE);
119    }
120
121    public void testAll_RWLock() throws Exception JavaDoc
122    {
123       setLevelRW();
124       all();
125    }
126
127    public void XtestAll_SimpleLock() throws Exception JavaDoc
128    {
129       setLevelSerial();
130       all();
131    }
132
133    private void all() throws Exception JavaDoc
134    {
135       RunThread t1 = new RunThread(1);
136       RunThread t2 = new RunThread(2);
137       RunThread t3 = new RunThread(3);
138       RunThread t4 = new RunThread(4);
139
140       t1.start();
141       t2.start();
142       t3.start();
143       t4.start();
144
145       t1.join(60000); // wait for 20 secs
146
t2.join(60000); // wait for 20 secs
147
t3.join(60000); // wait for 20 secs
148
t4.join(60000); // wait for 20 secs
149

150       if(thread_ex != null)
151          throw thread_ex;
152    }
153
154    static class RunThread extends Thread JavaDoc
155    {
156       final int seed_;
157       Random JavaDoc random_;
158       Person person_;
159
160       public RunThread(int seed)
161       {
162          seed_ = seed;
163          random_ = new Random JavaDoc(seed);
164          createPerson();
165       }
166
167       private void createPerson() {
168          person_ = new Person();
169          person_.setName("Ben");
170          person_.setAge(18);
171          ArrayList JavaDoc lang = new ArrayList JavaDoc();
172          lang.add("English");
173          lang.add("French");
174          lang.add("Mandarin");
175          person_.setLanguages(lang);
176          Address addr = new Address();
177          addr.setZip(95123);
178          addr.setStreet("Almeria");
179          addr.setCity("San Jose");
180          person_.setAddress(addr);
181       }
182
183       public void run() {
184          try {
185             _run();
186          }
187          catch(Exception JavaDoc e) {
188             thread_ex=e;
189          }
190       }
191
192       public void _run() throws HeuristicMixedException, SystemException,
193                                 NotSupportedException, HeuristicRollbackException, RollbackException, CacheException {
194          for (int loop = 0; loop < MAX_LOOP; loop++) {
195             sleep_(random_.nextInt(50));
196             log("Executing op1. Loop-" + loop);
197             op1();
198             sleep_(random_.nextInt(50));
199             log("Executing op2...");
200             op2();
201             sleep_(random_.nextInt(50));
202             log("Executing op3...");
203             op3();
204          }
205       }
206
207       // Operation 1
208
private void op1() throws SystemException, NotSupportedException, CacheException,
209                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
210          int i = random_.nextInt(nodeList_.size() - 1);
211
212          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
213          cache_.getObject(node);
214          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
215
cache_.putObject(node, person_);
216          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
217
cache_.removeObject(node);
218       }
219
220       // Operation 2
221
private void op2() throws SystemException, NotSupportedException, CacheException,
222                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
223          int i = random_.nextInt(nodeList_.size() - 1);
224
225          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
226          cache_.getObject(node);
227          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
228
cache_.getObject(node);
229          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
230
}
231
232       // Operation 3
233
private void op3() throws SystemException, NotSupportedException, CacheException,
234                                 HeuristicMixedException, HeuristicRollbackException, RollbackException {
235          int i = random_.nextInt(nodeList_.size() - 1);
236
237          String JavaDoc node = (String JavaDoc) nodeList_.get(i);
238          cache_.putObject(node, person_);
239          sleep_(random_.nextInt(SLEEP_TIME)); // sleep for max 200 millis
240
cache_.removeObject(node);
241       }
242
243       private void sleep_(long msecs)
244       {
245          try {
246             Thread.sleep(msecs);
247          } catch (Exception JavaDoc ex) {
248          }
249       }
250    }
251
252    /**
253     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
254     * of the hierarchy and children is the number of children under each node.
255     * This strucutre is used to add, get, and remove for each node.
256     */

257    private ArrayList JavaDoc nodeGen(int depth, int children)
258    {
259       ArrayList JavaDoc strList = new ArrayList JavaDoc();
260       ArrayList JavaDoc oldList = new ArrayList JavaDoc();
261       ArrayList JavaDoc newList = new ArrayList JavaDoc();
262
263       // Skip root node
264
oldList.add("/");
265       newList.add("/");
266       strList.add("/");
267
268       while (depth > 0) {
269          // Trying to produce node name at this depth.
270
newList = new ArrayList JavaDoc();
271          for (int i = 0; i < oldList.size(); i++) {
272             for (int j = 0; j < children; j++) {
273                String JavaDoc tmp = (String JavaDoc) oldList.get(i);
274                tmp += Integer.toString(j);
275                if (depth != 1) {
276                   tmp += "/";
277                }
278
279                newList.add(tmp);
280             }
281          }
282          strList.addAll(newList);
283          oldList = newList;
284          depth--;
285       }
286
287       // let's prune out root node
288
for(int i=0; i < strList.size(); i++) {
289          if( ((String JavaDoc)strList.get(i)).equals("/") ) {
290             strList.remove(i);
291             break;
292          }
293       }
294       log("Nodes generated: " + strList.size());
295       return strList;
296    }
297
298    public static Test suite() throws Exception JavaDoc
299    {
300       return new TestSuite(LocalConcurrentTestCase.class);
301    }
302
303    private static void log(String JavaDoc str)
304    {
305       System.out.println("Thread: " + Thread.currentThread() + ": " + str);
306 // System.out.println(str);
307
}
308
309 }
310
Popular Tags