KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > DistributedMethodCallExpressionTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest;
6
7 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
8 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
9
10 import com.tc.object.config.ConfigVisitor;
11 import com.tc.object.config.DSOClientConfigHelper;
12 import com.tc.object.config.DistributedMethodSpec;
13 import com.tc.object.config.TransparencyClassSpec;
14 import com.tc.object.config.spec.CyclicBarrierSpec;
15 import com.tc.object.config.spec.SynchronizedIntSpec;
16 import com.tc.simulator.app.ApplicationConfig;
17 import com.tc.simulator.listener.ListenerProvider;
18 import com.tctest.runner.AbstractTransparentApp;
19
20 /**
21  * This test is to test the various method expressions for DistributedMethodCall. The test will also make sure that
22  * constructors and static methods will not be called.
23  */

24 public class DistributedMethodCallExpressionTestApp extends AbstractTransparentApp {
25
26   private final SharedModel model = new SharedModel();
27   private final int initialNodeCount = getParticipantCount();
28   private final CyclicBarrier nodeBarrier = new CyclicBarrier(initialNodeCount);
29   private final CyclicBarrier staticNodeBarrier = new CyclicBarrier(initialNodeCount);
30
31   public DistributedMethodCallExpressionTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
32     super(appId, cfg, listenerProvider);
33   }
34
35   public void run() {
36     try {
37       callNonStaticMethod();
38       callStaticMethod();
39     } catch (Throwable JavaDoc e) {
40       notifyError(e);
41     }
42   }
43
44   public void callNonStaticMethod() throws Throwable JavaDoc {
45     final boolean masterNode = nodeBarrier.barrier() == 0;
46     synchronized (model) {
47       if (masterNode) {
48         model.nonStaticMethod(null, 0, 0, null, null, false);
49       }
50     }
51     nodeBarrier.barrier();
52     System.err.println("\n### initialNodeCount = " + initialNodeCount);
53     synchronized (model) {
54       checkCountTimed(model.nonStaticCallCount, initialNodeCount, 10, 5 * 1000, "Non-Static Call Count");
55     }
56     nodeBarrier.barrier();
57   }
58
59   public void callStaticMethod() throws Throwable JavaDoc {
60     final boolean masterNode = staticNodeBarrier.barrier() == 0;
61     synchronized (model) {
62       if (masterNode) {
63         SharedModel.staticMethod();
64       }
65     }
66     staticNodeBarrier.barrier();
67     System.err.println("### -> sleeping before checking static method calls");
68     Thread.sleep(30000);
69     synchronized (model) {
70       checkCountTimed(SharedModel.staticCallCount, (masterNode) ? 1 : 0, 1, 10000, "Static Call Count");
71     }
72     staticNodeBarrier.barrier();
73   }
74
75   public static class SharedModel {
76     public final SynchronizedInt nonStaticCallCount = new SynchronizedInt(0);
77     public static final SynchronizedInt staticCallCount = new SynchronizedInt(0);
78
79     public static void staticMethod() {
80       staticCallCount.increment();
81     }
82
83     public void nonStaticMethod(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
84         throws Throwable JavaDoc {
85       nonStaticCallCount.increment();
86     }
87   }
88
89   private void checkCountTimed(SynchronizedInt actualSI, final int expected, final int slices, final long sliceMillis,
90                                String JavaDoc msg) throws InterruptedException JavaDoc {
91     // wait until all nodes have the right picture of the cluster
92
int actual = 0;
93     int i;
94     for (i = 0; i < slices; i++) {
95       actual = actualSI.get();
96       if (actual > expected || actual < 0) {
97         notifyError("Wrong Count: expected=" + expected + ", actual=" + actual);
98       }
99       if (actual < expected) {
100         Thread.sleep(sliceMillis);
101       } else {
102         break;
103       }
104     }
105     if (i == slices) {
106       notifyError("Wrong Count: expected=" + expected + ", actual=" + actual);
107     }
108     System.err.println("\n### -> check '" + msg + "' passed in " + i + " slices");
109   }
110
111   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
112     try {
113       new CyclicBarrierSpec().visit(visitor, config);
114       new SynchronizedIntSpec().visit(visitor, config);
115
116       TransparencyClassSpec spec = config.getOrCreateSpec(FooObject.class.getName());
117       String JavaDoc testClassName = DistributedMethodCallExpressionTestApp.class.getName();
118       spec = config.getOrCreateSpec(testClassName);
119       spec.addRoot("model", "model");
120       spec.addRoot("nodeBarrier", "nodeBarrier");
121       spec.addRoot("staticNodeBarrier", "staticNodeBarrier");
122       String JavaDoc methodExpression = "* " + testClassName + "*.*(..)";
123       config.addWriteAutolock(methodExpression);
124
125       spec = config.getOrCreateSpec(SharedModel.class.getName());
126       spec.addDistributedMethodCall("staticMethod", "()V", true);
127       try {
128         spec.addDistributedMethodCall("<init>", "()V", true);
129         throw new AssertionError JavaDoc("Should have thrown an AssertionError.");
130       } catch (AssertionError JavaDoc e) {
131         // Expected.
132
}
133       
134       config
135           .addDistributedMethodCall(new DistributedMethodSpec("* com.tctest.DistributedMethodCallExpressionTestApp$SharedModel.nonStaticMethod(..)", true));
136     } catch (Exception JavaDoc e) {
137       throw new AssertionError JavaDoc(e);
138     }
139   }
140 }
141
Popular Tags