1 4 package com.tc.object.tx; 5 6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 7 8 import java.util.Collections ; 9 import java.util.HashSet ; 10 import java.util.LinkedList ; 11 import java.util.List ; 12 import java.util.Set ; 13 14 import junit.framework.TestCase; 15 16 public class TransactionBatchAccountingTest extends TestCase { 17 private TransactionBatchAccounting acct; 18 private Sequence sequence; 19 20 public void setUp() throws Exception { 21 acct = new TransactionBatchAccounting(); 22 sequence = new Sequence(); 23 } 24 25 public void testBasics() throws Exception { 26 final List incompleteBatchIDs = new LinkedList (); 27 final Set completedGlobalTransactionIDs = new HashSet (); 28 Batch batch1 = new Batch(new TxnBatchID(sequence.next())); 30 acct.addBatch(batch1.batchID, batch1.transactionIDs); 31 assertEquals(Collections.EMPTY_SET, acct.addCompletedTransactionIDsTo(new HashSet ())); 33 assertEquals(Collections.EMPTY_LIST, acct.addIncompleteBatchIDsTo(new LinkedList ())); 35 assertEquals(TxnBatchID.NULL_BATCH_ID, acct.getMinIncompleteBatchID()); 37 38 TransactionID txID1 = new TransactionID(sequence.next()); 40 TransactionID txID2 = new TransactionID(sequence.next()); 41 42 Batch batch2 = new Batch(new TxnBatchID(sequence.next())); 43 batch2.addTransactionID(txID1); 44 batch2.addTransactionID(txID2); 45 incompleteBatchIDs.add(batch2.batchID); 46 acct.addBatch(batch2.batchID, batch2.transactionIDs); 47 48 TransactionID txID3 = new TransactionID(sequence.next()); 49 Batch batch3 = new Batch(new TxnBatchID(sequence.next())); 50 batch3.addTransactionID(txID3); 51 incompleteBatchIDs.add(batch3.batchID); 52 acct.addBatch(batch3.batchID, batch3.transactionIDs); 53 54 TransactionID txID4 = new TransactionID(sequence.next()); 55 Batch batch4 = new Batch(new TxnBatchID(sequence.next())); 56 batch4.addTransactionID(txID4); 57 incompleteBatchIDs.add(batch4.batchID); 58 acct.addBatch(batch4.batchID, batch4.transactionIDs); 59 60 assertEquals(Collections.EMPTY_SET, acct.addCompletedTransactionIDsTo(new HashSet ())); 62 assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 64 assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID()); 65 66 assertEquals(TxnBatchID.NULL_BATCH_ID, acct.acknowledge(txID1)); 68 completedGlobalTransactionIDs.add(txID1); 69 assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 71 assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 72 73 assertEquals(batch2.batchID, acct.acknowledge(txID2)); 76 incompleteBatchIDs.remove(batch2.batchID); 77 assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 78 assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID()); 79 assertEquals(batch2.transactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 80 assertEquals(batch2.transactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 81 82 acct.clearCompletedTransactionIds(); 84 completedGlobalTransactionIDs.clear(); 85 assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 86 87 assertEquals(batch3.batchID, acct.acknowledge(txID3)); 89 completedGlobalTransactionIDs.addAll(batch3.transactionIDs); 90 incompleteBatchIDs.remove(batch3.batchID); 91 assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 92 assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID()); 93 assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 94 95 assertEquals(batch4.batchID, acct.acknowledge(txID4)); 97 completedGlobalTransactionIDs.addAll(batch4.transactionIDs); 98 incompleteBatchIDs.remove(batch4.batchID); 99 assertEquals(Collections.EMPTY_LIST, incompleteBatchIDs); 100 assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 101 assertEquals(TxnBatchID.NULL_BATCH_ID, acct.getMinIncompleteBatchID()); 102 assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 103 104 acct.clearCompletedTransactionIds(); 105 completedGlobalTransactionIDs.clear(); 106 assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet ())); 107 } 108 109 112 public void testBatchOrdering() throws Exception { 113 List batchIDs = new LinkedList (); 114 for (int i = 0; i < 1000; i++) { 115 TxnBatchID batchID = new TxnBatchID(sequence.next()); 116 Set transactionIDs = new HashSet (); 117 for (int j = 0; j < 10; j++) { 118 transactionIDs.add(new TransactionID(sequence.next())); 119 } 120 acct.addBatch(batchID, transactionIDs); 121 batchIDs.add(batchID); 122 123 assertEquals(batchIDs, acct.addIncompleteBatchIDsTo(new LinkedList ())); 124 } 125 } 126 127 private static final class Sequence { 128 private SynchronizedLong sequence = new SynchronizedLong(0); 129 130 public long next() { 131 return sequence.increment(); 132 } 133 } 134 135 private static final class Batch { 136 137 private final TxnBatchID batchID; 138 private final Set transactionIDs = new HashSet (); 139 140 public Batch(TxnBatchID batchID) { 141 this.batchID = batchID; 142 } 143 144 public void addTransactionID(TransactionID txID) { 145 transactionIDs.add(txID); 146 } 147 } 148 149 } 150 | Popular Tags |