KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > transparency > SubClassOfThreadGotRootTestApp


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

4 package com.tctest.transparency;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.DSOClientConfigHelper;
8 import com.tc.object.config.TransparencyClassSpec;
9 import com.tc.object.config.spec.CyclicBarrierSpec;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tc.util.Assert;
13 import com.tc.util.concurrent.ThreadUtil;
14 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Random JavaDoc;
20
21 public class SubClassOfThreadGotRootTestApp extends AbstractErrorCatchingTransparentApp {
22
23   private static final int WORKER_COUNT = 5;
24   private static final int WORK_COUNT = 10;
25
26   private static final int CREATED_WORKERS = 0;
27   private static final int CREATED_WORK = 1;
28   private static final int WORK_DONE = 2;
29   private static final int VERIFIED = 2;
30
31   private ArrayList JavaDoc workers = new ArrayList JavaDoc();
32
33   public SubClassOfThreadGotRootTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
34     super(appId, cfg, listenerProvider);
35     Assert.assertTrue(getParticipantCount() == 2);
36   }
37
38   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
39     String JavaDoc workerClass = SubClassOfThreadGotRootTestApp.Worker.class.getName();
40     String JavaDoc workClass = SubClassOfThreadGotRootTestApp.Work.class.getName();
41     TransparencyClassSpec spec = config.getOrCreateSpec(workerClass);
42     String JavaDoc workerMethodExpression = "* " + workerClass + "*.*(..)";
43     String JavaDoc workMethodExpression = "* " + workClass + "*.*(..)";
44     config.addWriteAutolock(workerMethodExpression);
45     config.addWriteAutolock(workMethodExpression);
46     spec.addRoot("workQueue", "workQueue");
47
48     CyclicBarrierSpec cbspec = new CyclicBarrierSpec();
49     cbspec.visit(visitor, config);
50
51     // config.addExcludePattern("*..SubClassA");
52

53     // Include everything to be instrumented.
54
config.addIncludePattern("*..*", false);
55   }
56
57   protected void runTest() throws Throwable JavaDoc {
58
59     for (int i = 0; i < WORKER_COUNT; i++) {
60       Worker worker = new Worker(getApplicationId(), i);
61       workers.add(worker);
62       worker.start();
63     }
64
65     moveToStageAndWait(CREATED_WORKERS);
66
67     Random JavaDoc r = new Random JavaDoc();
68
69     for (int i = 0; i < WORKER_COUNT; i++) {
70       Worker w = (Worker) workers.get(i);
71       for (int j = 0; j < WORK_COUNT; j++) {
72         w.addWork(new Work(getApplicationId(), r.nextInt(Integer.MAX_VALUE), r.nextInt(Integer.MAX_VALUE)));
73       }
74     }
75
76     moveToStageAndWait(CREATED_WORK);
77
78     for (int i = 0; i < WORKER_COUNT; i++) {
79       Worker w = (Worker) workers.get(i);
80       w.waitToCompleteAndStop();
81     }
82
83     moveToStageAndWait(WORK_DONE);
84
85     for (int i = 0; i < WORKER_COUNT; i++) {
86       Worker w = (Worker) workers.get(i);
87       List JavaDoc workDone = w.getWorkDone();
88       System.err.println(Thread.currentThread().getName() + " verifying " + workDone.size() + " works done by "
89                          + w.getName());
90       for (Iterator JavaDoc j = workDone.iterator(); j.hasNext();) {
91         Work work = (Work) j.next();
92         work.verify();
93       }
94     }
95     moveToStage(VERIFIED);
96
97   }
98
99   // This class has the root
100
public class Worker extends Thread JavaDoc {
101
102     List JavaDoc workQueue = new ArrayList JavaDoc();
103     List JavaDoc workDone = new ArrayList JavaDoc();
104
105     boolean stopRequested = false;
106     private final String JavaDoc appId;
107
108     public Worker(String JavaDoc appId, int i) {
109       super(appId + "," + i);
110       this.appId = appId;
111     }
112
113     public void run() {
114       Random JavaDoc r = new Random JavaDoc();
115       while (!stopRequested) {
116         Work work = getWork();
117         if (work != null) {
118           work.run();
119           workDone.add(work);
120         }
121         else if (!stopRequested) {
122           ThreadUtil.reallySleep(r.nextInt(2000) + 1);
123         }
124       }
125     }
126
127     private int getSize() {
128       synchronized (workQueue) {
129         return workQueue.size();
130       }
131     }
132
133     private Work getWork() {
134       synchronized (workQueue) {
135         while (workQueue.size() == 0 && !stopRequested) {
136           try {
137             workQueue.wait();
138           } catch (InterruptedException JavaDoc e) {
139             throw new RuntimeException JavaDoc(e);
140           }
141         }
142         if (stopRequested) return null;
143         workQueue.notifyAll();
144         for (Iterator JavaDoc iter = workQueue.iterator(); iter.hasNext();) {
145           Work work = (Work) iter.next();
146           if (!work.getAppId().equals(appId)) {
147             iter.remove();
148             return work;
149           }
150         }
151         System.err.println("I, " + getName() + " couldn't get any foreign work. Trying again - Size = " + getSize());
152         return null;
153       }
154     }
155
156     public void addWork(Work work) {
157       synchronized (workQueue) {
158         workQueue.add(work);
159         workQueue.notify();
160       }
161     }
162
163     public void waitToCompleteAndStop() {
164       synchronized (workQueue) {
165         while (workQueue.size() != 0) {
166           try {
167             workQueue.wait();
168           } catch (InterruptedException JavaDoc e) {
169             throw new RuntimeException JavaDoc(e);
170           }
171         }
172         stopRequested = true;
173         workQueue.notifyAll();
174       }
175     }
176
177     public List JavaDoc getWorkDone() {
178       return workDone;
179     }
180   }
181
182   // This is the Portable Object
183
public static class Work implements Runnable JavaDoc {
184
185     int n1, n2;
186     long add, sub, multi;
187     double div;
188
189     String JavaDoc workerName;
190     private final String JavaDoc appId;
191
192     public Work(String JavaDoc appId, int n1, int n2) {
193       this.appId = appId;
194       this.n1 = n1;
195       this.n2 = n2;
196     }
197
198     public String JavaDoc getAppId() {
199       return this.appId;
200     }
201
202     public synchronized void run() {
203       workerName = Thread.currentThread().getName();
204       System.err.println("Worker " + workerName + " working on " + this);
205       add = add();
206       sub = subtract();
207       multi = multiply();
208       div = divide();
209     }
210
211     private double divide() {
212       if (n2 != 0) {
213         return n1 / n2;
214       } else {
215         return Double.NaN;
216       }
217     }
218
219     private long multiply() {
220       return n1 * n2;
221     }
222
223     private long subtract() {
224       return n1 - n2;
225     }
226
227     private long add() {
228       return n1 + n2;
229     }
230
231     public synchronized void verify() {
232       Assert.assertEquals(add, add());
233       Assert.assertEquals(sub, subtract());
234       Assert.assertEquals(multi, multiply());
235       if (Double.isNaN(div)) {
236         Assert.assertTrue(Double.isNaN(divide()));
237       } else {
238         Assert.assertEquals(div, divide());
239       }
240     }
241
242     public String JavaDoc toString() {
243       return "Work(" + appId + "):{" + n1 + "," + n2 + "}";
244     }
245
246   }
247
248 }
249
Popular Tags