KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > transaction > AsyncRollbackTxTest


1 package org.jboss.cache.transaction;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheImpl;
5 import org.jboss.cache.Fqn;
6 import org.jboss.cache.config.Configuration;
7 import org.jboss.cache.factories.DefaultCacheFactory;
8
9 import javax.transaction.SystemException JavaDoc;
10 import javax.transaction.TransactionManager JavaDoc;
11
12 /**
13  * Test behaviour of async rollback timeouted transaction
14  *
15  * @author <a HREF="mailto:jhalat@infovide.pl">Jacek Halat</a>
16  * @since 1.4.0
17  */

18 public class AsyncRollbackTxTest extends TestCase
19 {
20    private CacheImpl cache;
21    private TransactionManager JavaDoc tm;
22    private Fqn fqn = Fqn.fromString("/test");
23    // This sleep time (millis) should be longer than the transaction timeout time.
24
private long sleepTime = 2500;
25    // seconds
26
private int txTimeout = 2;
27
28    protected void setUp() throws Exception JavaDoc
29    {
30       Configuration c = new Configuration();
31       c.setTransactionManagerLookupClass("org.jboss.cache.transaction.AsyncRollbackTransactionManagerLookup");
32       cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
33       tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
34       tm.setTransactionTimeout(txTimeout);
35    }
36
37    protected void tearDown()
38    {
39       try
40       {
41          if (tm != null && tm.getTransaction() != null)
42          {
43             try
44             {
45                tm.rollback();
46             }
47             catch (SystemException JavaDoc e)
48             {
49                // do nothing
50
}
51          }
52       }
53       catch (SystemException JavaDoc e)
54       {
55          // do nothing
56
}
57       if (cache != null) cache.stop();
58       cache = null;
59       tm = null;
60    }
61
62    public void testCommitCreationInSameTx() throws Exception JavaDoc
63    {
64       assertEquals(0, cache.getNumberOfLocksHeld());
65       tm.begin();
66       cache.put(fqn, "k", "v");
67       assertEquals(2, cache.getNumberOfLocksHeld());
68       Thread.sleep(sleepTime);
69       tm.commit();
70       assertEquals(0, cache.getNumberOfLocksHeld());
71    }
72
73    public void testRollbackCreationInSameTx() throws Exception JavaDoc
74    {
75       assertEquals(0, cache.getNumberOfLocksHeld());
76       tm.begin();
77       cache.put(fqn, "k", "v");
78       assertEquals(2, cache.getNumberOfLocksHeld());
79       Thread.sleep(sleepTime);
80       tm.rollback();
81       assertEquals(0, cache.getNumberOfLocksHeld());
82       // make sure the node was NOT added!!
83
assertFalse(cache.exists(fqn));
84       // even in a "deleted" form
85
assertNull(cache.peek(fqn));
86    }
87
88
89    private void doTest(boolean commit, boolean writeLock) throws Exception JavaDoc
90    {
91       assertEquals(0, cache.getNumberOfLocksHeld());
92       cache.put(fqn, "k", "v");//Executed in Not transactional context
93
assertEquals(0, cache.getNumberOfLocksHeld());
94       SeparateThread t = new SeparateThread(commit, writeLock);
95       t.start();
96       t.join();
97       if (t.getException() != null)
98       {
99          throw t.getException();
100       }
101       assertEquals(0, cache.getNumberOfLocksHeld());
102       assertEquals("v", cache.get(fqn, "k"));
103    }
104
105    public void testRollbackCreationInDifferentTxReadLock() throws Exception JavaDoc
106    {
107       doTest(false, false);
108    }
109
110    public void testCommitCreationInDifferentTxReadLock() throws Exception JavaDoc
111    {
112       doTest(true, false);
113    }
114
115    public void testRollbackCreationInDifferentTxWriteLock() throws Exception JavaDoc
116    {
117       doTest(false, true);
118    }
119
120    public void testCommitCreationInDifferentTxWriteLock() throws Exception JavaDoc
121    {
122       doTest(true, true);
123    }
124
125    public void testTxTimeoutAndPutAfter() throws Exception JavaDoc
126    {
127       assertEquals(0, cache.getNumberOfLocksHeld());
128       tm.begin();
129       cache.put(fqn, "k", "v");
130       assertEquals(2, cache.getNumberOfLocksHeld());
131       assertNotNull(tm.getTransaction());
132       Thread.sleep(sleepTime);
133       tm.rollback();
134       assertNull(tm.getTransaction());
135       assertEquals(0, cache.getNumberOfLocksHeld());
136
137       // make sure the node was NOT added!!
138
assertFalse(cache.exists(fqn));
139       // even in a "deleted" form
140
assertNull(cache.peek(fqn));
141
142       //Put not some data into cache. Because no transaction is used
143
//no locks should be helds after this line.
144
cache.put(fqn, "k", "v");
145       assertEquals(0, cache.getNumberOfLocksHeld());
146    }
147
148
149    public void testTxTimeoutAndPutGetAfter() throws Exception JavaDoc
150    {
151       assertEquals(0, cache.getNumberOfLocksHeld());
152       tm.begin();
153       cache.put(fqn, "k", "v");
154       assertEquals(2, cache.getNumberOfLocksHeld());
155       assertNotNull(tm.getTransaction());
156       Thread.sleep(sleepTime);
157       tm.rollback();
158       // make sure the node was NOT added!!
159
assertFalse(cache.exists(fqn));
160       // even in a "deleted" form
161
assertNull(cache.peek(fqn));
162       assertNull(tm.getTransaction());
163       assertEquals(0, cache.getNumberOfLocksHeld());
164
165       //Put not some data into cache. Because no transaction is used
166
//no locks should be helds after this line.
167
cache.put(fqn, "k", "v");
168       cache.get(fqn, "k");
169
170       // Make sure no write lock is retained by the main thread. Test that another thread can read.
171
SeparateThread t = new SeparateThread(false, false);
172       t.start();
173       t.join();
174       if (t.getException() != null)
175       {
176          throw t.getException();
177       }
178    }
179
180
181    private class SeparateThread extends Thread JavaDoc
182    {
183       Exception JavaDoc e = null;
184       boolean commit, writeLock;
185
186
187       public SeparateThread(boolean commit, boolean writeLock)
188       {
189          this.commit = commit;
190          this.writeLock = writeLock;
191       }
192
193       public Exception JavaDoc getException()
194       {
195          return e;
196       }
197
198       public void run()
199       {
200          try
201          {
202             tm.begin();
203             if (writeLock)
204             {
205                cache.put(fqn, "k", "v2");// obtain write lock on node
206
}
207             else
208             {
209                cache.get(fqn, "k");// obtain read lock on node
210
}
211
212             // sleep to ensure tx times out.
213
sleep(2500);
214
215             if (commit)
216             {
217                tm.commit();
218             }
219             else
220             {
221                tm.rollback();
222             }
223
224             assertEquals(0, cache.getNumberOfLocksHeld());
225
226          }
227          catch (Exception JavaDoc e)
228          {
229             this.e = e;
230          }
231       }
232    }
233
234    ;
235 }
236
237
Popular Tags