KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > transactions > StandardTransactionalObjectTest


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.test.transactions;
5
6 import com.tc.test.TCTestCase;
7 import com.tc.util.TCAssertionError;
8
9 /**
10  * Unit test for {@link StandardTransactionalObject}.
11  */

12 public class StandardTransactionalObjectTest extends TCTestCase {
13
14   private TransactionalObject trans;
15
16   public void setUp() throws Exception JavaDoc {
17     this.trans = new StandardTransactionalObject("test-object", "foo");
18   }
19
20   /**
21    * This tests for a very specific case, where we could end up GCing old data that we might need later — this is
22    * what the GC slop in {@link StandardTransactionalObject} is for.
23    */

24   public void testMixedSpecifiedAndUnspecifiedTimes() throws Exception JavaDoc {
25     TransactionalObject.Context write6615 = this.trans.startWrite("6615");
26     this.trans.endWrite(write6615);
27
28     Thread.sleep(100);
29
30     TransactionalObject.Context write6616 = this.trans.startWrite("6616");
31
32     Thread.sleep(100);
33
34     long readTime = System.currentTimeMillis();
35
36     Thread.sleep(100);
37
38     this.trans.endWrite(write6616);
39
40     Thread.sleep(100);
41
42     TransactionalObject.Context read = this.trans.startRead();
43     this.trans.endRead(read, "6616");
44
45     read = this.trans.startRead(readTime);
46     this.trans.endRead(read, "6615");
47   }
48
49   public void xtestSimpleWrites() throws Exception JavaDoc {
50     checkValues(new String JavaDoc[] { "foo" }, new String JavaDoc[] { "bar", "baz", "quux" });
51
52     StandardTransactionalObject.Context context1 = this.trans.startWrite("bar");
53     checkValues(new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
54
55     this.trans.endWrite(context1);
56     checkValues(new String JavaDoc[] { "bar" }, new String JavaDoc[] { "foo", "baz", "quux" });
57
58     context1 = this.trans.startWrite("foo");
59     StandardTransactionalObject.Context context2 = this.trans.startWrite("baz");
60
61     checkValues(new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] { "quux" });
62     this.trans.endWrite(context1);
63     checkValues(new String JavaDoc[] { "foo", "baz" }, new String JavaDoc[] { "bar", "quux" });
64     this.trans.endWrite(context2);
65     checkValues(new String JavaDoc[] { "foo", "baz" }, new String JavaDoc[] { "bar", "quux" });
66   }
67
68   public void xtestInterlacedReads() {
69     this.trans = new StandardTransactionalObject("test-object", 0, "foo", 1000);
70
71     StandardTransactionalObject.Context readContext1 = this.trans.startRead(1020);
72     StandardTransactionalObject.Context readContext1b = this.trans.startRead(1020);
73     StandardTransactionalObject.Context readContext1c = this.trans.startRead(1020);
74     StandardTransactionalObject.Context writeContext1 = this.trans.startWrite("bar", 1040);
75     StandardTransactionalObject.Context readContext2 = this.trans.startRead(1060);
76     StandardTransactionalObject.Context readContext2b = this.trans.startRead(1060);
77     StandardTransactionalObject.Context readContext2c = this.trans.startRead(1060);
78     StandardTransactionalObject.Context writeContext2 = this.trans.startWrite("baz", 1080);
79     StandardTransactionalObject.Context readContext3 = this.trans.startRead(1100);
80     StandardTransactionalObject.Context readContext3b = this.trans.startRead(1100);
81     StandardTransactionalObject.Context readContext3c = this.trans.startRead(1100);
82
83     checkValues(readContext1, 1020, new String JavaDoc[] { "foo" }, new String JavaDoc[] { "bar", "baz" });
84     checkValues(readContext1, 1030, new String JavaDoc[] { "foo" }, new String JavaDoc[] { "bar", "baz" });
85     checkValues(readContext1, 1039, new String JavaDoc[] { "foo" }, new String JavaDoc[] { "bar", "baz" });
86     checkValues(readContext1, 1040, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz" });
87     checkValues(readContext1, 1079, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz" });
88     checkValues(readContext1, 1080, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
89     checkValues(readContext1, 1120, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
90
91     checkValues(readContext2, 1060, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz" });
92     checkValues(readContext2, 1079, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz" });
93     checkValues(readContext2, 1080, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
94     checkValues(readContext2, 1120, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
95
96     checkValues(readContext3, 1100, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
97
98     this.trans.endWrite(writeContext1, 1140);
99
100     checkValues(readContext1b, 1160, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
101     checkValues(readContext2b, 1160, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
102     checkValues(readContext3b, 1160, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
103
104     this.trans.endWrite(writeContext2, 1180);
105
106     checkValues(readContext1c, 1200, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
107     checkValues(readContext2c, 1200, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
108     checkValues(readContext3c, 1200, new String JavaDoc[] { "foo", "bar", "baz" }, new String JavaDoc[] {});
109
110     StandardTransactionalObject.Context readContext4 = this.trans.startRead(1180);
111
112     checkValues(readContext4, 1180, new String JavaDoc[] { "bar", "baz" }, new String JavaDoc[] { "foo" });
113   }
114
115   public void xtestSlop() throws Exception JavaDoc {
116     this.trans = new StandardTransactionalObject("test-object", 100, "foo", 1000);
117
118     StandardTransactionalObject.Context context1 = this.trans.startWrite("bar", 1050);
119     checkValues(1050, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
120     this.trans.endWrite(context1, 1070);
121
122     checkValues(1070, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
123     checkValues(1100, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
124     checkValues(1169, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
125     checkValues(1170, new String JavaDoc[] { "foo", "bar" }, new String JavaDoc[] { "baz", "quux" });
126     checkValues(1171, new String JavaDoc[] { "bar" }, new String JavaDoc[] { "foo", "baz", "quux" });
127     checkValues(1500, new String JavaDoc[] { "bar" }, new String JavaDoc[] { "foo", "baz", "quux" });
128
129     context1 = this.trans.startWrite("baz", 2000);
130     checkValues(2000, new String JavaDoc[] { "bar", "baz" }, new String JavaDoc[] { "foo", "quux" });
131     checkValues(2050, new String JavaDoc[] { "bar", "baz" }, new String JavaDoc[] { "foo", "quux" });
132     checkValues(2150, new String JavaDoc[] { "bar", "baz" }, new String JavaDoc[] { "foo", "quux" });
133     StandardTransactionalObject.Context context2 = this.trans.startWrite("quux", 2200);
134
135     checkValues(2200, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
136     checkValues(2300, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
137     checkValues(2400, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
138
139     this.trans.endWrite(context2, 2500);
140     checkValues(2500, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
141     checkValues(2599, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
142     checkValues(2600, new String JavaDoc[] { "bar", "baz", "quux" }, new String JavaDoc[] { "foo" });
143     checkValues(2601, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
144
145     this.trans.endWrite(context1, 2800);
146     checkValues(2800, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
147     checkValues(2801, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
148     checkValues(2900, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
149
150     // This is important: because we can't actually know which write happened first, it needs to keep returning both
151
// values.
152
checkValues(2901, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
153     checkValues(5000, new String JavaDoc[] { "baz", "quux" }, new String JavaDoc[] { "foo", "bar" });
154   }
155
156   private void checkValues(String JavaDoc[] accepted, String JavaDoc[] notAccepted) {
157     checkValues(System.currentTimeMillis(), accepted, notAccepted);
158   }
159
160   private void checkValues(long when, String JavaDoc[] accepted, String JavaDoc[] notAccepted) {
161     checkValues(this.trans.startRead(when), when, accepted, notAccepted);
162   }
163
164   private void checkValues(StandardTransactionalObject.Context context, long when, String JavaDoc[] accepted,
165                            String JavaDoc[] notAccepted) {
166     for (int i = 0; i < accepted.length; ++i) {
167       this.trans.endRead(context, accepted[i], when);
168     }
169
170     for (int i = 0; i < notAccepted.length; ++i) {
171       try {
172         this.trans.endRead(context, notAccepted[i], when);
173         fail("Didn't get TCAE on endRead with bad value");
174       } catch (TCAssertionError tcae) {
175         // ok
176
}
177     }
178
179   }
180
181 }
182
Popular Tags