1 4 package com.tc.objectserver.gtx; 5 6 import com.tc.net.protocol.tcm.ChannelID; 7 import com.tc.object.gtx.GlobalTransactionID; 8 import com.tc.object.tx.ServerTransactionID; 9 import com.tc.object.tx.TransactionID; 10 import com.tc.objectserver.persistence.api.PersistenceTransaction; 11 import com.tc.objectserver.persistence.impl.TestPersistenceTransactionProvider; 12 import com.tc.objectserver.persistence.impl.TestTransactionStore; 13 import com.tc.util.SequenceValidator; 14 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 18 import junit.framework.TestCase; 19 20 public class GlobalTransactionManagerImplTest extends TestCase { 21 22 private TestTransactionStore transactionStore; 23 private TestPersistenceTransactionProvider ptxp; 24 private ServerGlobalTransactionManager gtxm; 25 26 protected void setUp() throws Exception { 27 super.setUp(); 28 transactionStore = new TestTransactionStore(); 29 ptxp = new TestPersistenceTransactionProvider(); 30 gtxm = new ServerGlobalTransactionManagerImpl(new SequenceValidator(0), transactionStore, ptxp); 31 } 32 33 public void testStartAndCommitApply() throws Exception { 34 ServerTransactionID stxID1 = new ServerTransactionID(new ChannelID(1), new TransactionID(1)); 35 ServerTransactionID stxID2 = new ServerTransactionID(new ChannelID(1), new TransactionID(2)); 36 37 assertTrue(gtxm.needsApply(stxID1)); 38 assertTrue(gtxm.needsApply(stxID2)); 39 40 assertTrue(gtxm.needsApply(stxID1)); 41 assertTrue(gtxm.needsApply(stxID2)); 42 43 gtxm.commit(null, stxID1); 45 gtxm.commit(null, stxID2); 46 47 assertFalse(gtxm.needsApply(stxID1)); 49 assertFalse(gtxm.needsApply(stxID2)); 50 51 try { 53 gtxm.commit(null, stxID1); 54 fail("TransactionCommittedError"); 55 } catch (TransactionCommittedError e) { 56 } 58 59 transactionStore.restart(); 60 } 61 62 public void testReapplyTransactionsAcrossRestart() throws Exception { 63 ChannelID channel1 = new ChannelID(1); 64 TransactionID tx1 = new TransactionID(1); 65 ServerTransactionID stxid = new ServerTransactionID(channel1, tx1); 66 67 GlobalTransactionID gtxid = gtxm.createGlobalTransactionID(stxid); 68 assertGlobalTxWasLoaded(stxid); 69 70 assertTrue(gtxm.needsApply(stxid)); 71 assertGlobalTxWasLoaded(stxid); 72 assertNextGlobalTxNotCalled(); 73 74 assertTrue(gtxm.needsApply(stxid)); 75 assertGlobalTxWasLoaded(stxid); 76 assertNextGlobalTxNotCalled(); 77 78 assertGlobalTxWasNotLoaded(); 80 81 transactionStore.restart(); 83 84 assertTrue(gtxm.needsApply(stxid)); 86 assertGlobalTxWasLoaded(stxid); 87 assertNextGlobalTxNotCalled(); 88 89 GlobalTransactionID gtxid2 = gtxm.createGlobalTransactionID(stxid); 91 assertNotSame(gtxid, gtxid2); 92 assertFalse(gtxid.equals(gtxid2)); 93 assertGlobalTxWasLoaded(stxid); 94 95 gtxm.commit(null, stxid); 96 assertGlobalTxWasLoaded(stxid); 97 assertNextGlobalTXWasCalled(stxid); 98 99 assertNotNull(transactionStore.commitContextQueue.poll(1)); 100 101 assertFalse(gtxm.needsApply(stxid)); 103 assertGlobalTxWasLoaded(stxid); 104 assertNextGlobalTxNotCalled(); 105 106 assertTrue(transactionStore.commitContextQueue.isEmpty()); 108 109 transactionStore.restart(); 111 112 assertFalse(gtxm.needsApply(stxid)); 114 assertGlobalTxWasLoaded(stxid); 115 assertNextGlobalTxNotCalled(); 116 117 GlobalTransactionID gtxid3 = gtxm.createGlobalTransactionID(stxid); 119 assertNotSame(gtxid2, gtxid3); 120 assertFalse(gtxid3.equals(gtxid2)); 121 assertGlobalTxWasLoaded(stxid); 122 123 try { 124 gtxm.commit(null, stxid); 125 fail("Should not be able to commit twice"); 126 } catch (TransactionCommittedError e) { 127 } 129 assertGlobalTxWasLoaded(stxid); 130 assertNextGlobalTxNotCalled(); 131 132 ServerTransactionID stxid2 = new ServerTransactionID(channel1, new TransactionID(2)); 134 assertTrue(gtxm.needsApply(stxid2)); 135 assertGlobalTxWasLoaded(stxid2); 136 137 gtxm.commit(null, stxid2); 138 139 assertFalse(gtxm.needsApply(stxid2)); 140 141 Collection finished = new ArrayList (2); 142 finished.add(stxid); 143 finished.add(stxid2); 144 145 PersistenceTransaction txn = ptxp.newTransaction(); 146 gtxm.completeTransactions(txn,finished); 147 txn.commit(); 148 149 150 assertTrue(gtxm.needsApply(stxid)); 152 assertTrue(gtxm.needsApply(stxid2)); 153 } 154 155 private void assertNextGlobalTXWasCalled(ServerTransactionID stxid) { 156 assertEquals(stxid, transactionStore.nextTransactionIDContextQueue.poll(1)); 157 assertNextGlobalTxNotCalled(); 158 } 159 160 private void assertNextGlobalTxNotCalled() { 161 assertTrue(transactionStore.nextTransactionIDContextQueue.isEmpty()); 162 } 163 164 private void assertGlobalTxWasNotLoaded() { 165 assertTrue(transactionStore.loadContextQueue.isEmpty()); 166 } 167 168 private void assertGlobalTxWasLoaded(ServerTransactionID stxid) { 169 ServerTransactionID stxidAsLoadKey = (ServerTransactionID) transactionStore.loadContextQueue.poll(1); 170 assertNotNull(stxidAsLoadKey); 171 assertEquals(stxid, stxidAsLoadKey); 172 assertGlobalTxWasNotLoaded(); 173 } 174 175 } 176 | Popular Tags |