KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > CyclicBarrierTestApp


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 com.tc.object.config.ConfigVisitor;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tc.util.Assert;
13 import com.tctest.runner.AbstractTransparentApp;
14
15 import java.util.concurrent.BrokenBarrierException JavaDoc;
16 import java.util.concurrent.CyclicBarrier JavaDoc;
17 import java.util.concurrent.TimeUnit JavaDoc;
18 import java.util.concurrent.TimeoutException JavaDoc;
19
20 public class CyclicBarrierTestApp extends AbstractTransparentApp {
21   private static final int FINISHED_BARRIER_ACTION_FLAG = 100;
22
23   private final DataRoot dataRoot = new DataRoot();
24   private final CyclicBarrier JavaDoc barrier;
25   private final CyclicBarrier JavaDoc testBarrier;
26
27   public CyclicBarrierTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
28     super(appId, cfg, listenerProvider);
29     barrier = new CyclicBarrier JavaDoc(getParticipantCount());
30     testBarrier = new CyclicBarrier JavaDoc(getParticipantCount(), new BarrierAction(dataRoot));
31   }
32
33   public void run() {
34     try {
35       basicBarrierActionTest();
36       basicBarrierAPITest();
37       interruptedBarrierTest();
38     } catch (Throwable JavaDoc t) {
39       notifyError(t);
40     }
41   }
42
43   private void clear() throws Exception JavaDoc {
44     synchronized (dataRoot) {
45       dataRoot.clear();
46     }
47
48     barrier.await();
49   }
50
51   private void basicBarrierActionTest() throws Exception JavaDoc {
52     testBarrier.await();
53
54     Assert.assertEquals(FINISHED_BARRIER_ACTION_FLAG, dataRoot.getData());
55
56     barrier.await();
57   }
58
59   private void basicBarrierAPITest() throws Exception JavaDoc {
60     Assert.assertEquals(getParticipantCount(), testBarrier.getParties());
61
62     clear();
63
64     int index = -1;
65     synchronized (dataRoot) {
66       index = dataRoot.getIndex();
67       dataRoot.setIndex(index + 1);
68     }
69
70     barrier.await();
71
72     if (index == 0) {
73       try {
74         Assert.assertEquals(0, testBarrier.getNumberWaiting());
75         testBarrier.await(10, TimeUnit.MILLISECONDS);
76       } catch (TimeoutException JavaDoc e) {
77         Assert.assertTrue(testBarrier.isBroken());
78       }
79     }
80
81     barrier.await();
82
83     if (index != 0) {
84       Assert.assertTrue(testBarrier.isBroken());
85     }
86
87     barrier.await();
88
89     if (index == 0) {
90       testBarrier.reset();
91     }
92
93     barrier.await();
94
95     Assert.assertFalse(testBarrier.isBroken());
96
97     testBarrier.await();
98   }
99
100   private void interruptedBarrierTest() throws Exception JavaDoc {
101     clear();
102
103     int index = -1;
104     synchronized (dataRoot) {
105       index = dataRoot.getIndex();
106       dataRoot.setIndex(index + 1);
107     }
108
109     barrier.await();
110
111     Thread JavaDoc thread = null;
112
113     if (index == 0) {
114       thread = new Thread JavaDoc(new BarrierRunnable(testBarrier));
115       thread.start();
116     }
117
118     if (index == 1) {
119       while (testBarrier.getNumberWaiting() == 0) {
120         // do nothing
121
}
122     }
123
124     barrier.await();
125
126     if (index == 0) {
127       thread.interrupt();
128     }
129
130     barrier.await();
131
132     if (index == 0) {
133       thread.join();
134     }
135
136     barrier.await();
137   }
138
139   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
140     String JavaDoc testClass = CyclicBarrierTestApp.class.getName();
141     TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
142
143     config.addIncludePattern(testClass + "$*", false, false, true);
144
145     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
146     config.addWriteAutolock(methodExpression);
147
148     spec.addRoot("barrier", "barrier");
149     spec.addRoot("testBarrier", "testBarrier");
150     spec.addRoot("dataRoot", "dataRoot");
151   }
152
153   private static class BarrierAction implements Runnable JavaDoc {
154     private final DataRoot dataRoot;
155
156     public BarrierAction(DataRoot dataRoot) {
157       this.dataRoot = dataRoot;
158     }
159
160     public void run() {
161       dataRoot.setData(FINISHED_BARRIER_ACTION_FLAG);
162     }
163   }
164
165   private class BarrierRunnable implements Runnable JavaDoc {
166     private CyclicBarrier JavaDoc b;
167
168     public BarrierRunnable(CyclicBarrier JavaDoc b) {
169       this.b = b;
170     }
171
172     public void run() {
173       try {
174         run0();
175       } catch (Throwable JavaDoc t) {
176         notifyError(t);
177       }
178     }
179
180     private void run0() {
181       try {
182         b.await();
183       } catch (InterruptedException JavaDoc e) {
184         Assert.assertTrue(b.isBroken());
185       } catch (BrokenBarrierException JavaDoc e) {
186         Assert.assertTrue(b.isBroken());
187       }
188     }
189   }
190
191   private static class DataRoot {
192     private int index;
193     private volatile int data;
194
195     public DataRoot() {
196       this.index = 0;
197     }
198
199     public int getIndex() {
200       return index;
201     }
202
203     public void setIndex(int index) {
204       this.index = index;
205     }
206
207     public int getData() {
208       return data;
209     }
210
211     public void setData(int data) {
212       this.data = data;
213     }
214
215     public void clear() {
216       this.index = 0;
217       this.data = 0;
218     }
219   }
220 }
221
Popular Tags