1 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 lockObject = new Object (); 20 private final SharedData sharedData = new SharedData(); 21 22 private final CyclicBarrier barrier; 23 24 public InterruptTestApp(String 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 t) { 37 notifyError(t); 38 } 39 } 40 41 private void testWaitInterrupt1(int index) throws Exception { 42 if (index == 0) { 43 final CyclicBarrier localBarrier = new CyclicBarrier(2); 44 Thread t = new Thread (new Runnable () { 45 public void run() { 46 try { 47 synchronized (lockObject) { 48 localBarrier.barrier(); 49 lockObject.wait(); 50 throw new AssertionError ("Should have thrown an InterruptedException."); 51 } 52 } catch (InterruptedException e) { 53 interruptedFlag = true; 54 } catch (Throwable e) { 55 notifyError(e); 56 } 57 } 58 }); 59 t.start(); 60 localBarrier.barrier(); 61 t.interrupt(); 62 while (!interruptedFlag) { 63 } 65 Assert.assertTrue(interruptedFlag); 69 interruptedFlag = false; 70 } 71 barrier.barrier(); 72 } 73 74 private void testWaitInterrupt2(int index) throws Exception { 75 if (index == 0) { 76 final CyclicBarrier localBarrier = new CyclicBarrier(2); 77 Thread t1 = new Thread (new Runnable () { 78 public void run() { 79 synchronized (lockObject) { 80 try { 81 localBarrier.barrier(); 82 lockObject.wait(); 83 throw new AssertionError ("Should have thrown an InterruptedException."); 84 } catch (InterruptedException e) { 85 interruptedFlag = true; 86 } catch (Throwable e) { 87 notifyError(e); 88 } 89 } 90 } 91 }); 92 Thread t2 = new Thread (new Runnable () { 93 public void run() { 94 synchronized(lockObject) { 95 try { 96 localBarrier.barrier(); 97 lockObject.wait(); 98 } catch (InterruptedException ie) { 99 interruptedFlag = true; 100 } catch (Throwable 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 } 114 interruptedFlag = false; 118 t2.interrupt(); 119 while (!interruptedFlag) { 120 } 122 interruptedFlag = false; 126 } 127 barrier.barrier(); 128 } 129 130 private void testWaitInterrupt3(int index) throws Exception { 131 System.err.println("Running test 3"); 132 if (index == 0) { 133 final CyclicBarrier localBarrier = new CyclicBarrier(2); 134 Thread t1 = new Thread (new Runnable () { 135 public void run() { 136 synchronized (lockObject) { 137 try { 138 localBarrier.barrier(); 139 lockObject.wait(); 140 throw new AssertionError ("Should have thrown an InterruptedException."); 141 } catch (InterruptedException e) { 142 interruptedFlag = true; 143 } catch (Throwable e) { 144 notifyError(e); 145 } 146 } 147 } 148 }); 149 Thread t2 = new Thread (new Runnable () { 150 public void run() { 151 synchronized(lockObject) { 152 try { 153 localBarrier.barrier(); 154 Thread.sleep(10000); 155 } catch (Throwable 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 } 169 interruptedFlag = false; 173 } 174 barrier.barrier(); 175 } 176 177 181 private void testWaitInterrupt4(int index) throws Exception { 182 if (index == 0) { 183 final CyclicBarrier localBarrier = new CyclicBarrier(2); 184 Thread t1 = new Thread (new Runnable () { 185 public void run() { 186 synchronized (lockObject) { 187 try { 188 localBarrier.barrier(); 189 lockObject.wait(); 190 throw new AssertionError ("Should have thrown an InterruptedException."); 191 } catch (InterruptedException e) { 192 interruptedFlag = true; 193 } catch (Throwable 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 } 209 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 testClass = InterruptTestApp.class.getName(); 233 spec = config.getOrCreateSpec(testClass); 234 235 config.addIncludePattern(testClass + "$*"); 236 237 String 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 |