Skip to content
Permalink
Browse files
GERONIMO-5289 add some heuristic tests, and fix everCommit test
  • Loading branch information
djencks committed May 7, 2010
1 parent 5503987 commit 940cceb4814e442e3ab89b9b8a8d16be6bc3b813
Showing 10 changed files with 163 additions and 26 deletions.
@@ -68,6 +68,7 @@ public void run() {
try {
try {
manager.getCommitter().commit(manager.getBranchId(), false);
evercommit = true;
i.remove();
} catch (XAException e) {
log.error("Unexpected exception committing " + manager.getCommitter() + "; continuing to commit other RMs", e);
@@ -757,7 +757,7 @@ private void commitResources(List<TransactionBranch> rms) throws HeuristicRollba
XAException cause = commitTask.getCause();
boolean evercommit = commitTask.isEvercommit();
if (cause != null) {
if (cause.errorCode == XAException.XA_HEURRB&& !evercommit) {
if (cause.errorCode == XAException.XA_HEURRB && !evercommit) {
throw (HeuristicRollbackException) new HeuristicRollbackException("Error during two phase commit").initCause(cause);
} else if (cause.errorCode == XAException.XA_HEURRB && evercommit) {
throw (HeuristicMixedException) new HeuristicMixedException("Error during two phase commit").initCause(cause);
@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


package org.apache.geronimo.transaction.manager;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.xa.XAException;
import junit.framework.TestCase;

/**
* Test all combinations of heuristic error codes except XA_HEURHAZ
* @version $Rev$ $Date$
*/
public class HeuristicTest extends TestCase {

TransactionLog transactionLog = new MockLog();

TransactionManagerImpl tm;

protected void setUp() throws Exception {
tm = new TransactionManagerImpl(10,
new XidFactoryImpl("WHAT DO WE CALL IT?".getBytes()), transactionLog);
}

protected void tearDown() throws Exception {
tm = null;
}

public void test_C_C() throws Exception {
expectSuccess(0,0);
}
public void test_C_HC() throws Exception {
expectSuccess(0,XAException.XA_HEURCOM);
}
public void test_C_HM() throws Exception {
expectHeuristicMixedException(0, XAException.XA_HEURMIX);
}
public void test_C_HR() throws Exception {
expectHeuristicMixedException(0, XAException.XA_HEURRB);
}

public void test_HC_C() throws Exception {
expectSuccess(XAException.XA_HEURCOM,0);
}
public void test_HC_HC() throws Exception {
expectSuccess(XAException.XA_HEURCOM, XAException.XA_HEURCOM);
}
public void test_HC_HM() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURCOM, XAException.XA_HEURMIX);
}
public void test_HC_HR() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURCOM, XAException.XA_HEURRB);
}

public void test_HM_C() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURMIX, 0);
}
public void test_HM_HC() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURMIX, XAException.XA_HEURCOM);
}
public void test_HM_HM() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURMIX, XAException.XA_HEURMIX);
}
public void test_HM_HR() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURMIX, XAException.XA_HEURRB);
}


public void test_HR_C() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURRB, 0);
}
public void test_HR_HC() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURRB, XAException.XA_HEURCOM);
}
public void test_HR_HM() throws Exception {
expectHeuristicMixedException(XAException.XA_HEURRB, XAException.XA_HEURMIX);
}
public void test_HR_HR() throws Exception {
expectHeuristicRollbackException(XAException.XA_HEURRB, XAException.XA_HEURRB);
}


