001: /*
002: * All content copyright (c) 2003-2006 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: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
009:
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.DistributedMethodSpec;
013: import com.tc.object.config.TransparencyClassSpec;
014: import com.tc.object.config.spec.CyclicBarrierSpec;
015: import com.tc.object.config.spec.SynchronizedIntSpec;
016: import com.tc.simulator.app.ApplicationConfig;
017: import com.tc.simulator.listener.ListenerProvider;
018: import com.tctest.runner.AbstractTransparentApp;
019:
020: /**
021: * This test is to test the various method expressions for DistributedMethodCall. The test will also make sure that
022: * constructors and static methods will not be called.
023: */
024: public class DistributedMethodCallExpressionTestApp extends
025: AbstractTransparentApp {
026:
027: private final SharedModel model = new SharedModel();
028: private final int initialNodeCount = getParticipantCount();
029: private final CyclicBarrier nodeBarrier = new CyclicBarrier(
030: initialNodeCount);
031: private final CyclicBarrier staticNodeBarrier = new CyclicBarrier(
032: initialNodeCount);
033:
034: public DistributedMethodCallExpressionTestApp(String appId,
035: ApplicationConfig cfg, ListenerProvider listenerProvider) {
036: super (appId, cfg, listenerProvider);
037: }
038:
039: public void run() {
040: try {
041: callNonStaticMethod();
042: callStaticMethod();
043: } catch (Throwable e) {
044: notifyError(e);
045: }
046: }
047:
048: public void callNonStaticMethod() throws Throwable {
049: final boolean masterNode = nodeBarrier.barrier() == 0;
050: synchronized (model) {
051: if (masterNode) {
052: model.nonStaticMethod(null, 0, 0, null, null, false);
053: }
054: }
055: nodeBarrier.barrier();
056: System.err.println("\n### initialNodeCount = "
057: + initialNodeCount);
058: synchronized (model) {
059: checkCountTimed(model.nonStaticCallCount, initialNodeCount,
060: 10, 5 * 1000, "Non-Static Call Count");
061: }
062: nodeBarrier.barrier();
063: }
064:
065: public void callStaticMethod() throws Throwable {
066: final boolean masterNode = staticNodeBarrier.barrier() == 0;
067: synchronized (model) {
068: if (masterNode) {
069: SharedModel.staticMethod();
070: }
071: }
072: staticNodeBarrier.barrier();
073: System.err
074: .println("### -> sleeping before checking static method calls");
075: Thread.sleep(30000);
076: synchronized (model) {
077: checkCountTimed(SharedModel.staticCallCount,
078: (masterNode) ? 1 : 0, 1, 10000, "Static Call Count");
079: }
080: staticNodeBarrier.barrier();
081: }
082:
083: public static class SharedModel {
084: public final SynchronizedInt nonStaticCallCount = new SynchronizedInt(
085: 0);
086: public static final SynchronizedInt staticCallCount = new SynchronizedInt(
087: 0);
088:
089: public static void staticMethod() {
090: staticCallCount.increment();
091: }
092:
093: public void nonStaticMethod(Object obj, int i, double d,
094: FooObject[][] foos, int[][][] ints, boolean b)
095: throws Throwable {
096: nonStaticCallCount.increment();
097: }
098: }
099:
100: private void checkCountTimed(SynchronizedInt actualSI,
101: final int expected, final int slices,
102: final long sliceMillis, String msg)
103: throws InterruptedException {
104: // wait until all nodes have the right picture of the cluster
105: int actual = 0;
106: int i;
107: for (i = 0; i < slices; i++) {
108: actual = actualSI.get();
109: if (actual > expected || actual < 0) {
110: notifyError("Wrong Count: expected=" + expected
111: + ", actual=" + actual);
112: }
113: if (actual < expected) {
114: Thread.sleep(sliceMillis);
115: } else {
116: break;
117: }
118: }
119: if (i == slices) {
120: notifyError("Wrong Count: expected=" + expected
121: + ", actual=" + actual);
122: }
123: System.err.println("\n### -> check '" + msg + "' passed in "
124: + i + " slices");
125: }
126:
127: public static void visitL1DSOConfig(ConfigVisitor visitor,
128: DSOClientConfigHelper config) {
129: try {
130: new CyclicBarrierSpec().visit(visitor, config);
131: new SynchronizedIntSpec().visit(visitor, config);
132:
133: TransparencyClassSpec spec = config
134: .getOrCreateSpec(FooObject.class.getName());
135: String testClassName = DistributedMethodCallExpressionTestApp.class
136: .getName();
137: spec = config.getOrCreateSpec(testClassName);
138: spec.addRoot("model", "model");
139: spec.addRoot("nodeBarrier", "nodeBarrier");
140: spec.addRoot("staticNodeBarrier", "staticNodeBarrier");
141: String methodExpression = "* " + testClassName + "*.*(..)";
142: config.addWriteAutolock(methodExpression);
143:
144: spec = config.getOrCreateSpec(SharedModel.class.getName());
145: spec.addDistributedMethodCall("staticMethod", "()V", true);
146: try {
147: spec.addDistributedMethodCall("<init>", "()V", true);
148: throw new AssertionError(
149: "Should have thrown an AssertionError.");
150: } catch (AssertionError e) {
151: // Expected.
152: }
153:
154: config
155: .addDistributedMethodCall(new DistributedMethodSpec(
156: "* com.tctest.DistributedMethodCallExpressionTestApp$SharedModel.nonStaticMethod(..)",
157: true));
158: } catch (Exception e) {
159: throw new AssertionError(e);
160: }
161: }
162: }
|