001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.ListenerProvider;
013: import com.tc.util.Assert;
014: import com.tctest.runner.AbstractTransparentApp;
015:
016: import java.util.Comparator;
017: import java.util.Set;
018: import java.util.SortedSet;
019: import java.util.TreeSet;
020:
021: public class TreeSetTestApp extends AbstractTransparentApp {
022:
023: // plain old TreeSet
024: private final TreeSet set = new TreeSet();
025:
026: // Use a comparator with a shared TreeSet too. If the comparator doesn't make it across VMs,
027: // we should get some ClassCastExceptions.
028: private final TreeSet set2 = new TreeSet(
029: new WrappedStringComparator());
030:
031: private final CyclicBarrier barrier;
032:
033: private final SubSetSharedObject subSetSharedRoot = new SubSetSharedObject(
034: 0);
035:
036: public TreeSetTestApp(String appId, ApplicationConfig cfg,
037: ListenerProvider listenerProvider) {
038: super (appId, cfg, listenerProvider);
039: barrier = new CyclicBarrier(getParticipantCount());
040: }
041:
042: public void run() {
043: try {
044: subSetTesting();
045: headSetTesting();
046: tailSetTesting();
047: viewSetSharedTesting();
048: } catch (Throwable t) {
049: notifyError(t);
050: }
051: }
052:
053: private int getKey() throws Exception {
054: int key = -1;
055: synchronized (subSetSharedRoot) {
056: if (subSetSharedRoot.getKey() != 0) {
057: subSetSharedRoot.setKey(0);
058: }
059: }
060: barrier.barrier();
061:
062: synchronized (subSetSharedRoot) {
063: key = subSetSharedRoot.getKey();
064: if (key == 0) {
065: subSetSharedRoot.setKey(1);
066: }
067: }
068: return key;
069: }
070:
071: private void subSetTesting() throws Exception {
072: clear();
073: initializeSets();
074:
075: // subSet() testing.
076:
077: int key = getKey();
078: barrier.barrier();
079:
080: if (key == 0) {
081: synchronized (set) {
082: Object fromKey = new Integer(0);
083: Object toKey = new Integer(10);
084: Set subSet = set.subSet(fromKey, toKey);
085: subSet.add(new Integer(1));
086: }
087: }
088: barrier.barrier();
089:
090: Assert.assertEquals(getParticipantCount() + 1, set.size());
091: Assert.assertTrue(set.contains(new Integer(1)));
092:
093: barrier.barrier();
094: key = getKey();
095: barrier.barrier();
096:
097: if (key == 0) {
098: synchronized (set2) {
099: Object fromKey = new WrappedString(0);
100: Object toKey = new WrappedString(10);
101: Set subSet = set2.subSet(fromKey, toKey);
102: subSet.add(new WrappedString(1));
103: }
104: }
105: barrier.barrier();
106:
107: Assert.assertEquals(getParticipantCount() + 1, set2.size());
108: Assert.assertTrue(set2.contains(new WrappedString(1)));
109: barrier.barrier();
110: }
111:
112: private void headSetTesting() throws Exception {
113: clear();
114: initializeSets();
115:
116: int key = getKey();
117: barrier.barrier();
118:
119: // headSet() testing.
120: if (key == 0) {
121: synchronized (set) {
122: Object toKey = new Integer(2);
123: Set headSet = set.headSet(toKey);
124: headSet.add(new Integer(1));
125: }
126: }
127: barrier.barrier();
128:
129: Assert.assertEquals(getParticipantCount() + 1, set.size());
130: Assert.assertTrue(set.contains(new Integer(1)));
131:
132: barrier.barrier();
133:
134: key = getKey();
135: barrier.barrier();
136:
137: if (key == 0) {
138: synchronized (set2) {
139: Object toKey = new WrappedString(2);
140: Set headSet = set2.headSet(toKey);
141: headSet.add(new WrappedString(1));
142: }
143: }
144:
145: barrier.barrier();
146:
147: Assert.assertEquals(getParticipantCount() + 1, set2.size());
148: Assert.assertTrue(set2.contains(new WrappedString(1)));
149:
150: barrier.barrier();
151: }
152:
153: private void tailSetTesting() throws Exception {
154: clear();
155: initializeSets();
156:
157: // tailSet() testing.
158: int key = getKey();
159: barrier.barrier();
160:
161: if (key == 0) {
162: synchronized (set) {
163: Object fromKey = new Integer(0);
164: Set tailSet = set.tailSet(fromKey);
165: tailSet.add(new Integer(1));
166: }
167: }
168:
169: barrier.barrier();
170:
171: Assert.assertEquals(getParticipantCount() + 1, set.size());
172: Assert.assertTrue(set.contains(new Integer(1)));
173:
174: barrier.barrier();
175:
176: key = getKey();
177: barrier.barrier();
178:
179: if (key == 0) {
180: synchronized (set2) {
181: Object fromKey = new WrappedString(0);
182: Set tailSet = set2.tailSet(fromKey);
183: tailSet.add(new WrappedString(1));
184: }
185: }
186:
187: barrier.barrier();
188:
189: Assert.assertEquals(getParticipantCount() + 1, set2.size());
190: Assert.assertTrue(set2.contains(new WrappedString(1)));
191: barrier.barrier();
192:
193: clear();
194: initializeSets();
195:
196: key = getKey();
197: barrier.barrier();
198:
199: // tailSet() clear testing.
200: if (key == 0) {
201: synchronized (set) {
202: Object fromKey = new Integer(0);
203: Set tailSet = set.tailSet(fromKey);
204: tailSet.clear();
205: }
206: }
207: barrier.barrier();
208:
209: Assert.assertEquals(0, set.size());
210:
211: barrier.barrier();
212:
213: key = getKey();
214: barrier.barrier();
215:
216: if (key == 0) {
217: synchronized (set2) {
218: Object fromKey = new WrappedString(0);
219: Set tailSet = set2.tailSet(fromKey);
220: tailSet.clear();
221: }
222: }
223: barrier.barrier();
224:
225: Assert.assertEquals(0, set2.size());
226: barrier.barrier();
227: }
228:
229: private void viewSetSharedTesting() throws Exception {
230: clear();
231: initializeSets();
232:
233: int key = getKey();
234: barrier.barrier();
235:
236: // subSet() share testing.
237: if (key == 0) {
238: Object fromKey = new Integer(0);
239: Object toKey = new Integer(10);
240: Set subSet = set.subSet(fromKey, toKey);
241: synchronized (subSetSharedRoot) {
242: subSetSharedRoot.setSet(subSet);
243: }
244: synchronized (subSetSharedRoot.getSet()) {
245: subSetSharedRoot.getSet().add(new Integer(1));
246: }
247: }
248:
249: barrier.barrier();
250:
251: Assert.assertEquals(getParticipantCount() + 1, set.size());
252: Assert.assertTrue(set.contains(new Integer(1)));
253:
254: barrier.barrier();
255:
256: key = getKey();
257: barrier.barrier();
258:
259: if (key == 0) {
260: Object fromKey = new WrappedString(0);
261: Object toKey = new WrappedString(10);
262: Set subSet = set2.subSet(fromKey, toKey);
263: synchronized (subSetSharedRoot) {
264: subSetSharedRoot.setSet(subSet);
265: }
266: synchronized (subSetSharedRoot.getSet()) {
267: subSetSharedRoot.getSet().add(new WrappedString(1));
268: }
269: }
270:
271: barrier.barrier();
272:
273: Assert.assertEquals(getParticipantCount() + 1, set2.size());
274: Assert.assertTrue(set2.contains(new WrappedString(1)));
275:
276: barrier.barrier();
277:
278: clear();
279: initializeSets();
280:
281: key = getKey();
282: barrier.barrier();
283:
284: // headSet() share testing.
285: if (key == 0) {
286: Object toKey = new Integer(10);
287: Set headSet = set.headSet(toKey);
288: synchronized (subSetSharedRoot) {
289: subSetSharedRoot.setSet(headSet);
290: }
291: synchronized (subSetSharedRoot.getSet()) {
292: subSetSharedRoot.getSet().add(new Integer(1));
293: }
294: }
295:
296: barrier.barrier();
297:
298: Assert.assertEquals(getParticipantCount() + 1, set.size());
299: Assert.assertTrue(set.contains(new Integer(1)));
300:
301: barrier.barrier();
302:
303: key = getKey();
304: barrier.barrier();
305:
306: if (key == 0) {
307: Object toKey = new WrappedString(10);
308: Set headSet = set2.headSet(toKey);
309: synchronized (subSetSharedRoot) {
310: subSetSharedRoot.setSet(headSet);
311: }
312: synchronized (subSetSharedRoot.getSet()) {
313: subSetSharedRoot.getSet().add(new WrappedString(1));
314: }
315: }
316:
317: barrier.barrier();
318:
319: Assert.assertEquals(getParticipantCount() + 1, set2.size());
320: Assert.assertTrue(set2.contains(new WrappedString(1)));
321:
322: barrier.barrier();
323:
324: clear();
325: initializeSets();
326:
327: key = getKey();
328: barrier.barrier();
329:
330: // tailSet() share testing.
331: if (key == 0) {
332: Object fromKey = new Integer(0);
333: Set tailSet = set.tailSet(fromKey);
334: synchronized (subSetSharedRoot) {
335: subSetSharedRoot.setSet(tailSet);
336: }
337:
338: synchronized (subSetSharedRoot.getSet()) {
339: subSetSharedRoot.getSet().add(new Integer(1));
340: }
341: }
342:
343: barrier.barrier();
344:
345: Assert.assertEquals(getParticipantCount() + 1, set.size());
346: Assert.assertTrue(set.contains(new Integer(1)));
347:
348: barrier.barrier();
349:
350: key = getKey();
351: barrier.barrier();
352:
353: if (key == 0) {
354: Object fromKey = new WrappedString(0);
355: Set tailSet = set2.tailSet(fromKey);
356: synchronized (subSetSharedRoot) {
357: subSetSharedRoot.setSet(tailSet);
358: }
359:
360: synchronized (subSetSharedRoot.getSet()) {
361: subSetSharedRoot.getSet().add(new WrappedString(1));
362: }
363: }
364: barrier.barrier();
365:
366: Assert.assertEquals(getParticipantCount() + 1, set2.size());
367: Assert.assertTrue(set2.contains(new WrappedString(1)));
368:
369: barrier.barrier();
370:
371: clear();
372: initializeSets();
373:
374: key = getKey();
375: barrier.barrier();
376:
377: // subSet().subSet() share testing.
378: if (key == 0) {
379: Object fromKey = new Integer(0);
380: Object toKey = new Integer(10);
381: Object toKey2 = new Integer(5);
382: SortedSet subSet = set.subSet(fromKey, toKey);
383: Set subSet2 = subSet.subSet(fromKey, toKey2);
384: synchronized (subSetSharedRoot) {
385: subSetSharedRoot.setSet(subSet2);
386: }
387: synchronized (subSetSharedRoot.getSet()) {
388: subSetSharedRoot.getSet().add(new Integer(1));
389: }
390: }
391:
392: barrier.barrier();
393:
394: Assert.assertEquals(getParticipantCount() + 1, set.size());
395: Assert.assertTrue(set.contains(new Integer(1)));
396:
397: barrier.barrier();
398:
399: key = getKey();
400: barrier.barrier();
401: if (key == 0) {
402: Object fromKey = new WrappedString(0);
403: Object toKey = new WrappedString(10);
404: Object toKey2 = new WrappedString(5);
405: SortedSet subSet = set2.subSet(fromKey, toKey2);
406: Set subSet2 = subSet.subSet(fromKey, toKey);
407: synchronized (subSetSharedRoot) {
408: subSetSharedRoot.setSet(subSet2);
409: }
410: synchronized (subSetSharedRoot.getSet()) {
411: subSetSharedRoot.getSet().add(new WrappedString(1));
412: }
413: }
414:
415: barrier.barrier();
416: Assert.assertEquals(getParticipantCount() + 1, set2.size());
417: Assert.assertTrue(set2.contains(new WrappedString(1)));
418:
419: barrier.barrier();
420: }
421:
422: private void clear() throws Exception {
423: synchronized (set) {
424: set.clear();
425: }
426: synchronized (set2) {
427: set2.clear();
428: }
429:
430: synchronized (subSetSharedRoot) {
431: subSetSharedRoot.clear();
432: }
433:
434: barrier.barrier();
435: }
436:
437: private void initializeSets() throws Exception {
438: synchronized (subSetSharedRoot) {
439: if (subSetSharedRoot.getKey() != 0) {
440: subSetSharedRoot.setKey(0);
441: }
442: }
443: barrier.barrier();
444:
445: synchronized (set) {
446: int key = subSetSharedRoot.getKey();
447: set.add(new Integer(key));
448: subSetSharedRoot.setKey(key + 2);
449: }
450: barrier.barrier();
451: synchronized (subSetSharedRoot) {
452: if (subSetSharedRoot.getKey() != 0) {
453: subSetSharedRoot.setKey(0);
454: }
455: }
456: barrier.barrier();
457: synchronized (set2) {
458: int key = subSetSharedRoot.getKey();
459: set2.add(new WrappedString(key));
460: subSetSharedRoot.setKey(key + 2);
461: }
462: barrier.barrier();
463: }
464:
465: public static void visitL1DSOConfig(ConfigVisitor visitor,
466: DSOClientConfigHelper config) {
467: TransparencyClassSpec spec = config
468: .getOrCreateSpec(CyclicBarrier.class.getName());
469: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
470: + "*.*(..)");
471:
472: String testClass = TreeSetTestApp.class.getName();
473: spec = config.getOrCreateSpec(testClass);
474:
475: config.addIncludePattern(testClass + "$*");
476:
477: String methodExpression = "* " + testClass + "*.*(..)";
478: config.addWriteAutolock(methodExpression);
479:
480: spec.addRoot("set", "set");
481: spec.addRoot("set2", "set2");
482: spec.addRoot("barrier", "barrier");
483: spec.addRoot("subSetSharedRoot", "subSetSharedRoot");
484: }
485:
486: // The main purpose of this class is that it does NOT implement Comparable
487: private static class WrappedString {
488: private final String string;
489:
490: WrappedString(String string) {
491: this .string = string;
492: }
493:
494: WrappedString(int i) {
495: this .string = String.valueOf(i);
496: }
497:
498: String getString() {
499: return this .string;
500: }
501: }
502:
503: private static class WrappedStringComparator implements Comparator {
504:
505: public int compare(Object o1, Object o2) {
506: WrappedString ws1 = (WrappedString) o1;
507: WrappedString ws2 = (WrappedString) o2;
508: return ws1.getString().compareTo(ws2.getString());
509: }
510:
511: }
512:
513: /**
514: * The main purpose of this class is for to generate the key for the subSet(), headSet(), and tailSet() testing.
515: */
516: private static class SubSetSharedObject {
517: private int key;
518: private Set set;
519:
520: public SubSetSharedObject(int key) {
521: this .key = key;
522: }
523:
524: public int getKey() {
525: return key;
526: }
527:
528: public void setKey(int key) {
529: this .key = key;
530: }
531:
532: public Set getSet() {
533: return set;
534: }
535:
536: public void setSet(Set set) {
537: this .set = set;
538: }
539:
540: public void clear() {
541: this .key = 0;
542: this.set = null;
543: }
544: }
545: }
|