KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > tx > TransactionBatchAccountingTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.tx;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
7
8 import java.util.Collections JavaDoc;
9 import java.util.HashSet JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Set JavaDoc;
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 JavaDoc {
21     acct = new TransactionBatchAccounting();
22     sequence = new Sequence();
23   }
24
25   public void testBasics() throws Exception JavaDoc {
26     final List JavaDoc incompleteBatchIDs = new LinkedList JavaDoc();
27     final Set JavaDoc completedGlobalTransactionIDs = new HashSet JavaDoc();
28     // try adding an empty batch
29
Batch batch1 = new Batch(new TxnBatchID(sequence.next()));
30     acct.addBatch(batch1.batchID, batch1.transactionIDs);
31     // there should be no completed transactions
32
assertEquals(Collections.EMPTY_SET, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
33     // there should be no incomplete batches
34
assertEquals(Collections.EMPTY_LIST, acct.addIncompleteBatchIDsTo(new LinkedList JavaDoc()));
35     // the min incomplete batch id should be the null id.
36
assertEquals(TxnBatchID.NULL_BATCH_ID, acct.getMinIncompleteBatchID());
37
38     // try adding a batch with a two transactions
39
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     // there should be no completed transactions, since there were no acknowledgements
61
assertEquals(Collections.EMPTY_SET, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
62     // check the incomplete batches
63
assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList JavaDoc()));
64     assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID());
65
66     // ACK the first transaction in the multi-transaction batch
67
assertEquals(TxnBatchID.NULL_BATCH_ID, acct.acknowledge(txID1));
68     completedGlobalTransactionIDs.add(txID1);
69     // there should still be no completed batches
70
assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList JavaDoc()));
71     assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
72
73     // ACK the last transaction in the multi-transaction batch. This should cause that batch to become complete AND
74
// cause all of its constituent transactions to become completed.
75
assertEquals(batch2.batchID, acct.acknowledge(txID2));
76     incompleteBatchIDs.remove(batch2.batchID);
77     assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList JavaDoc()));
78     assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID());
79     assertEquals(batch2.transactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
80     assertEquals(batch2.transactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
81
82     // now clear the completed transactions
83
acct.clearCompletedTransactionIds();
84     completedGlobalTransactionIDs.clear();
85     assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
86
87     // ACK another transaction
88
assertEquals(batch3.batchID, acct.acknowledge(txID3));
89     completedGlobalTransactionIDs.addAll(batch3.transactionIDs);
90     incompleteBatchIDs.remove(batch3.batchID);
91     assertEquals(incompleteBatchIDs, acct.addIncompleteBatchIDsTo(new LinkedList JavaDoc()));
92     assertEquals(incompleteBatchIDs.get(0), acct.getMinIncompleteBatchID());
93     assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
94
95     // ACK the last transaction
96
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 JavaDoc()));
101     assertEquals(TxnBatchID.NULL_BATCH_ID, acct.getMinIncompleteBatchID());
102     assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
103
104     acct.clearCompletedTransactionIds();
105     completedGlobalTransactionIDs.clear();
106     assertEquals(completedGlobalTransactionIDs, acct.addCompletedTransactionIDsTo(new HashSet JavaDoc()));
107   }
108
109   /**
110    * Tests that the set of incomplete batch ids comes out in the same order it come in.
111    */

112   public void testBatchOrdering() throws Exception JavaDoc {
113     List JavaDoc batchIDs = new LinkedList JavaDoc();
114     for (int i = 0; i < 1000; i++) {
115       TxnBatchID batchID = new TxnBatchID(sequence.next());
116       Set JavaDoc transactionIDs = new HashSet JavaDoc();
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 JavaDoc()));
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 JavaDoc transactionIDs = new HashSet JavaDoc();
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