KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > transaction > ConcurrentTransactionalTest


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.cache.tests.transaction;
9
10 import junit.framework.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.jboss.cache.PropertyConfigurator;
16 import org.jboss.cache.TreeCache;
17 import org.jboss.cache.lock.IsolationLevel;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.transaction.UserTransaction JavaDoc;
24 import java.util.*;
25
26 /**
27  * Unit test for local TreeCache. Use locking and multiple threads to test
28  * concurrent access to the tree.
29  *
30  * @version $Id: ConcurrentTransactionalTest.java,v 1.2 2005/04/05 17:19:44 belaban Exp $
31  */

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