KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > ThreadedOptimisticCreateIfNotExistsInterceptorTest


1 /*
2  * Created on 17-Feb-2005
3  *
4  *
5  *
6  */

7 package org.jboss.cache.optimistic;
8
9 import junit.framework.Test;
10 import junit.framework.TestSuite;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.OptimisticTransactionEntry;
14 import org.jboss.cache.interceptors.Interceptor;
15 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
16 import org.jboss.cache.loader.SamplePojo;
17 import org.jboss.cache.misc.TestingUtil;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.transaction.Transaction JavaDoc;
21 import javax.transaction.TransactionManager JavaDoc;
22
23 /**
24  * @author xenephon
25  */

26 public class ThreadedOptimisticCreateIfNotExistsInterceptorTest extends AbstractOptimisticTestCase
27 {
28
29    /**
30     * @param name
31     */

32    public ThreadedOptimisticCreateIfNotExistsInterceptorTest(String JavaDoc name)
33    {
34       super(name);
35
36    }
37
38    protected synchronized void setTransactionsInInvocationCtx(TransactionManager mgr, CacheImpl cache) throws Exception JavaDoc
39    {
40       cache.getInvocationContext().setTransaction(mgr.getTransaction());
41       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction());
42    }
43
44    protected void resetInvocationCtx(CacheImpl cache)
45    {
46       cache.getInvocationContext().setTransaction(null);
47       cache.getInvocationContext().setGlobalTransaction(null);
48    }
49
50    public void testDifferentTransactions() throws Exception JavaDoc
51    {
52
53       int numThreads = 100;
54       final int minSleep = 0;
55       final int maxSleep = 1000;
56       TestListener listener = new TestListener();
57       final CacheImpl cache = createCacheWithListener(listener);
58
59       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
60       interceptor.setCache(cache);
61       Interceptor dummy = new MockInterceptor();
62       dummy.setCache(cache);
63       interceptor.setNext(dummy);
64
65       cache.setInterceptorChain(interceptor);
66
67       // should just be the root node
68
assertEquals(0, cache.getNumberOfNodes());
69
70       Runnable JavaDoc run = new Runnable JavaDoc()
71       {
72
73          public void run()
74          {
75             try
76             {
77                //start a new transaction in this thread
78
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
79                mgr.begin();
80                setTransactionsInInvocationCtx(mgr, cache);
81                SamplePojo pojo = new SamplePojo(21, "test");
82
83                cache.put("/one", "key1", pojo);
84
85                randomSleep(minSleep, maxSleep);
86
87                cache.put("/one/two", "key2", pojo);
88
89                OptimisticTransactionEntry entry = (OptimisticTransactionEntry) cache.getTransactionTable().get(cache.getCurrentTransaction());
90                assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
91                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/")) != null);
92                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one")) != null);
93                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one/two")) != null);
94                mgr.commit();
95                resetInvocationCtx(cache);
96             }
97             catch (Exception JavaDoc e)
98             {
99                e.printStackTrace();
100             }
101          }
102       };
103       Thread JavaDoc[] threads = new Thread JavaDoc[numThreads];
104       for (int i = 0; i < numThreads; i++)
105       {
106          Thread JavaDoc t = new Thread JavaDoc(run);
107          t.start();
108          threads[i] = t;
109       }
110       for (int i = 0; i < numThreads; i++)
111       {
112          threads[i].join();
113       }
114       cache.stop();
115    }
116
117    public void testDifferentThreadsSameTransaction() throws Exception JavaDoc
118    {
119       int numThreads = 100;
120       final int minSleep = 0;
121       final int maxSleep = 500;
122       TestListener listener = new TestListener();
123       final CacheImpl cache = createCacheWithListener(listener);
124
125       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
126       interceptor.setCache(cache);
127       Interceptor dummy = new MockInterceptor();
128       dummy.setCache(cache);
129       interceptor.setNext(dummy);
130
131       cache.setInterceptorChain(interceptor);
132
133       final DummyTransactionManager mgr = DummyTransactionManager.getInstance();
134       mgr.begin();
135       final Transaction JavaDoc tx = mgr.getTransaction();
136
137       Runnable JavaDoc run = new Runnable JavaDoc()
138       {
139
140          public void run()
141          {
142             try
143             {
144                //start a new transaction in this thread
145

146                mgr.setTransaction(tx);
147                SamplePojo pojo = new SamplePojo(21, "test");
148
149                setTransactionsInInvocationCtx(mgr, cache);
150                cache.put("/one", "key1", pojo);
151                OptimisticTransactionEntry entry = (OptimisticTransactionEntry) cache.getTransactionTable().get(cache.getCurrentTransaction());
152
153                randomSleep(minSleep, maxSleep);
154
155                cache.put("/one/two", "key2", pojo);
156                assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
157                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/")) != null);
158                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one")) != null);
159                assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one/two")) != null);
160             }
161             catch (Exception JavaDoc e)
162             {
163                e.printStackTrace();
164             }
165             finally
166             {
167                resetInvocationCtx(cache);
168             }
169          }
170       };
171       Thread JavaDoc[] threads = new Thread JavaDoc[numThreads];
172       for (int i = 0; i < numThreads; i++)
173       {
174          Thread JavaDoc t = new Thread JavaDoc(run);
175          t.start();
176          threads[i] = t;
177       }
178       for (int i = 0; i < numThreads; i++)
179       {
180          threads[i].join();
181       }
182       mgr.commit();
183
184       TestingUtil.sleepThread((long) 4000);
185       cache.stop();
186    }
187
188    public static Test suite()
189    {
190       return new TestSuite(ThreadedOptimisticCreateIfNotExistsInterceptorTest.class);
191    }
192
193    public static void main(String JavaDoc[] args)
194    {
195       junit.textui.TestRunner.run(suite());
196    }
197 }
198
Popular Tags