KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > LocalConcurrentTest


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.cache.pojo;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.config.Configuration;
15 import org.jboss.cache.lock.UpgradeException;
16 import org.jboss.cache.pojo.test.Address;
17 import org.jboss.cache.pojo.test.Person;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Properties JavaDoc;
25 import java.util.Random JavaDoc;
26
27
28 /**
29  * Local concurrent test for PojoCache. Test attach and detach under load
30  * and concurrency.
31  *
32  * @version $Revision: 1.6 $
33  * @author<a HREF="mailto:bwang@jboss.org">Ben Wang</a> December 2004
34  */

35 public class LocalConcurrentTest extends TestCase
36 {
37    static PojoCache cache_;
38    Configuration.CacheMode cachingMode_ = Configuration.CacheMode.LOCAL;
39    Properties JavaDoc p_;
40    String JavaDoc oldFactory_ = null;
41    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
42    static ArrayList JavaDoc nodeList_;
43    static final int depth_ = 2;
44    static final int children_ = 2;
45    static final int MAX_LOOP = 100;
46    static final int SLEEP_TIME = 50;
47    static Exception JavaDoc thread_ex = null;
48    UserTransaction JavaDoc tx_ = null;
49
50    public LocalConcurrentTest(String JavaDoc name)
51    {
52       super(name);
53    }
54
55    public void setUp() throws Exception JavaDoc
56    {
57       super.setUp();
58       oldFactory_ = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
59       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
60       DummyTransactionManager.getInstance();
61       if (p_ == null)
62       {
63          p_ = new Properties JavaDoc();
64          p_.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
65       }
66
67       tx_ = (UserTransaction JavaDoc) new InitialContext JavaDoc(p_).lookup("UserTransaction");
68
69       initCaches(Configuration.CacheMode.LOCAL);
70       nodeList_ = nodeGen(depth_, children_);
71
72       log("LocalConcurrentTestCase: cacheMode=TRANSIENT, one cache");
73    }
74
75    public void tearDown() throws Exception JavaDoc
76    {
77       super.tearDown();
78       thread_ex = null;
79       DummyTransactionManager.destroy();
80       destroyCaches();
81
82       if (oldFactory_ != null)
83       {
84          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactory_);
85          oldFactory_ = null;
86       }
87
88    }
89
90    void initCaches(Configuration.CacheMode caching_mode) throws Exception JavaDoc
91    {
92       cachingMode_ = caching_mode;
93       boolean toStart = false;
94       cache_ = PojoCacheFactory.createCache("META-INF/local-service.xml", toStart);
95       cache_.start();
96    }
97
98    void destroyCaches() throws Exception JavaDoc
99    {
100       cache_.stop();
101       cache_ = null;
102    }
103
104    public void testAll_RWLock() throws Exception JavaDoc
105    {
106       try
107       {
108          all();
109       }
110       catch (UpgradeException ue)
111       {
112          log("Upgrade exception. Can ingore for repeatable read. " + ue);
113       }
114       catch (Exception JavaDoc ex)
115       {
116          log("Exception: " + ex);
117          throw ex;
118       }
119    }
120
121    private void all() throws Exception JavaDoc
122    {
123       RunThread t1 = new RunThread(1);
124       RunThread t2 = new RunThread(2);
125       RunThread t3 = new RunThread(3);
126       RunThread t4 = new RunThread(4);
127
128       t1.start();
129       TestingUtil.sleepThread(100);
130       t2.start();
131       TestingUtil.sleepThread(100);
132       t3.start();
133       TestingUtil.sleepThread(100);
134       t4.start();
135
136       t1.join(60000);// wait for 20 secs
137
t2.join(60000);// wait for 20 secs
138
t3.join(60000);// wait for 20 secs
139
t4.join(60000);// wait for 20 secs
140

141       if (thread_ex != null)
142       {
143          throw thread_ex;
144       }
145    }
146
147    class RunThread extends Thread JavaDoc
148    {
149       final int seed_;
150       Random JavaDoc random_;
151       Person person_;
152
153       public RunThread(int seed)
154       {
155          seed_ = seed;
156          random_ = new Random JavaDoc(seed);
157       }
158
159       private void createPerson()
160       {
161          person_ = new Person();
162          person_.setName("Ben");
163          person_.setAge(18);
164          ArrayList JavaDoc<String JavaDoc> lang = new ArrayList JavaDoc<String JavaDoc>();
165          lang.add("English");
166          lang.add("French");
167          lang.add("Mandarin");
168          person_.setLanguages(lang);
169          Address addr = new Address();
170          addr.setZip(95123);
171          addr.setStreet("Almeria");
172          addr.setCity("San Jose");
173          person_.setAddress(addr);
174       }
175
176       public void run()
177       {
178          try
179          {
180             _run();
181          }
182          catch (Exception JavaDoc e)
183          {
184             thread_ex = e;
185          }
186       }
187
188       /**
189        */

190       public void _run() throws Exception JavaDoc
191       {
192          for (int loop = 0; loop < MAX_LOOP; loop++)
193          {
194             createPerson();// create a new person instance every loop.
195
TestingUtil.sleepThread(random_.nextInt(50));
196             op1();
197          }
198       }
199
200       // Operation 1
201
private void op1()
202       {
203          int i = random_.nextInt(nodeList_.size() - 1);
204          if (i == 0) return;// it is meaningless to test root
205
String JavaDoc node = nodeList_.get(i) + "/aop";
206          cache_.attach(node, person_);
207          TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
208
TestingUtil.sleepThread(random_.nextInt(SLEEP_TIME));// sleep for max 200 millis
209
cache_.detach(node);
210       }
211    }
212
213    /**
214     * Generate the tree nodes quasi-exponentially. I.e., depth is the level
215     * of the hierarchy and children is the number of children under each node.
216     * This strucutre is used to add, get, and remove for each node.
217     */

218    private ArrayList JavaDoc nodeGen(int depth, int children)
219    {
220       ArrayList JavaDoc<String JavaDoc> strList = new ArrayList JavaDoc<String JavaDoc>();
221       ArrayList JavaDoc<String JavaDoc> oldList = new ArrayList JavaDoc<String JavaDoc>();
222       ArrayList JavaDoc<String JavaDoc> newList = new ArrayList JavaDoc<String JavaDoc>();
223
224       // Skip root node
225
oldList.add("/");
226       newList.add("/");
227       strList.add("/");
228
229       while (depth > 0)
230       {
231          // Trying to produce node name at this depth.
232
newList = new ArrayList JavaDoc<String JavaDoc>();
233          for (int i = 0; i < oldList.size(); i++)
234          {
235             for (int j = 0; j < children; j++)
236             {
237                String JavaDoc tmp = oldList.get(i);
238                tmp += Integer.toString(j);
239                if (depth != 1)
240                {
241                   tmp += "/";
242                }
243
244                newList.add(tmp);
245             }
246          }
247          strList.addAll(newList);
248          oldList = newList;
249          depth--;
250       }
251
252       // let's prune out root node
253
for (int i = 0; i < strList.size(); i++)
254       {
255          if (strList.get(i).equals("/"))
256          {
257             strList.remove(i);
258             break;
259          }
260       }
261       log("Nodes generated: " + strList.size());
262       return strList;
263    }
264
265    public static Test suite() throws Exception JavaDoc
266    {
267       return new TestSuite(LocalConcurrentTest.class);
268    }
269
270    private static void log(String JavaDoc str)
271    {
272       System.out.println("Thread: " + Thread.currentThread() + ": " + str);
273       // System.out.println(str);
274
}
275
276 }
277
Popular Tags