1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheImpl; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.config.Configuration; |
8 |
| |
9 |
| import javax.transaction.SystemException; |
10 |
| import javax.transaction.TransactionManager; |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| public class AsyncRollbackTxTest extends TestCase |
19 |
| { |
20 |
| private CacheImpl cache; |
21 |
| private TransactionManager tm; |
22 |
| private Fqn fqn = Fqn.fromString("/test"); |
23 |
| |
24 |
| private long sleepTime = 2500; |
25 |
| |
26 |
| private int txTimeout = 2; |
27 |
| |
28 |
8
| protected void setUp() throws Exception
|
29 |
| { |
30 |
8
| Configuration c = new Configuration();
|
31 |
8
| c.setTransactionManagerLookupClass("org.jboss.cache.transaction.AsyncRollbackTransactionManagerLookup");
|
32 |
8
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
|
33 |
8
| tm = cache.getConfiguration().getRuntimeConfig().getTransactionManager();
|
34 |
8
| tm.setTransactionTimeout(txTimeout);
|
35 |
| } |
36 |
| |
37 |
8
| protected void tearDown()
|
38 |
| { |
39 |
8
| try
|
40 |
| { |
41 |
8
| if (tm != null && tm.getTransaction() != null)
|
42 |
| { |
43 |
0
| try
|
44 |
| { |
45 |
0
| tm.rollback();
|
46 |
| } |
47 |
| catch (SystemException e) |
48 |
| { |
49 |
| |
50 |
| } |
51 |
| } |
52 |
| } |
53 |
| catch (SystemException e) |
54 |
| { |
55 |
| |
56 |
| } |
57 |
8
| if (cache != null) cache.stop();
|
58 |
8
| cache = null;
|
59 |
8
| tm = null;
|
60 |
| } |
61 |
| |
62 |
1
| public void testCommitCreationInSameTx() throws Exception
|
63 |
| { |
64 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
65 |
1
| tm.begin();
|
66 |
1
| cache.put(fqn, "k", "v");
|
67 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
68 |
1
| Thread.sleep(sleepTime);
|
69 |
1
| tm.commit();
|
70 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
71 |
| } |
72 |
| |
73 |
1
| public void testRollbackCreationInSameTx() throws Exception
|
74 |
| { |
75 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
76 |
1
| tm.begin();
|
77 |
1
| cache.put(fqn, "k", "v");
|
78 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
79 |
1
| Thread.sleep(sleepTime);
|
80 |
1
| tm.rollback();
|
81 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
82 |
| |
83 |
1
| assertFalse(cache.exists(fqn));
|
84 |
| |
85 |
1
| assertNull(cache.peek(fqn, true));
|
86 |
| } |
87 |
| |
88 |
| |
89 |
4
| private void doTest(boolean commit, boolean writeLock) throws Exception
|
90 |
| { |
91 |
4
| assertEquals(0, cache.getNumberOfLocksHeld());
|
92 |
4
| cache.put(fqn, "k", "v");
|
93 |
4
| assertEquals(0, cache.getNumberOfLocksHeld());
|
94 |
4
| SeparateThread t = new SeparateThread(commit, writeLock);
|
95 |
4
| t.start();
|
96 |
4
| t.join();
|
97 |
4
| if (t.getException() != null)
|
98 |
| { |
99 |
0
| throw t.getException();
|
100 |
| } |
101 |
4
| assertEquals(0, cache.getNumberOfLocksHeld());
|
102 |
4
| assertEquals("v", cache.get(fqn, "k"));
|
103 |
| } |
104 |
| |
105 |
1
| public void testRollbackCreationInDifferentTxReadLock() throws Exception
|
106 |
| { |
107 |
1
| doTest(false, false);
|
108 |
| } |
109 |
| |
110 |
1
| public void testCommitCreationInDifferentTxReadLock() throws Exception
|
111 |
| { |
112 |
1
| doTest(true, false);
|
113 |
| } |
114 |
| |
115 |
1
| public void testRollbackCreationInDifferentTxWriteLock() throws Exception
|
116 |
| { |
117 |
1
| doTest(false, true);
|
118 |
| } |
119 |
| |
120 |
1
| public void testCommitCreationInDifferentTxWriteLock() throws Exception
|
121 |
| { |
122 |
1
| doTest(true, true);
|
123 |
| } |
124 |
| |
125 |
1
| public void testTxTimeoutAndPutAfter() throws Exception
|
126 |
| { |
127 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
128 |
1
| tm.begin();
|
129 |
1
| cache.put(fqn, "k", "v");
|
130 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
131 |
1
| assertNotNull(tm.getTransaction());
|
132 |
1
| Thread.sleep(sleepTime);
|
133 |
1
| tm.rollback();
|
134 |
1
| assertNull(tm.getTransaction());
|
135 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
136 |
| |
137 |
| |
138 |
1
| assertFalse(cache.exists(fqn));
|
139 |
| |
140 |
1
| assertNull(cache.peek(fqn, true));
|
141 |
| |
142 |
| |
143 |
| |
144 |
1
| cache.put(fqn, "k", "v");
|
145 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
146 |
| } |
147 |
| |
148 |
| |
149 |
1
| public void testTxTimeoutAndPutGetAfter() throws Exception
|
150 |
| { |
151 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
152 |
1
| tm.begin();
|
153 |
1
| cache.put(fqn, "k", "v");
|
154 |
1
| assertEquals(2, cache.getNumberOfLocksHeld());
|
155 |
1
| assertNotNull(tm.getTransaction());
|
156 |
1
| Thread.sleep(sleepTime);
|
157 |
1
| tm.rollback();
|
158 |
| |
159 |
1
| assertFalse(cache.exists(fqn));
|
160 |
| |
161 |
1
| assertNull(cache.peek(fqn, true));
|
162 |
1
| assertNull(tm.getTransaction());
|
163 |
1
| assertEquals(0, cache.getNumberOfLocksHeld());
|
164 |
| |
165 |
| |
166 |
| |
167 |
1
| cache.put(fqn, "k", "v");
|
168 |
1
| cache.get(fqn, "k");
|
169 |
| |
170 |
| |
171 |
1
| SeparateThread t = new SeparateThread(false, false);
|
172 |
1
| t.start();
|
173 |
1
| t.join();
|
174 |
1
| if (t.getException() != null)
|
175 |
| { |
176 |
0
| throw t.getException();
|
177 |
| } |
178 |
| } |
179 |
| |
180 |
| |
181 |
| private class SeparateThread extends Thread |
182 |
| { |
183 |
| Exception e = null; |
184 |
| boolean commit, writeLock; |
185 |
| |
186 |
| |
187 |
5
| public SeparateThread(boolean commit, boolean writeLock)
|
188 |
| { |
189 |
5
| this.commit = commit;
|
190 |
5
| this.writeLock = writeLock;
|
191 |
| } |
192 |
| |
193 |
5
| public Exception getException()
|
194 |
| { |
195 |
5
| return e;
|
196 |
| } |
197 |
| |
198 |
5
| public void run()
|
199 |
| { |
200 |
5
| try
|
201 |
| { |
202 |
5
| tm.begin();
|
203 |
5
| if (writeLock)
|
204 |
| { |
205 |
2
| cache.put(fqn, "k", "v2");
|
206 |
| } |
207 |
| else |
208 |
| { |
209 |
3
| cache.get(fqn, "k");
|
210 |
| } |
211 |
| |
212 |
| |
213 |
5
| sleep(2500);
|
214 |
| |
215 |
5
| if (commit)
|
216 |
| { |
217 |
2
| tm.commit();
|
218 |
| } |
219 |
| else |
220 |
| { |
221 |
3
| tm.rollback();
|
222 |
| } |
223 |
| |
224 |
5
| assertEquals(0, cache.getNumberOfLocksHeld());
|
225 |
| |
226 |
| } |
227 |
| catch (Exception e) |
228 |
| { |
229 |
0
| this.e = e;
|
230 |
| } |
231 |
| } |
232 |
| } |
233 |
| |
234 |
| ; |
235 |
| } |
236 |
| |