KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > InterruptTestApp


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
9 import com.tc.object.config.ConfigVisitor;
10 import com.tc.object.config.DSOClientConfigHelper;
11 import com.tc.object.config.TransparencyClassSpec;
12 import com.tc.simulator.app.ApplicationConfig;
13 import com.tc.simulator.listener.ListenerProvider;
14 import com.tc.util.Assert;
15 import com.tctest.runner.AbstractTransparentApp;
16
17 public class InterruptTestApp extends AbstractTransparentApp {
18   private volatile boolean interruptedFlag = false;
19   private final Object JavaDoc lockObject = new Object JavaDoc();
20   private final SharedData sharedData = new SharedData();
21
22   private final CyclicBarrier barrier;
23
24   public InterruptTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
25     super(appId, cfg, listenerProvider);
26     barrier = new CyclicBarrier(getParticipantCount());
27   }
28
29   public void run() {
30     try {
31       int index = barrier.barrier();
32       testWaitInterrupt1(index);
33       testWaitInterrupt2(index);
34       testWaitInterrupt3(index);
35       testWaitInterrupt4(index);
36     } catch (Throwable JavaDoc t) {
37       notifyError(t);
38     }
39   }
40
41   private void testWaitInterrupt1(int index) throws Exception JavaDoc {
42     if (index == 0) {
43       final CyclicBarrier localBarrier = new CyclicBarrier(2);
44       Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
45         public void run() {
46           try {
47             synchronized (lockObject) {
48               localBarrier.barrier();
49               lockObject.wait();
50               throw new AssertionError JavaDoc("Should have thrown an InterruptedException.");
51             }
52           } catch (InterruptedException JavaDoc e) {
53             interruptedFlag = true;
54           } catch (Throwable JavaDoc e) {
55             notifyError(e);
56           }
57         }
58       });
59       t.start();
60       localBarrier.barrier();
61       t.interrupt();
62       while (!interruptedFlag) {
63         // do nothing
64
}
65       //while (!interruptedFlag && t.isAlive()) {
66
// t.interrupt();
67
// }
68
Assert.assertTrue(interruptedFlag);
69       interruptedFlag = false;
70     }
71     barrier.barrier();
72   }
73
74   private void testWaitInterrupt2(int index) throws Exception JavaDoc {
75     if (index == 0) {
76       final CyclicBarrier localBarrier = new CyclicBarrier(2);
77       Thread JavaDoc t1 = new Thread JavaDoc(new Runnable JavaDoc() {
78         public void run() {
79           synchronized (lockObject) {
80             try {
81               localBarrier.barrier();
82               lockObject.wait();
83               throw new AssertionError JavaDoc("Should have thrown an InterruptedException.");
84             } catch (InterruptedException JavaDoc e) {
85               interruptedFlag = true;
86             } catch (Throwable JavaDoc e) {
87               notifyError(e);
88             }
89           }
90         }
91       });
92       Thread JavaDoc t2 = new Thread JavaDoc(new Runnable JavaDoc() {
93         public void run() {
94           synchronized(lockObject) {
95             try {
96               localBarrier.barrier();
97               lockObject.wait();
98             } catch (InterruptedException JavaDoc ie) {
99               interruptedFlag = true;
100             } catch (Throwable JavaDoc t) {
101               notifyError(t);
102             }
103           }
104         }
105       });
106       t1.start();
107       localBarrier.barrier();
108       t2.start();
109       localBarrier.barrier();
110       t1.interrupt();
111       while (!interruptedFlag) {
112         // do nothing
113
}
114       //while (!interruptedFlag && t1.isAlive()) {
115
// t1.interrupt();
116
//}
117
interruptedFlag = false;
118       t2.interrupt();
119       while (!interruptedFlag) {
120         // do nothing
121
}
122       //while (!interruptedFlag && t2.isAlive()) {
123
// t2.interrupt();
124
//}
125
interruptedFlag = false;
126     }
127     barrier.barrier();
128   }
129   
130   private void testWaitInterrupt3(int index) throws Exception JavaDoc {
131     System.err.println("Running test 3");
132     if (index == 0) {
133       final CyclicBarrier localBarrier = new CyclicBarrier(2);
134       Thread JavaDoc t1 = new Thread JavaDoc(new Runnable JavaDoc() {
135         public void run() {
136           synchronized (lockObject) {
137             try {
138               localBarrier.barrier();
139               lockObject.wait();
140               throw new AssertionError JavaDoc("Should have thrown an InterruptedException.");
141             } catch (InterruptedException JavaDoc e) {
142               interruptedFlag = true;
143             } catch (Throwable JavaDoc e) {
144               notifyError(e);
145             }
146           }
147         }
148       });
149       Thread JavaDoc t2 = new Thread JavaDoc(new Runnable JavaDoc() {
150         public void run() {
151           synchronized(lockObject) {
152             try {
153               localBarrier.barrier();
154               Thread.sleep(10000);
155             } catch (Throwable JavaDoc t) {
156               notifyError(t);
157             }
158           }
159         }
160       });
161       t1.start();
162       localBarrier.barrier();
163       t2.start();
164       localBarrier.barrier();
165       t1.interrupt();
166       while (!interruptedFlag) {
167         // do nothing
168
}
169       //while (!interruptedFlag && t1.isAlive()) {
170
// t1.interrupt();
171
//}
172
interruptedFlag = false;
173     }
174     barrier.barrier();
175   }
176   
177   /**
178    * This test involves 2 nodes. One node goes to wait, while a remote node grabs the lock. The first node
179    * get interrupted, wait for the other node to release the lock and grabs the lock back.
180    */

