KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > DistributedMethodCallTestApp


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.TransparencyClassSpec;
13 import com.tc.object.config.spec.CyclicBarrierSpec;
14 import com.tc.object.config.spec.SynchronizedIntSpec;
15 import com.tc.simulator.app.ApplicationConfig;
16 import com.tc.simulator.listener.ListenerProvider;
17 import com.tctest.runner.AbstractTransparentApp;
18
19 import java.util.Arrays JavaDoc;
20
21 public class DistributedMethodCallTestApp extends AbstractTransparentApp {
22
23   private SharedModel model = new SharedModel();
24   private final CyclicBarrier sharedBarrier = new CyclicBarrier(getParticipantCount());
25   private final CyclicBarrier sharedBarrier2 = new CyclicBarrier(getParticipantCount());
26
27   public DistributedMethodCallTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
28     super(appId, cfg, listenerProvider);
29   }
30
31   public void run() {
32     try {
33       runTest();
34       runTestWithNulls();
35       runTestNested();
36       runTestNonVoid();
37       runTestWithParamChange();
38 // runTestWithSynchAndNested();
39
} catch (Throwable JavaDoc e) {
40       notifyError(e);
41     }
42   }
43
44   private void runTest() throws Throwable JavaDoc {
45     final boolean callInitiator = sharedBarrier.barrier() == 0;
46
47     if (callInitiator) {
48       model.callCount.set(0);
49       synchronized (model) {
50         FooObject[][] foos = makeFooArray();
51         int[][][] ints = makeIntArray();
52         model.addObject(new FooObject(), 1, 2, foos, ints, true);
53       }
54     }
55     sharedBarrier.barrier();
56     final int actual = model.callCount.get();
57     if (actual != getParticipantCount()) {
58       notifyError("Unexpected call count: expected=" + getParticipantCount() + ", actual=" + actual);
59     }
60   }
61
62   private void runTestWithParamChange() throws Throwable JavaDoc {
63     final boolean callInitiator = sharedBarrier.barrier() == 0;
64
65     if (callInitiator) {
66       model.callCount.set(0);
67       synchronized (model) {
68         FooObject[][] foos = makeFooArray();
69         int[][][] ints = makeIntArray();
70         model.addObjectWithParamChange(new FooObject(), 1, 2, foos, ints, true);
71       }
72     }
73     sharedBarrier.barrier();
74     final int actual = model.callCount.get();
75     if (actual != getParticipantCount()) {
76       notifyError("Unexpected call count: expected=" + getParticipantCount() + ", actual=" + actual);
77     }
78   }
79
80   private void runTestNonVoid() throws Throwable JavaDoc {
81     final boolean callInitiator = sharedBarrier.barrier() == 0;
82
83     if (callInitiator) {
84       model.callCount.set(0);
85       synchronized (model) {
86         FooObject[][] foos = makeFooArray();
87         int[][][] ints = makeIntArray();
88         model.addObjectNonVoid(new FooObject(), 1, 2, foos, ints, true);
89       }
90     }
91     sharedBarrier.barrier();
92     final int actual = model.callCount.get();
93     if (actual != getParticipantCount()) {
94       notifyError("Unexpected call count: expected=" + getParticipantCount() + ", actual=" + actual);
95     }
96   }
97
98   private void runTestNested() throws Throwable JavaDoc {
99     final boolean callInitiator = sharedBarrier.barrier() == 0;
100
101     if (callInitiator) {
102       model.callCount.set(0);
103       synchronized (model) {
104         FooObject[][] foos = makeFooArray();
105         int[][][] ints = makeIntArray();
106         model.addObjectNested(new FooObject(), 1, 2, foos, ints, true);
107       }
108     }
109     sharedBarrier.barrier();
110     final int actual = model.callCount.get();
111     if (actual != getParticipantCount()) {
112       notifyError("Unexpected call count: expected=" + getParticipantCount() + ", actual=" + actual);
113     }
114   }
115
116   private void runTestWithNulls() throws Throwable JavaDoc {
117     final boolean callInitiator = sharedBarrier.barrier() == 0;
118
119     if (callInitiator) {
120       model.callCount.set(0);
121       synchronized (model) {
122         model.addObjectWithNulls(null, 1, 2, null, null, true);
123       }
124     }
125     sharedBarrier.barrier();
126     final int actual = model.callCount.get();
127     if (actual != getParticipantCount()) {
128       notifyError("Unexpected call count: expected=" + getParticipantCount() + ", actual=" + actual);
129     }
130   }
131
132   private static int[][][] makeIntArray() {
133     int[][][] ints = new int[6][8][9];
134     int count = 0;
135     for (int i = 0; i < ints.length; i++) {
136       int[][] array1 = ints[i];
137       for (int j = 0; j < array1.length; j++) {
138         int[] array2 = array1[j];
139         for (int k = 0; k < array2.length; k++) {
140           array2[k] = count++;
141         }
142       }
143     }
144     return ints;
145   }
146
147   private static FooObject[][] makeFooArray() {
148     FooObject[][] foos = new FooObject[2][3];
149     for (int i = 0; i < foos.length; i++) {
150       Arrays.fill(foos[i], new FooObject());
151     }
152     return foos;
153   }
154
155   public class SharedModel {
156     public final SynchronizedInt callCount = new SynchronizedInt(0);
157
158     public synchronized void addObjectSynched(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
159         throws Throwable JavaDoc {
160       this.addObjectSynched(obj, i, d, foos, ints, b);
161     }
162
163     public String JavaDoc addObjectNonVoid(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
164         throws Throwable JavaDoc {
165       addObject(obj, i, d, foos, ints, b);
166       return new String JavaDoc("A-OK");
167     }
168
169     public void addObject(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b) throws Throwable JavaDoc {
170       callCount.increment();
171       // Everything in the "foos" array should be non-null
172
for (int index = 0; index < foos.length; index++) {
173         FooObject[] array = foos[index];
174         for (int j = 0; j < array.length; j++) {
175           FooObject foo = array[j];
176           if (foo == null) notifyError("foo == null");
177         }
178       }
179
180       // access all the "ints"
181
int count = 0;
182       for (int index = 0; index < ints.length; index++) {
183         int[][] array1 = ints[index];
184         for (int j = 0; j < array1.length; j++) {
185           int[] array2 = array1[j];
186           for (int k = 0; k < array2.length; k++) {
187             int val = array2[k];
188             if (count++ != val) notifyError("count ++ != val");
189           }
190         }
191       }
192
193       checkLiteralParams(i, d, b);
194       sharedBarrier2.barrier();
195     }
196
197     public void addObjectWithParamChange(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
198         throws Throwable JavaDoc {
199       callCount.increment();
200       // Everything in the "foos" array should be non-null
201
for (int index = 0; index < foos.length; index++) {
202         FooObject[] array = foos[index];
203         for (int j = 0; j < array.length; j++) {
204           FooObject foo = array[j];
205           if (foo == null) notifyError("foo == null");
206         }
207       }
208
209       // access all the "ints"
210
int count = 0;
211       for (int index = 0; index < ints.length; index++) {
212         int[][] array1 = ints[index];
213         for (int j = 0; j < array1.length; j++) {
214           int[] array2 = array1[j];
215           for (int k = 0; k < array2.length; k++) {
216             int val = array2[k];
217             if (count++ != val) notifyError("count ++ != val");
218           }
219         }
220       }
221       checkLiteralParams(i, d, b);
222       // now re-assign all params...
223
obj = null;
224       i = -777;
225       foos = null;
226       ints = null;
227       b = !b;
228       sharedBarrier2.barrier();
229     }
230
231     public void addObjectWithNulls(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
232         throws Throwable JavaDoc {
233       callCount.increment();
234       // all params should be nulls
235
checkReferenceParams(obj, foos, ints, true);
236       checkLiteralParams(i, d, b);
237       sharedBarrier2.barrier();
238     }
239
240     public void addObjectNested(Object JavaDoc obj, int i, double d, FooObject[][] foos, int[][][] ints, boolean b)
241         throws Throwable JavaDoc {
242       addObject(obj, i, d, foos, ints, b);
243     }
244
245     private void checkLiteralParams(int i, double d, boolean b) {
246       if (i != 1 || d != 2 || !b) {
247         System.out.println("Invalid parameters: i:" + i + " d:" + d + " b:" + b);
248         notifyError("Invalid parameters: i:" + i + " d:" + d + " b:" + b);
249       }
250     }
251
252     private void checkReferenceParams(Object JavaDoc o, FooObject[][] foos, int[][][] ints, boolean nullExpected) {
253       checkNull(o, nullExpected);
254       checkNull(foos, nullExpected);
255       checkNull(ints, nullExpected);
256     }
257
258     private void checkNull(Object JavaDoc o, boolean nullExpected) {
259       final boolean isNull = o == null;
260       if (isNull != nullExpected) notifyError("Wrong parameter value: null is expected, actual = " + o);
261     }
262   }
263
264   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
265     try {
266       new CyclicBarrierSpec().visit(visitor, config);
267       new SynchronizedIntSpec().visit(visitor, config);
268
269       TransparencyClassSpec spec = config.getOrCreateSpec(FooObject.class.getName());
270       String JavaDoc testClassName = DistributedMethodCallTestApp.class.getName();
271       spec = config.getOrCreateSpec(testClassName);
272       spec.addTransient("callInitiator");
273       spec.addRoot("model", "model");
274       spec.addRoot("sharedStuff", "sharedStuff");
275       spec.addRoot("sharedBarrier", "sharedBarrier");
276       spec.addRoot("sharedBarrier2", "sharedBarrier2");
277       String JavaDoc methodExpression = "* " + testClassName + "*.*(..)";
278       config.addWriteAutolock(methodExpression);
279       // methodExpression = "* " + DistributedMethodCallTestApp.SharedModel.class.getName() + "*.*(..)";
280
// config.addWriteAutolock(methodExpression);
281

282       spec = config.getOrCreateSpec(SharedModel.class.getName());
283       spec.addDistributedMethodCall("addObjectNonVoid",
284                                     "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)java/lang/String;", true);
285       spec.addDistributedMethodCall("addObjectWithNulls", "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)V", true);
286       spec
287           .addDistributedMethodCall("addObjectWithParamChange", "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)V", true);
288       spec.addDistributedMethodCall("addObjectSynched", "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)V", true);
289       spec.addDistributedMethodCall("addObjectNested", "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)V", true);
290       spec.addDistributedMethodCall("addObject", "(Ljava/lang/Object;ID[[Lcom/tctest/FooObject;[[[IZ)V", true);
291     } catch (Exception JavaDoc e) {
292       throw new AssertionError JavaDoc(e);
293     }
294   }
295 }
296
Popular Tags