public void expectSuccess(int first, int second) throws Exception {
tm.begin();
tm.getTransaction().enlistResource(new MockResource("1", first));
tm.getTransaction().enlistResource(new MockResource("2", second));
tm.commit();
}
public void expectHeuristicMixedException(int first, int second) throws Exception {
tm.begin();
tm.getTransaction().enlistResource(new MockResource("1", first));
tm.getTransaction().enlistResource(new MockResource("2", second));
try {
tm.commit();
fail();
} catch (HeuristicMixedException e) {
//expected
}
}
public void expectHeuristicRollbackException(int first, int second) throws Exception {
tm.begin();
tm.getTransaction().enlistResource(new MockResource("1", first));
tm.getTransaction().enlistResource(new MockResource("2", second));
try {
tm.commit();
fail();
} catch (HeuristicRollbackException e) {
//expected
}
}

}
@@ -36,15 +36,22 @@ public class MockResource implements NamedXAResource, NamedXAResourceFactory {
private boolean prepared;
private boolean committed;
private boolean rolledback;
private Set preparedXids = new HashSet();
private Set knownXids = new HashSet();
private Set finishedXids = new HashSet();//end was called with TMSUCCESS or TMFAIL
private Set<Xid> preparedXids = new HashSet<Xid>();
private Set<Xid> knownXids = new HashSet<Xid>();
private Set<Xid> finishedXids = new HashSet<Xid>();//end was called with TMSUCCESS or TMFAIL
private int errorStatus;

public MockResource(MockResourceManager manager, String xaResourceName) {
this.manager = manager;
this.xaResourceName = xaResourceName;
}

public MockResource(String xaResourceName, int errorStatus) {
this.manager = new MockResourceManager();
this.xaResourceName = xaResourceName;
this.errorStatus = errorStatus;
}

public int getTransactionTimeout() throws XAException {
return timeout;
}
@@ -110,6 +117,9 @@ public void commit(Xid xid, boolean onePhase) throws XAException {
if (!finishedXids.contains(xid)) {
throw new XAException(XAException.XAER_PROTO);
}
if (errorStatus != 0) {
throw new XAException(errorStatus);
}
preparedXids.remove(xid);
committed = true;
}
@@ -131,11 +141,11 @@ public boolean isSameRM(XAResource xaResource) throws XAException {
}

public void forget(Xid xid) throws XAException {
throw new UnsupportedOperationException();
// throw new UnsupportedOperationException();
}

public Xid[] recover(int flag) throws XAException {
return (Xid[]) preparedXids.toArray(new Xid[preparedXids.size()]);
return preparedXids.toArray(new Xid[preparedXids.size()]);
}

public boolean isPrepared() {
@@ -22,7 +22,6 @@
import java.util.Map;
import java.util.Set;

import javax.transaction.SystemException;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
@@ -33,20 +32,17 @@
* @version $Rev$ $Date$
*/
public class MockResourceManager {
private boolean willCommit;
private Map xids = new HashMap();
private Map<Xid, Set<XAResource>> xids = new HashMap<Xid, Set<XAResource>>();

public MockResourceManager(boolean willCommit) {
this.willCommit = willCommit;
public MockResourceManager() {
}

public MockResource getResource(String xaResourceName) {
MockResource mockResource = new MockResource(this, xaResourceName);
return mockResource;
return new MockResource(this, xaResourceName);
}

public void join(Xid xid, XAResource xaRes) throws XAException {
Set resSet = (Set) xids.get(xid);
Set<XAResource> resSet = xids.get(xid);
if (resSet == null) {
throw new XAException(XAException.XAER_NOTA);
}
@@ -57,7 +53,7 @@ public void newTx(Xid xid, XAResource xaRes) throws XAException {
if (xids.containsKey(xid)) {
throw new XAException(XAException.XAER_DUPID);
}
Set resSet = new HashSet();
Set<XAResource> resSet = new HashSet<XAResource>();
resSet.add(xaRes);
xids.put(xid, resSet);
}
@@ -32,8 +32,8 @@ public class ProtocolTest extends TestCase {

protected void setUp() throws Exception {
tm = new TransactionManagerImpl();
mrm1 = new MockResourceManager(true);
mrm2 = new MockResourceManager(true);
mrm1 = new MockResourceManager();
mrm2 = new MockResourceManager();
mr11 = new MockResource(mrm1, "mr11");
mr12 = new MockResource(mrm1, "mr12");
mr21 = new MockResource(mrm2, "mr21");
@@ -33,10 +33,10 @@
*/
public class TransactionManagerImplTest extends TestCase {

MockResourceManager rm1 = new MockResourceManager(true);
MockResourceManager rm1 = new MockResourceManager();
MockResource r1_1 = rm1.getResource("rm1_1");
MockResource r1_2 = rm1.getResource("rm1_2");
MockResourceManager rm2 = new MockResourceManager(true);
MockResourceManager rm2 = new MockResourceManager();
MockResource r2_1 = rm2.getResource("rm2_1");
MockResource r2_2 = rm2.getResource("rm2_2");

@@ -29,7 +29,7 @@
*
* @version $Rev$ $Date$
*/
public class TestTransactionManager extends TestCase {
public class TransactionManagerTest extends TestCase {
TransactionManager tm;
MockResourceManager rm1, rm2, rm3;

@@ -113,8 +113,8 @@ public void testOneResource() throws Exception {

protected void setUp() throws Exception {
tm = new TransactionManagerImpl();
rm1 = new MockResourceManager(true);
rm2 = new MockResourceManager(true);
rm3 = new MockResourceManager(false);
rm1 = new MockResourceManager();
rm2 = new MockResourceManager();
rm3 = new MockResourceManager();
}
}
@@ -33,10 +33,10 @@
* */
public class XidImporterTest extends TestCase{

MockResourceManager rm1 = new MockResourceManager(true);
MockResourceManager rm1 = new MockResourceManager();
MockResource r1_1 = new MockResource(rm1, "rm1");
MockResource r1_2 = new MockResource(rm1, "rm1");
MockResourceManager rm2 = new MockResourceManager(true);
MockResourceManager rm2 = new MockResourceManager();
MockResource r2_1 = new MockResource(rm2, "rm2");
MockResource r2_2 = new MockResource(rm2, "rm2");

@@ -1,5 +1,5 @@
Apache Geronimo
Copyright 2003-2009 The Apache Software Foundation
Copyright 2003-2010 The Apache Software Foundation

This product includes software developed by
The Apache Software Foundation (http://www.apache.org/).

0 comments on commit 940cceb

Please sign in to comment.