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.BrokenBarrierException;
008: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
009:
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.TransparencyClassSpec;
013: import com.tc.object.config.spec.CyclicBarrierSpec;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.util.Assert;
017: import com.tctest.runner.AbstractTransparentApp;
018:
019: public class AutolockedDistributedMethodCallTestApp extends
020: AbstractTransparentApp {
021:
022: private static final int ITERATION_COUNT = 10;
023:
024: private static String appId;
025: private final int nodeCount;
026:
027: // roots
028: private CyclicBarrier barrier;
029: private SharedObject sharedObject;
030:
031: public AutolockedDistributedMethodCallTestApp(String appId,
032: ApplicationConfig cfg, ListenerProvider listenerProvider) {
033: super (appId, cfg, listenerProvider);
034: AutolockedDistributedMethodCallTestApp.appId = appId;
035: nodeCount = cfg.getGlobalParticipantCount();
036: barrier = new CyclicBarrier(this .nodeCount);
037: sharedObject = new SharedObject(nodeCount);
038: }
039:
040: public void run() {
041: try {
042: int id = barrier.barrier();
043: if (id % nodeCount == 0) {
044: System.out
045: .println("##### appId=["
046: + appId
047: + "] initiating autolockedSynchronizedRead method call");
048: this .sharedObject.autolockedSynchronizedRead();
049: for (int i = 0; i < ITERATION_COUNT; i++) {
050: System.out
051: .println("##### appId=["
052: + appId
053: + "] initiating autolockedSynchronizedIncrement method call");
054: this .sharedObject.autolockedSynchronizedIncrement();
055: System.out
056: .println("##### appId=["
057: + appId
058: + "] initiating autosynchronizedIncrement method call");
059: this .sharedObject.autosynchronizedIncrement();
060: System.out
061: .println("##### appId=["
062: + appId
063: + "] initiating autolockedSynchronizeBlockIncrement method call");
064: this .sharedObject
065: .autolockedSynchronizeBlockIncrement();
066: System.out
067: .println("##### appId=["
068: + appId
069: + "] initiating localCounterIncrement method call");
070: this .sharedObject.localCounterIncrement();
071: }
072: }
073: barrier.barrier();
074:
075: waitForAllDistributedCalls();
076:
077: int autolockedSynchronizedCount = this .sharedObject
078: .getAutolockedSynchronizedCounter();
079: int autosynchronizedCount = this .sharedObject
080: .getAutosynchronizedCounter();
081: int autolockedSynchronizeBlockCount = this .sharedObject
082: .getAutolockedSynchronizeBlockCounter();
083: int localCount = this .sharedObject.getLocalCounter();
084: int expectedCount = ITERATION_COUNT * nodeCount;
085:
086: Assert.assertEquals(expectedCount,
087: autolockedSynchronizedCount);
088: Assert.assertEquals(expectedCount, autosynchronizedCount);
089: Assert.assertEquals(expectedCount,
090: autolockedSynchronizeBlockCount);
091: Assert.assertEquals(ITERATION_COUNT, localCount);
092:
093: } catch (Throwable e) {
094: notifyError(e);
095: }
096: }
097:
098: private void waitForAllDistributedCalls()
099: throws InterruptedException {
100: final long waitDuration = 1000 * 15;
101: long start = System.currentTimeMillis();
102: while ((System.currentTimeMillis() - start) < waitDuration) {
103: Thread.sleep(1000);
104: }
105: }
106:
107: public static void visitL1DSOConfig(ConfigVisitor visitor,
108: DSOClientConfigHelper config) {
109: try {
110: new CyclicBarrierSpec().visit(visitor, config);
111:
112: String testClassName = AutolockedDistributedMethodCallTestApp.class
113: .getName();
114: TransparencyClassSpec spec = config
115: .getOrCreateSpec(testClassName);
116: spec.addRoot("barrier", "barrier");
117: spec.addRoot("sharedObject", "sharedObject");
118: String methodExpression = "* " + testClassName + "*.*(..)";
119: config.addWriteAutolock(methodExpression);
120:
121: spec = config.getOrCreateSpec(SharedObject.class.getName());
122: spec.addDistributedMethodCall(
123: "autolockedSynchronizedIncrement", "()V", true);
124: spec.addDistributedMethodCall("autosynchronizedIncrement",
125: "()V", true);
126: spec.addDistributedMethodCall(
127: "autolockedSynchronizeBlockIncrement", "()V", true);
128: spec.addDistributedMethodCall("localCounterIncrement",
129: "()V", true);
130: spec.addDistributedMethodCall("autolockedSynchronizedRead",
131: "()V", true);
132: spec.addTransient("localObject");
133: spec.addTransient("localCounter");
134: methodExpression = "* " + SharedObject.class.getName()
135: + "*.get*(..)";
136: config.addWriteAutolock(methodExpression);
137: methodExpression = "* " + SharedObject.class.getName()
138: + "*.autolockedSynchronizedIncrement(..)";
139: config.addWriteAutolock(methodExpression);
140: methodExpression = "* " + SharedObject.class.getName()
141: + "*.autosynchronizedIncrement(..)";
142: config.addWriteAutoSynchronize(methodExpression);
143: methodExpression = "* " + SharedObject.class.getName()
144: + "*.autolockedSynchronizeBlockIncrement(..)";
145: config.addWriteAutolock(methodExpression);
146: methodExpression = "* " + SharedObject.class.getName()
147: + "*.autolockedSynchronizedRead(..)";
148: config.addReadAutolock(methodExpression);
149: } catch (Exception e) {
150: throw new AssertionError(e);
151: }
152: }
153:
154: private static class SharedObject {
155: private static final Object localObject = new Object();
156: private static int localCounter = 0;
157:
158: private int autolockedSynchronizedCounter;
159: private int autosynchronizedCounter;
160: private int autolockedSynchronizeBlockCounter;
161: private CyclicBarrier readerBarrier;
162:
163: public SharedObject(int readerCount) {
164: autolockedSynchronizedCounter = 0;
165: autosynchronizedCounter = 0;
166: autolockedSynchronizeBlockCounter = 0;
167: readerBarrier = new CyclicBarrier(readerCount);
168: }
169:
170: public synchronized int getAutolockedSynchronizedCounter() {
171: return autolockedSynchronizedCounter;
172: }
173:
174: public synchronized int getAutosynchronizedCounter() {
175: return autosynchronizedCounter;
176: }
177:
178: public synchronized int getAutolockedSynchronizeBlockCounter() {
179: return autolockedSynchronizeBlockCounter;
180: }
181:
182: public synchronized void autolockedSynchronizedIncrement() {
183: autolockedSynchronizedCounter++;
184: System.out
185: .println("##### appId=["
186: + AutolockedDistributedMethodCallTestApp.appId
187: + "] autolockedSynchronizedIncrement called: autolockedSynchronizedCounter=["
188: + autolockedSynchronizedCounter + "]");
189: }
190:
191: public void autosynchronizedIncrement() {
192: autosynchronizedCounter++;
193: System.out
194: .println("##### appId=["
195: + AutolockedDistributedMethodCallTestApp.appId
196: + "] autosynchronizedIncrement called: autosynchronizedCounter=["
197: + autosynchronizedCounter + "]");
198: }
199:
200: public void autolockedSynchronizeBlockIncrement() {
201: synchronized (this ) {
202: autolockedSynchronizeBlockCounter++;
203: System.out
204: .println("##### appId=["
205: + AutolockedDistributedMethodCallTestApp.appId
206: + "] autolockedSynchronizeBlockIncrement called: autolockedSynchronizeBlockCounter=["
207: + autolockedSynchronizeBlockCounter
208: + "]");
209: }
210: }
211:
212: public void localCounterIncrement() {
213: synchronized (localObject) {
214: localCounter++;
215: System.out
216: .println("***** appId=["
217: + AutolockedDistributedMethodCallTestApp.appId
218: + "] localCounterIncrement called: localCounter=["
219: + localCounter + "]");
220: }
221: }
222:
223: public int getLocalCounter() {
224: synchronized (localObject) {
225: return localCounter;
226: }
227: }
228:
229: public synchronized void autolockedSynchronizedRead()
230: throws BrokenBarrierException, InterruptedException {
231: System.out
232: .println("***** appId=["
233: + AutolockedDistributedMethodCallTestApp.appId
234: + "] autolockedSynchronizedRead called: autolockedSynchronizedCounter=["
235: + autolockedSynchronizedCounter + "]");
236: readerBarrier.barrier();
237: }
238: }
239:
240: }
|