001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.object.config.spec.CyclicBarrierSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.util.Assert;
016: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020: import java.util.TreeSet;
021:
022: public class TreeSetSimplifiedGCTestApp extends
023: AbstractErrorCatchingTransparentApp {
024: // roots
025: private final CyclicBarrier barrier = new CyclicBarrier(
026: getParticipantCount());
027: private final Map root = new HashMap();
028:
029: public TreeSetSimplifiedGCTestApp(String appId,
030: ApplicationConfig cfg, ListenerProvider listenerProvider) {
031: super (appId, cfg, listenerProvider);
032: }
033:
034: protected void runTest() throws Throwable {
035: final int index = barrier.barrier();
036:
037: if (index == 0) {
038: synchronized (root) {
039: TreeSet set = new TreeSet();
040: set.add(new FooObject(0));
041: set.add(new FooObject(1));
042: set.add(new FooObject(2));
043: set.add(new FooObject(3));
044: root.put("key0", set);
045: }
046: synchronized (root) {
047: TreeSet set = (TreeSet) root.get("key0");
048: set.remove(new FooObject(3));
049: }
050: synchronized (root) {
051: TreeSet set = (TreeSet) root.get("key0");
052: Assert.assertEquals("Mutator see HashSet size", 3, set
053: .size());
054: }
055: }
056: barrier.barrier();
057: if (index != 0) {
058: synchronized (root) {
059: TreeSet set = (TreeSet) root.get("key0");
060: Assert.assertEquals("Reader see HashSet size", 3, set
061: .size());
062: }
063: }
064: barrier.barrier();
065: }
066:
067: public static void visitL1DSOConfig(ConfigVisitor visitor,
068: DSOClientConfigHelper config) {
069: new CyclicBarrierSpec().visit(visitor, config);
070: config.getOrCreateSpec(FooObject.class.getName());
071:
072: String testClassName = TreeSetSimplifiedGCTestApp.class
073: .getName();
074: TransparencyClassSpec spec = config
075: .getOrCreateSpec(testClassName);
076: String methodExpression = "* " + testClassName + "*.*(..)";
077: config.addWriteAutolock(methodExpression);
078: spec.addRoot("root", "root");
079: spec.addRoot("barrier", "barrier");
080: }
081:
082: private static final class FooObject implements Comparable {
083: private final int id;
084:
085: public FooObject(int id) {
086: this .id = id;
087: }
088:
089: public int getId() {
090: return id;
091: }
092:
093: public boolean equals(Object foo) {
094: if (foo == null) {
095: return false;
096: }
097: return ((FooObject) foo).getId() == id;
098: }
099:
100: public int hashCode() {
101: return id;
102: }
103:
104: public int compareTo(Object o) {
105: int othersId = ((FooObject) o).getId();
106: if (id < othersId) {
107: return -1;
108: } else if (id == othersId) {
109: return 0;
110: } else {
111: return 1;
112: }
113: }
114: }
115: }
|