KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > gtx > ClientGlobalTransactionManagerTest


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.gtx;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7 import com.tc.object.tx.TestRemoteTransactionManager;
8 import com.tc.object.tx.TransactionID;
9
10 import junit.framework.TestCase;
11
12 public class ClientGlobalTransactionManagerTest extends TestCase {
13
14   private ClientGlobalTransactionManagerImpl mgr;
15
16   public void setUp() {
17     mgr = new ClientGlobalTransactionManagerImpl(new TestRemoteTransactionManager());
18   }
19
20   public void testBasics() throws Exception JavaDoc {
21     int max = 5;
22     for (int i = 100; i <= max; i++) {
23       GlobalTransactionID gtx1 = new GlobalTransactionID(i);
24       ChannelID channelID = new ChannelID(i);
25       TransactionID transactionID = new TransactionID(i);
26       // start the apply
27
assertTrue(mgr.startApply(channelID, transactionID, gtx1));
28       // a further call to startApply should return false, since the apply is already in progress or complete.
29
assertFalse(mgr.startApply(channelID, transactionID, gtx1));
30
31       if (i > 2) {
32         GlobalTransactionID lowWatermark = new GlobalTransactionID(i - 1);
33         ChannelID chIDBelowWatermark = new ChannelID(i - 2);
34         TransactionID txIDBelowWatermark = new TransactionID(i - 2);
35         GlobalTransactionID belowLowWatermark = new GlobalTransactionID(i - mgr.getAllowedLowWaterMarkDelta());
36         mgr.setLowWatermark(lowWatermark);
37
38         try {
39           mgr.startApply(chIDBelowWatermark, txIDBelowWatermark, belowLowWatermark);
40           fail("Should have thrown an UnknownTransactionError");
41         } catch (UnknownTransactionError e) {
42           // expected
43
}
44       }
45     }
46   }
47
48   public void testCleanup() throws Exception JavaDoc {
49     ChannelID channelID = new ChannelID(1);
50     TransactionID txID = new TransactionID(1);
51     GlobalTransactionID gtxID1 = new GlobalTransactionID(1);
52     GlobalTransactionID gtxID2 = new GlobalTransactionID(2);
53     GlobalTransactionID gtxID3 = new GlobalTransactionID(3 + mgr.getAllowedLowWaterMarkDelta());
54
55     assertEquals(0, mgr.size());
56     assertTrue(mgr.startApply(channelID, txID, gtxID1));
57     assertEquals(1, mgr.size());
58
59     // calling startApply with a different GlobalTransactionID should have the same result as calling it with the
60
// same GlobalTransactionID.
61
assertFalse(mgr.startApply(channelID, txID, gtxID1));
62     assertFalse(mgr.startApply(channelID, txID, gtxID2));
63     assertEquals(1, mgr.size());
64
65     // setting the low watermark to a gtxID equal to the lowest recorded for that pair should not remove that pair
66
mgr.setLowWatermark(gtxID1);
67     assertFalse(mgr.startApply(channelID, txID, gtxID2));
68     assertEquals(1, mgr.size());
69
70     // setting the low watermark to a gtxID above the highest recorded for that pair SHOULD remove that pair
71
// NOTE: The server should never re-send a transaction with the lower GlobalTransactionID: the only reason it will
72
// send a different global transaction id is if the server crashed before it was able to commit the earlier global
73
// transaction id => server transaction id mapping. In that case, the server will never send the lower gtx as the
74
// low watermark because the mapping doesn't exist in the server (because it didn't get committed)
75
mgr.setLowWatermark(gtxID3);
76     assertEquals(0, mgr.size());
77
78     // NOTE: the following should never happen in practical use, but is useful to make sure that the cleanup is
79
// happening properly
80
// The mgr should have forgotten about the channel id and transaction id by setting the watermark above the highest
81
// global transaction id recorded for that transaction.
82
assertTrue(mgr.startApply(channelID, txID, gtxID3));
83   }
84 }
85
Popular Tags