KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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

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

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

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

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