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.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
015:
016: import java.util.HashMap;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: public class RootClassChangeTestApp extends
021: AbstractErrorCatchingTransparentApp {
022: private final String appId;
023: private final int nodeCount;
024: private final boolean adapted;
025: private final HashMap tcHashMap;
026: private final int numOfLevels;
027: private final int numOfIterations;
028: private final CyclicBarrier barrier;
029:
030: public RootClassChangeTestApp(String appId, ApplicationConfig cfg,
031: ListenerProvider listenerProvider) {
032: super (appId, cfg, listenerProvider);
033: this .appId = appId;
034: nodeCount = cfg.getGlobalParticipantCount();
035: adapted = Boolean
036: .valueOf(
037: cfg.getAttribute(appId
038: + ApplicationConfig.ADAPTED_KEY))
039: .booleanValue();
040:
041: tcHashMap = new HashMap();
042: numOfLevels = 20;
043: numOfIterations = 100;
044: barrier = new CyclicBarrier(nodeCount);
045: }
046:
047: // un-adapted version has a field missing that adapted version has
048: public void runTest() throws Throwable {
049: System.out.println("***** Test starting: appId=[" + appId
050: + "] adapted=[" + adapted + "] nodeCount=[" + nodeCount
051: + "]");
052:
053: if (!adapted) {
054: runPutTest();
055: runGetTest();
056: }
057:
058: System.out.println("***** Barrier 1: appId=[" + appId
059: + "] adapted=[" + adapted + "]");
060:
061: barrier.barrier();
062:
063: if (adapted) {
064: runPutTest();
065: runGetTest();
066: }
067:
068: System.out.println("***** Barrier 2: appId=[" + appId
069: + "] adapted=[" + adapted + "]");
070:
071: barrier.barrier();
072:
073: if (!adapted) {
074: runPutTest();
075: runGetTest();
076: }
077:
078: System.out.println("***** Barrier 3: appId=[" + appId
079: + "] adapted=[" + adapted + "]");
080:
081: barrier.barrier();
082:
083: if (adapted) {
084: runPutTest();
085: runGetTest();
086: }
087:
088: System.out.println("***** Barrier 4: appId=[" + appId
089: + "] adapted=[" + adapted + "]");
090:
091: barrier.barrier();
092: }
093:
094: private void runGetTest() {
095: System.out.println("***** appId=[" + appId + "] adapted=["
096: + adapted + "] runGetTest");
097:
098: Set keySet;
099: synchronized (tcHashMap) {
100: keySet = tcHashMap.keySet();
101: }
102:
103: System.out.println("***** keySetSize=[" + keySet.size() + "]");
104:
105: for (Iterator iter = keySet.iterator(); iter.hasNext();) {
106: Object key = iter.next();
107: DeepLargeObject dlo;
108: synchronized (tcHashMap) {
109: dlo = (DeepLargeObject) tcHashMap.get(key);
110: }
111:
112: Object foo;
113: try {
114: foo = dlo.getFooObject();
115: } catch (Throwable t) {
116: foo = null;
117: }
118:
119: System.out.println("***** tcHashMap: key=[" + key
120: + "] numLevel=[" + dlo.getNumOfLevel()
121: + "] childObjectLevel=["
122: + dlo.getChildObject().getNumOfLevel()
123: + "] fooObject=[" + foo + "]");
124: }
125: }
126:
127: private void runPutTest() throws Exception {
128: System.out.println("***** appId=[" + appId + "] adapted=["
129: + adapted + "] runPutTest");
130:
131: int nextPosition = tcHashMap.size();
132:
133: for (int i = 0; i < numOfIterations; i++) {
134: int key = i + nextPosition;
135:
136: System.out.println("***** key=[" + key + "]");
137:
138: synchronized (tcHashMap) {
139: tcHashMap.put("key" + key, new DeepLargeObject(
140: numOfLevels));
141: }
142: }
143: }
144:
145: public static void visitL1DSOConfig(ConfigVisitor visitor,
146: DSOClientConfigHelper config) {
147: String testClass = RootClassChangeTestApp.class.getName();
148: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
149:
150: String methodExpression = "* " + testClass + "*.*(..)";
151: config.addWriteAutolock(methodExpression);
152:
153: config.addIncludePattern(testClass + "$*");
154:
155: spec.addRoot("tcHashMap", "tcHashMap");
156: spec.addRoot("barrier", "barrier");
157:
158: config.getOrCreateSpec(CyclicBarrier.class.getName());
159: config.addWriteAutolock("* " + CyclicBarrier.class.getName()
160: + "*.*(..)");
161: }
162:
163: public static class DeepLargeObject {
164: private int numOfLevel;
165:
166: private DeepLargeObject childObject;
167:
168: // Code added by adapter:
169: // private FooObject foo;
170:
171: public DeepLargeObject(int numOfLevel) {
172: this .numOfLevel = numOfLevel;
173: if (numOfLevel > 0) {
174: this .childObject = new DeepLargeObject(numOfLevel - 1);
175: } else {
176: this .childObject = null;
177: }
178: setFooObject();
179: }
180:
181: private void setFooObject() {
182: // Code added by adapter:
183: // foo = new FooObject("Yeah", 5);
184: }
185:
186: public DeepLargeObject getChildObject() {
187: return childObject;
188: }
189:
190: public int getNumOfLevel() {
191: return numOfLevel;
192: }
193:
194: public FooObject getFooObject() {
195: // Code changed to following by adapter:
196: // return foo;
197: throw new AssertionError();
198: }
199: }
200:
201: public static class FooObject {
202: private String name;
203:
204: private int count;
205:
206: public FooObject(String name, int count) {
207: this .name = name;
208: this .count = count;
209: }
210:
211: public String toString() {
212: return "FooObject: name=[" + name + "] count=[" + count
213: + "]";
214: }
215: }
216: }
|