1 |
| package org.jboss.cache.transaction; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.apache.commons.logging.Log; |
7 |
| import org.apache.commons.logging.LogFactory; |
8 |
| import org.jboss.cache.CacheImpl; |
9 |
| import org.jboss.cache.DefaultCacheFactory; |
10 |
| import org.jboss.cache.Fqn; |
11 |
| import org.jboss.cache.config.Configuration; |
12 |
| import org.jboss.cache.config.Configuration.CacheMode; |
13 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
14 |
| |
15 |
| import javax.naming.Context; |
16 |
| import javax.naming.InitialContext; |
17 |
| import javax.transaction.UserTransaction; |
18 |
| import java.util.Properties; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| public class ReplicatedTransactionDeadlockTest extends TestCase |
65 |
| { |
66 |
| |
67 |
| |
68 |
| private static final int NUM_WORKERS = 2; |
69 |
| |
70 |
| |
71 |
| private static final int NUM_RUNS = 100; |
72 |
| |
73 |
| |
74 |
| private static final long LOCK_ACQUISITION_TIMEOUT = 10000; |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| private static final Properties PROPERTIES; |
80 |
| |
81 |
| |
82 |
| |
83 |
| private static final String CONTEXT_FACTORY = |
84 |
| "org.jboss.cache.transaction.DummyContextFactory"; |
85 |
| |
86 |
| |
87 |
| |
88 |
| private String m_contextFactory = null; |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| private static volatile Exception exception = null; |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| private CacheImpl srcCache = null; |
99 |
| |
100 |
| |
101 |
| |
102 |
| private CacheImpl dstCache = null; |
103 |
| |
104 |
| private Log log = LogFactory.getLog(ReplicatedTransactionDeadlockTest.class); |
105 |
| |
106 |
| static |
107 |
| { |
108 |
1
| PROPERTIES = new Properties();
|
109 |
1
| PROPERTIES.put(Context.INITIAL_CONTEXT_FACTORY,
|
110 |
| "org.jboss.cache.transaction.DummyContextFactory"); |
111 |
| } |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
1
| public ReplicatedTransactionDeadlockTest(String name)
|
119 |
| { |
120 |
1
| super(name);
|
121 |
| } |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
1
| protected void setUp() throws Exception
|
127 |
| { |
128 |
1
| super.setUp();
|
129 |
1
| exception = null;
|
130 |
1
| m_contextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
131 |
1
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, CONTEXT_FACTORY);
|
132 |
1
| DummyTransactionManager.getInstance();
|
133 |
| |
134 |
1
| srcCache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
135 |
1
| srcCache.setConfiguration(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
|
136 |
1
| srcCache.getConfiguration().setTransactionManagerLookupClass(
|
137 |
| "org.jboss.cache.DummyTransactionManagerLookup"); |
138 |
1
| srcCache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
139 |
| |
140 |
1
| srcCache.getConfiguration().setSyncCommitPhase(true);
|
141 |
1
| srcCache.getConfiguration().setLockAcquisitionTimeout(LOCK_ACQUISITION_TIMEOUT);
|
142 |
1
| srcCache.create();
|
143 |
1
| srcCache.start();
|
144 |
| |
145 |
1
| dstCache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
146 |
1
| dstCache.setConfiguration(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
|
147 |
1
| dstCache.getConfiguration().setTransactionManagerLookupClass(
|
148 |
| "org.jboss.cache.DummyTransactionManagerLookup"); |
149 |
1
| dstCache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
150 |
| |
151 |
1
| dstCache.getConfiguration().setSyncCommitPhase(true);
|
152 |
1
| dstCache.getConfiguration().setLockAcquisitionTimeout(LOCK_ACQUISITION_TIMEOUT);
|
153 |
1
| dstCache.create();
|
154 |
1
| dstCache.start();
|
155 |
| } |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
1
| protected void tearDown() throws Exception
|
161 |
| { |
162 |
1
| super.tearDown();
|
163 |
1
| DummyTransactionManager.destroy();
|
164 |
1
| srcCache.stop();
|
165 |
1
| srcCache = null;
|
166 |
1
| dstCache.stop();
|
167 |
1
| dstCache = null;
|
168 |
1
| if (m_contextFactory != null)
|
169 |
| { |
170 |
0
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
|
171 |
| m_contextFactory); |
172 |
0
| m_contextFactory = null;
|
173 |
| } |
174 |
| } |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
1
| public void testConcurrentReplicatedTransaction() throws Exception
|
184 |
| { |
185 |
1
| performTest();
|
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
1
| private void performTest() throws Exception
|
194 |
| { |
195 |
| |
196 |
1
| for (int i = 0; i < NUM_RUNS; i++)
|
197 |
| { |
198 |
100
| if (exception != null)
|
199 |
| { |
200 |
| |
201 |
0
| fail("Due to an exception: " + exception);
|
202 |
| } |
203 |
| |
204 |
100
| Worker[] t = new Worker[NUM_WORKERS];
|
205 |
100
| for (int j = 0; j < t.length; j++)
|
206 |
| { |
207 |
200
| t[j] = new Worker("worker " + i + ":" + j);
|
208 |
200
| t[j].start();
|
209 |
| } |
210 |
| |
211 |
200
| for (Worker aT : t) aT.join();
|
212 |
| } |
213 |
| } |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
200
| private UserTransaction getTransaction() throws Exception
|
222 |
| { |
223 |
200
| return (UserTransaction) new InitialContext(PROPERTIES)
|
224 |
| .lookup("UserTransaction"); |
225 |
| } |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| private class Worker extends Thread |
235 |
| { |
236 |
| |
237 |
| |
238 |
| |
239 |
200
| public Worker(String name)
|
240 |
| { |
241 |
200
| super(name);
|
242 |
| } |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
200
| public void run()
|
248 |
| { |
249 |
200
| try
|
250 |
| { |
251 |
200
| UserTransaction tx = getTransaction();
|
252 |
200
| log.warn("begin");
|
253 |
200
| tx.begin();
|
254 |
200
| log.warn("put");
|
255 |
200
| srcCache.put(new Fqn("Node"), Boolean.FALSE, Boolean.TRUE);
|
256 |
200
| log.warn("commit");
|
257 |
200
| tx.commit();
|
258 |
200
| log.warn("leave");
|
259 |
| } |
260 |
| catch (Exception e) |
261 |
| { |
262 |
0
| log.error("caught exception " + e, e);
|
263 |
0
| exception = e;
|
264 |
| } |
265 |
| } |
266 |
| } |
267 |
| |
268 |
1
| public static Test suite()
|
269 |
| { |
270 |
1
| return new TestSuite(ReplicatedTransactionDeadlockTest.class);
|
271 |
| } |
272 |
| |
273 |
0
| public static void main(String[] args) throws Exception
|
274 |
| { |
275 |
0
| junit.textui.TestRunner.run(suite());
|
276 |
| } |
277 |
| } |