181   private void testWaitInterrupt4(int index) throws Exception JavaDoc {
182     if (index == 0) {
183       final CyclicBarrier localBarrier = new CyclicBarrier(2);
184       Thread JavaDoc t1 = new Thread JavaDoc(new Runnable JavaDoc() {
185         public void run() {
186           synchronized (lockObject) {
187             try {
188               localBarrier.barrier();
189               lockObject.wait();
190               throw new AssertionError JavaDoc("Should have thrown an InterruptedException.");
191             } catch (InterruptedException JavaDoc e) {
192               interruptedFlag = true;
193             } catch (Throwable JavaDoc e) {
194               notifyError(e);
195             }
196             Assert.assertEquals(10, sharedData.getData());
197             sharedData.setData(20);
198           }
199         }
200       });
201       t1.start();
202       localBarrier.barrier();
203       barrier.barrier();
204       barrier.barrier();
205       t1.interrupt();
206       while (!interruptedFlag) {
207         // do nothing
208
}
209       //while (!interruptedFlag && t1.isAlive()) {
210
// t1.interrupt();
211
//}
212
interruptedFlag = false;
213     } else {
214       barrier.barrier();
215       synchronized(lockObject) {
216         barrier.barrier();
217         Thread.sleep(10000);
218         sharedData.setData(10);
219       }
220     }
221     barrier.barrier();
222     
223     Assert.assertEquals(20, sharedData.getData());
224     
225     barrier.barrier();
226   }
227
228   public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
229     TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName());
230     config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)");
231
232     String JavaDoc testClass = InterruptTestApp.class.getName();
233     spec = config.getOrCreateSpec(testClass);
234
235     config.addIncludePattern(testClass + "$*");
236
237     String JavaDoc methodExpression = "* " + testClass + "*.*(..)";
238     config.addWriteAutolock(methodExpression);
239
240     spec.addRoot("barrier", "barrier");
241     spec.addRoot("lockObject", "lockObject");
242     spec.addRoot("sharedData", "sharedData");
243   }
244   
245   private static class SharedData {
246     private int data;
247
248     public int getData() {
249       return data;
250     }
251
252     public void setData(int data) {
253       this.data = data;
254     }
255   }
256 }
257
Popular Tags