KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > local > ConcurrentTransactionalUnitTestCase


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

8 package org.jboss.test.cache.test.local;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.jboss.cache.PropertyConfigurator;
14 import org.jboss.cache.TreeCache;
15 import org.jboss.cache.lock.IsolationLevel;
16 import org.jboss.cache.transaction.DummyTransactionManager;
17 import org.jboss.logging.Logger;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23 import java.util.*;
24
25 /**
26  * Unit test for local TreeCache. Use locking and multiple threads to test
27  * concurrent access to the tree.
28  *
29  * @version $Id: ConcurrentTransactionalUnitTestCase.java,v 1.1.4.1 2004/12/16 03:31:44 bwang00 Exp $
30  */

31 public class ConcurrentTransactionalUnitTestCase extends TestCase
32 {
33    TreeCache cache;
34    private Logger logger_ = Logger.getLogger(ConcurrentTransactionalUnitTestCase.class);
35    static Throwable JavaDoc thread_ex=null;
36    final int NUM=10000;
37    static Properties p = null;
38    String JavaDoc old_factory = null;
39    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
40
41    public ConcurrentTransactionalUnitTestCase(String JavaDoc name)
42    {
43       super(name);
44    }
45
46    public void setUp() throws Exception JavaDoc
47    {
48       super.setUp();
49       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
50       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
51       DummyTransactionManager.getInstance();
52       if (p == null) {
53          p = new Properties();
54          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
55       }
56    }
57
58    private void createCache(IsolationLevel level) throws Exception JavaDoc
59    {
60       cache = new TreeCache();
61       PropertyConfigurator config = new PropertyConfigurator();
62       config.configure(cache, "META-INF/local-service.xml");
63
64       cache.setCacheMode(TreeCache.LOCAL);
65       cache.setIsolationLevel(level);
66       cache.start();
67       cache.put("/a/b/c", null);
68    }
69
70    private UserTransaction JavaDoc getTransaction() {
71       UserTransaction JavaDoc tx = null;
72       try {
73          tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p).lookup("UserTransaction");
74       } catch (NamingException JavaDoc e) {
75          e.printStackTrace();
76       }
77
78       if(tx == null)
79          throw new RuntimeException JavaDoc("Tx is null");
80
81       return tx;
82    }
83
84    public void tearDown() throws Exception JavaDoc
85    {
86       super.tearDown();
87       cache.stopService();
88       thread_ex=null;
89       DummyTransactionManager.destroy();
90       if (old_factory != null) {
91          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
92          old_factory = null;
93       }
94    }
95
96    public void testConcurrentAccessWithRWLock() throws Throwable JavaDoc
97    {
98       createCache(IsolationLevel.REPEATABLE_READ);
99       work_();
100    }
101
102    public void testConcurrentAccessWithExclusiveLock() throws Throwable JavaDoc
103    {
104       createCache(IsolationLevel.SERIALIZABLE);
105       work_();
106    }
107
108    private void work_() throws Throwable JavaDoc {
109       Updater one, two;
110       try {
111          one = new Updater("Thread one");
112          two = new Updater("Thread two");
113          long current = System.currentTimeMillis();
114          one.start();
115          two.start();
116          one.join();
117          two.join();
118          if(thread_ex != null)
119             throw thread_ex;
120
121          long now = System.currentTimeMillis();
122          log("*** Time elapsed: " + (now-current));
123
124          System.out.println("cache content: " + cache.toString());
125          Set keys = cache.getKeys("/a/b/c");
126          System.out.println("number of keys=" + keys.size());
127
128          if(keys.size() != NUM) {
129             scanForNullValues(keys);
130
131             try {
132                System.out.println("size=" + keys.size());
133                List l=new LinkedList(keys);
134                Collections.sort(l);
135                System.out.println("keys: " + l);
136                for(int i=0; i < NUM; i++) {
137                   if(!l.contains(new Integer JavaDoc(i)))
138                      System.out.println("missing: " + i);
139                }
140
141                LinkedList duplicates=new LinkedList();
142                for(Iterator it=l.iterator(); it.hasNext();) {
143                   Integer JavaDoc integer=(Integer JavaDoc)it.next();
144                   if(duplicates.contains(integer)) {
145                      System.out.println(integer + " is a duplicate");
146                   }
147                   else
148                      duplicates.add(integer);
149                }
150             }
151             catch(Exception JavaDoc e1) {
152                e1.printStackTrace();
153             }
154          }
155
156          assertEquals(NUM, keys.size());
157          log("lock info:\n" + cache.printLockInfo());
158
159       } catch (Exception JavaDoc e) {
160          e.printStackTrace();
161          fail(e.toString());
162       }
163    }
164
165    private void scanForNullValues(Set keys) {
166       for(Iterator it=keys.iterator(); it.hasNext();) {
167          Object JavaDoc o=it.next();
168          if(o == null)
169             System.err.println("found a null value in keys");
170       }
171    }
172
173
174    void sleep(long timeout)
175    {
176       try {
177          Thread.sleep(timeout);
178       } catch (InterruptedException JavaDoc e) {
179       }
180    }
181
182    void log(String JavaDoc msg)
183    {
184 // System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
185
logger_.debug(" [" + Thread.currentThread() + "]: " + msg);
186    }
187
188
189    class Updater extends Thread JavaDoc {
190       String JavaDoc name=null;
191       UserTransaction JavaDoc tx;
192
193       Updater(String JavaDoc name) {
194          this.name=name;
195       }
196
197       public void run() {
198          try {
199             log("adding data");
200             tx=getTransaction();
201             for(int i=0; i < NUM; i++) {
202                log("adding data i=" + i);
203                tx.begin();
204                cache.put("/a/b/c", new Integer JavaDoc(i), name);
205                tx.commit();
206                yield();
207             }
208          }
209          catch(Throwable JavaDoc t) {
210             t.printStackTrace();
211             thread_ex=t;
212          }
213       }
214    }
215
216    public static Test suite()
217    {
218       return new TestSuite(ConcurrentTransactionalUnitTestCase.class);
219    }
220
221    public static void main(String JavaDoc[] args)
222    {
223       junit.textui.TestRunner.run(suite());
224    }
225
226
227 }
228
Popular Tags