-
Notifications
You must be signed in to change notification settings - Fork 172
/
XARecoveryModuleUnitTest.java
400 lines (305 loc) · 13.5 KB
/
XARecoveryModuleUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* JBoss, Home of Professional Open Source
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 2005-2006,
* @author JBoss Inc.
*/
/*
* Copyright (C) 2004,
*
* Arjuna Technologies Ltd,
* Newcastle upon Tyne,
* Tyne and Wear,
* UK.
*
* $Id: xidcheck.java 2342 2006-03-30 13:06:17Z $
*/
package com.hp.mwtests.ts.jta.recovery;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import org.junit.Test;
import com.arjuna.ats.arjuna.common.Uid;
import com.arjuna.ats.arjuna.coordinator.ActionStatus;
import com.arjuna.ats.arjuna.coordinator.AddOutcome;
import com.arjuna.ats.arjuna.coordinator.BasicAction;
import com.arjuna.ats.arjuna.coordinator.RecordType;
import com.arjuna.ats.arjuna.coordinator.TwoPhaseOutcome;
import com.arjuna.ats.arjuna.coordinator.abstractrecord.RecordTypeManager;
import com.arjuna.ats.arjuna.coordinator.abstractrecord.RecordTypeMap;
import com.arjuna.ats.arjuna.recovery.RecoverAtomicAction;
import com.arjuna.ats.arjuna.recovery.RecoveryManager;
import com.arjuna.ats.internal.jta.recovery.arjunacore.NodeNameXAResourceOrphanFilter;
import com.arjuna.ats.internal.jta.recovery.arjunacore.RecoveryXids;
import com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule;
import com.arjuna.ats.internal.jta.resources.arjunacore.XAResourceRecord;
import com.arjuna.ats.internal.jta.transaction.arjunacore.AtomicAction;
import com.arjuna.ats.internal.jta.transaction.arjunacore.subordinate.jca.TransactionImple;
import com.arjuna.ats.jta.common.jtaPropertyManager;
import com.arjuna.ats.jta.recovery.XAResourceOrphanFilter;
import com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper;
import com.arjuna.ats.jta.xa.XidImple;
import com.hp.mwtests.ts.jta.common.RecoveryXAResource;
public class XARecoveryModuleUnitTest
{
@Test
public void testNull ()
{
XARecoveryModule xarm = new XARecoveryModule();
xarm.periodicWorkFirstPass();
xarm.periodicWorkSecondPass();
assertNotNull(xarm.id());
}
@Test
public void testRecoverFromMultipleXAResourceRecovery() throws Exception {
// Make sure the file doesn't exist
assertFalse(new File("XARR.txt").exists());
ArrayList<String> r = new ArrayList<String>();
AtomicAction aa = new AtomicAction();
aa.begin();
assertEquals(AddOutcome.AR_ADDED, aa.add(new XAResourceRecord(null, new XARRTestResource(), new XidImple(aa), null)));
Class c = BasicAction.class;
Method method = c.getDeclaredMethod("prepare", boolean.class);
method.setAccessible(true);
int result = (Integer) method.invoke(aa, new Object[] { true });
assertEquals(result, TwoPhaseOutcome.PREPARE_OK);
// Make sure the file exists
assertTrue(new File("XARR.txt").exists());
// AtomicActionRecoveryModule aaRecoveryModule = new AtomicActionRecoveryModule();
// aaRecoveryModule.periodicWorkFirstPass();
// aaRecoveryModule.periodicWorkSecondPass();
RecordTypeManager.manager().add(new RecordTypeMap() {
@SuppressWarnings("unchecked")
public Class getRecordClass ()
{
return XAResourceRecord.class;
}
public int getType ()
{
return RecordType.JTA_RECORD;
}
});
List<String> xarn = new ArrayList<String>();
xarn.add(NodeNameXAResourceOrphanFilter.RECOVER_ALL_NODES);
jtaPropertyManager.getJTAEnvironmentBean().setXaRecoveryNodes(xarn);
XARecoveryModule xaRecoveryModule = new XARecoveryModule();
Field safetyIntervalMillis = RecoveryXids.class.getDeclaredField("safetyIntervalMillis");
safetyIntervalMillis.setAccessible(true);
safetyIntervalMillis.set(null, 0);
xaRecoveryModule.addXAResourceRecoveryHelper(new XARROne());
xaRecoveryModule.addXAResourceRecoveryHelper(new XARRTwo());
xaRecoveryModule.addXAResourceOrphanFilter(new com.arjuna.ats.internal.jta.recovery.arjunacore.JTATransactionLogXAResourceOrphanFilter());
xaRecoveryModule.addXAResourceOrphanFilter(new com.arjuna.ats.internal.jta.recovery.arjunacore.JTANodeNameXAResourceOrphanFilter());
RecoveryManager.manager().addModule(xaRecoveryModule);
// This is done rather than using the AtomicActionRecoveryModule as the transaction is inflight
RecoverAtomicAction rcvAtomicAction = new RecoverAtomicAction(aa.get_uid(), ActionStatus.COMMITTED);
rcvAtomicAction.replayPhase2();
// The XARM would execute next
xaRecoveryModule.periodicWorkFirstPass();
xaRecoveryModule.periodicWorkSecondPass();
// Make sure the file doesn't exist
assertFalse(new File("XARR.txt").exists());
aa.abort();
}
@Test
public void testRecover () throws Exception
{
ArrayList<String> r = new ArrayList<String>();
TransactionImple tx = new TransactionImple(0);
assertTrue(tx.enlistResource(new RecoveryXAResource()));
assertEquals(tx.doPrepare(), TwoPhaseOutcome.PREPARE_OK);
r.add("com.hp.mwtests.ts.jta.recovery.DummyXARecoveryResource");
jtaPropertyManager.getJTAEnvironmentBean().setXaResourceRecoveryClassNames(r);
XARecoveryModule xarm = new XARecoveryModule();
assertNull(xarm.getNewXAResource( new XAResourceRecord(null, null, new XidImple(), null) ));
for (int i = 0; i < 11; i++)
{
xarm.periodicWorkFirstPass();
xarm.periodicWorkSecondPass();
}
assertTrue(xarm.getNewXAResource( new XAResourceRecord(null, null, new XidImple(new Uid()), null) ) == null);
assertNull(xarm.getNewXAResource( new XAResourceRecord(null, null, new XidImple(), null) ));
}
@Test
public void testFailures () throws Exception
{
XARecoveryModule xarm = new XARecoveryModule();
Class[] parameterTypes = new Class[2];
Uid u = new Uid();
Xid x = new XidImple();
parameterTypes[0] = Xid.class;
parameterTypes[1] = Uid.class;
Method m = xarm.getClass().getDeclaredMethod("addFailure", parameterTypes);
m.setAccessible(true);
Object[] parameters = new Object[2];
parameters[0] = x;
parameters[1] = u;
m.invoke(xarm, parameters);
parameterTypes = new Class[1];
parameterTypes[0] = Xid.class;
parameters = new Object[1];
parameters[0] = x;
m = xarm.getClass().getDeclaredMethod("previousFailure", parameterTypes);
m.setAccessible(true);
Uid ret = (Uid) m.invoke(xarm, parameters);
assertEquals(ret, u);
parameterTypes = new Class[2];
parameterTypes[0] = Xid.class;
parameterTypes[1] = Uid.class;
parameters = new Object[2];
parameters[0] = x;
parameters[1] = u;
m = xarm.getClass().getDeclaredMethod("removeFailure", parameterTypes);
m.setAccessible(true);
m.invoke(xarm, parameters);
m = xarm.getClass().getDeclaredMethod("clearAllFailures", (Class[]) null);
m.setAccessible(true);
m.invoke(xarm, (Object[]) null);
}
@Test
public void testXAResourceRecoveryHelperRegistration() {
XARecoveryModule xaRecoveryModule = new XARecoveryModule();
XAResourceRecoveryHelper xaResourceRecoveryHelper = new DummyXAResourceRecoveryHelper();
xaRecoveryModule.addXAResourceRecoveryHelper(xaResourceRecoveryHelper);
xaRecoveryModule.removeXAResourceRecoveryHelper(xaResourceRecoveryHelper);
}
class DummyXAResourceRecoveryHelper implements XAResourceRecoveryHelper {
@Override
public boolean initialise(String p) throws Exception
{
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public XAResource[] getXAResources() throws Exception
{
return new XAResource[0]; //To change body of implemented methods use File | Settings | File Templates.
}
}
@Test
public void testXAResourceOrphanFilterRegistration() {
XARecoveryModule xaRecoveryModule = new XARecoveryModule();
XAResourceOrphanFilter xaResourceOrphanFilter = new DummyXAResourceOrphanFilter();
xaRecoveryModule.addXAResourceOrphanFilter(xaResourceOrphanFilter);
xaRecoveryModule.removeXAResourceOrphanFilter(xaResourceOrphanFilter);
}
@Test
public void testXAResourceOrphanFilter () throws Exception
{
XAResourceOrphanFilter xaResourceOrphanFilter = new DummyXAResourceOrphanFilter(XAResourceOrphanFilter.Vote.ROLLBACK);
XARecoveryModule xarm = new XARecoveryModule();
xarm.addXAResourceOrphanFilter(xaResourceOrphanFilter);
Class[] parameterTypes = new Class[2];
parameterTypes[0] = XAResource.class;
parameterTypes[1] = Xid.class;
Method m = xarm.getClass().getDeclaredMethod("handleOrphan", parameterTypes);
m.setAccessible(true);
Object[] parameters = new Object[2];
parameters[0] = new RecoveryXAResource();
parameters[1] = new XidImple();
m.invoke(xarm, parameters);
}
@Test
public void testCanRepeatFirstPass () throws Exception
{
XARecoveryModule xarm = new XARecoveryModule();
xarm.addXAResourceRecoveryHelper(new XAResourceRecoveryHelper() {
@Override
public boolean initialise(String p) throws Exception {
return false;
}
@Override
public XAResource[] getXAResources() throws Exception {
return new XAResource[]{new XAResource() {
@Override
public void commit(Xid xid, boolean b) throws XAException {
}
@Override
public void end(Xid xid, int i) throws XAException {
}
@Override
public void forget(Xid xid) throws XAException {
}
@Override
public int getTransactionTimeout() throws XAException {
return 0;
}
@Override
public boolean isSameRM(XAResource xaResource) throws XAException {
return false;
}
@Override
public int prepare(Xid xid) throws XAException {
return 0;
}
@Override
public Xid[] recover(int i) throws XAException {
recoverCalled++;
return new Xid[0];
}
@Override
public void rollback(Xid xid) throws XAException {
}
@Override
public boolean setTransactionTimeout(int i) throws XAException {
return false;
}
@Override
public void start(Xid xid, int i) throws XAException {
}
}};
}
});
xarm.periodicWorkFirstPass();
assertEquals(recoverCalled, 1);
xarm.periodicWorkSecondPass();
assertEquals(recoverCalled, 2);
xarm.periodicWorkFirstPass();
assertEquals(recoverCalled, 3);
xarm.periodicWorkFirstPass();
assertEquals(recoverCalled, 5);
xarm.periodicWorkSecondPass();
assertEquals(recoverCalled, 6);
}
class DummyXAResourceOrphanFilter implements XAResourceOrphanFilter
{
public DummyXAResourceOrphanFilter ()
{
_vote = null;
}
public DummyXAResourceOrphanFilter (Vote v)
{
_vote = v;
}
@Override
public Vote checkXid(Xid xid)
{
return _vote;
}
private Vote _vote;
}
private int recoverCalled;
}