KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > gtx > GlobalTransactionManagerImplTest


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.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 JavaDoc;
16 import java.util.Collection JavaDoc;
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 JavaDoc {
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 JavaDoc {
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     // now commit them
44
gtxm.commit(null, stxID1);
45     gtxm.commit(null, stxID2);
46
47     // the apply has already been started, so no-one else should have to do it.
48
assertFalse(gtxm.needsApply(stxID1));
49     assertFalse(gtxm.needsApply(stxID2));
50
51     // now try to commit again
52
try {
53       gtxm.commit(null, stxID1);
54       fail("TransactionCommittedError");
55     } catch (TransactionCommittedError e) {
56       // expected
57
}
58
59     transactionStore.restart();
60   }
61
62   public void testReapplyTransactionsAcrossRestart() throws Exception JavaDoc {
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     // make sure no new calls to load were made
79
assertGlobalTxWasNotLoaded();
80
81     // RESTART
82
transactionStore.restart();
83
84     // the transaction is still not applied
85
assertTrue(gtxm.needsApply(stxid));
86     assertGlobalTxWasLoaded(stxid);
87     assertNextGlobalTxNotCalled();
88
89     // get a new global Txid number
90
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     // no longer needs to be applied
102
assertFalse(gtxm.needsApply(stxid));
103     assertGlobalTxWasLoaded(stxid);
104     assertNextGlobalTxNotCalled();
105
106     // make sure no calls to store were made
107
assertTrue(transactionStore.commitContextQueue.isEmpty());
108
109     // RESTART
110
transactionStore.restart();
111
112     // make sure that it isn't in progress
113
assertFalse(gtxm.needsApply(stxid));
114     assertGlobalTxWasLoaded(stxid);
115     assertNextGlobalTxNotCalled();
116
117     // get a new global Txid number
118
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       // expected
128
}
129     assertGlobalTxWasLoaded(stxid);
130     assertNextGlobalTxNotCalled();
131
132     // APPLY A NEW TRANSACTION
133
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 JavaDoc finished = new ArrayList JavaDoc(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     // Check if the servertransactions are removed from the databases
151
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