1 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.Arrays ; 16 import java.util.HashMap ; 17 import java.util.Map ; 18 import java.util.concurrent.CyclicBarrier ; 19 20 public class EnumTestApp extends AbstractTransparentApp { 21 22 private final DataRoot dataRoot = new DataRoot(); 23 private final CyclicBarrier barrier; 24 private final Map raceRoot = new HashMap (); 25 26 private State stateRoot; 27 28 public EnumTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 29 super(appId, cfg, listenerProvider); 30 barrier = new CyclicBarrier (getParticipantCount()); 31 } 32 33 public void run() { 34 try { 35 int index = barrier.await(); 36 37 rootEnumTest(index); 38 39 if (index == 0) { 40 stateRoot = State.START; 41 } 42 43 barrier.await(); 44 45 Assert.assertTrue(stateRoot == State.START); 46 47 barrier.await(); 48 49 if (index == 1) { 50 stateRoot = State.RUN; 51 } 52 53 barrier.await(); 54 55 Assert.assertTrue(stateRoot == State.RUN); 56 57 barrier.await(); 58 59 for (int i = 0; i < 100; i++) { 60 if (index == 0) { 61 stateRoot = State.START; 62 } else { 63 stateRoot = State.RUN; 64 } 65 } 66 67 barrier.await(); 68 69 if (index == 0) { 70 dataRoot.setState(State.START); 71 } 72 73 barrier.await(); 74 75 Assert.assertTrue(dataRoot.getState() == State.START); 76 Assert.assertEquals(0, dataRoot.getState().getStateNum()); 77 78 barrier.await(); 79 80 if (index == 1) { 81 dataRoot.setState(State.RUN); 82 dataRoot.getState().setStateNum(10); 83 } 84 85 barrier.await(); 86 87 Assert.assertTrue(dataRoot.getState() == State.RUN); 88 if (index == 1) { 89 Assert.assertEquals(10, dataRoot.getState().getStateNum()); 90 } else { 91 Assert.assertEquals(1, dataRoot.getState().getStateNum()); 93 } 94 95 barrier.await(); 96 97 if (index == 0) { 98 dataRoot.setState(State.STOP); 99 } 100 101 barrier.await(); 102 103 Assert.assertTrue(dataRoot.getState() == State.STOP); 104 Assert.assertEquals(2, dataRoot.getState().getStateNum()); 105 106 barrier.await(); 107 108 if (index == 0) { 109 dataRoot.setStates(State.values().clone()); 110 } 111 112 barrier.await(); 113 114 Assert.assertEquals(3, dataRoot.getStates().length); 115 Assert.assertTrue(Arrays.equals(State.values(), dataRoot.getStates())); 116 117 if (index == 0) { 118 testRace(); 119 } 120 121 if (index == 0) { 122 testRace(); 123 } 124 125 } catch (Throwable t) { 126 notifyError(t); 127 } 128 } 129 130 enum EnumForRace { 132 V1, V2, V3; 133 } 134 135 private static class Ref { 136 private final EnumForRace e; 137 138 Ref(EnumForRace e) { 139 this.e = e; 140 } 141 142 public String toString() { 143 return "ref(" + e + ")"; 144 } 145 } 146 147 private void testRace() throws InterruptedException { 148 final Object lock1 = new Object (); 149 final Object lock2 = new Object (); 150 151 synchronized (raceRoot) { 152 raceRoot.put("lock1", lock1); 153 raceRoot.put("lock1", lock2); 154 } 155 156 final CyclicBarrier cb = new CyclicBarrier (2); 157 final EnumForRace enumInstance = EnumForRace.V1; 158 159 Thread other = new Thread () { 160 public void run() { 161 shareWithLock(lock1, new Ref(enumInstance), raceRoot, cb); 162 } 163 }; 164 other.start(); 165 166 shareWithLock(lock2, new Ref(enumInstance), raceRoot, cb); 167 168 other.join(); 169 } 170 171 private static void shareWithLock(Object lock, Object toShare, Map root, CyclicBarrier barrier) { 172 synchronized (lock) { 173 root.put(String.valueOf(System.identityHashCode(lock)), toShare); 174 175 try { 176 barrier.await(); 177 } catch (Exception e) { 178 throw new RuntimeException (e); 179 } 180 } 181 } 182 183 private void rootEnumTest(int index) throws Exception { 184 if (index == 0) { 185 stateRoot = State.START; 186 } 187 188 barrier.await(); 189 190 Assert.assertEquals(State.START, stateRoot); 191 192 barrier.await(); 193 194 if (index == 1) { 195 stateRoot = State.RUN; 196 } 197 198 barrier.await(); 199 200 Assert.assertEquals(State.RUN, stateRoot); 201 202 barrier.await(); 203 } 204 205 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 206 String testClass = EnumTestApp.class.getName(); 207 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 208 209 config.addIncludePattern(testClass + "$DataRoot*"); 210 211 String methodExpression = "* " + testClass + "*.*(..)"; 212 config.addWriteAutolock(methodExpression); 213 214 methodExpression = "* " + testClass + "$DataRoot*.*(..)"; 215 config.addWriteAutolock(methodExpression); 216 217 spec.addRoot("barrier", "barrier"); 218 spec.addRoot("dataRoot", "dataRoot"); 219 spec.addRoot("stateRoot", "stateRoot", false); 220 spec.addRoot("raceRoot", "raceRoot"); 221 222 config.addIncludePattern(EnumForRace.class.getName()); 225 config.addIncludePattern(Ref.class.getName()); 226 } 227 228 public enum State { 229 START(0), RUN(1), STOP(2); 230 231 private int stateNum; 232 233 State(int stateNum) { 234 this.stateNum = stateNum; 235 } 236 237 int getStateNum() { 238 return this.stateNum; 239 } 240 241 void setStateNum(int stateNum) { 242 this.stateNum = stateNum; 243 } 244 } 245 246 private static class DataRoot { 247 private State state; 248 private State states[]; 249 250 public DataRoot() { 251 super(); 252 } 253 254 public synchronized void setState(State state) { 255 this.state = state; 256 } 257 258 public synchronized State getState() { 259 return state; 260 } 261 262 public synchronized State[] getStates() { 263 return states; 264 } 265 266 public synchronized void setStates(State[] states) { 267 this.states = states; 268 } 269 270 } 271 } 272
| Popular Tags
|