1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedRef; 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.object.config.spec.CyclicBarrierSpec; 13 import com.tc.simulator.app.ApplicationConfig; 14 import com.tc.simulator.listener.ListenerProvider; 15 import com.tc.util.Assert; 16 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 import java.util.Map.Entry; 22 23 public class NewObjectCreationRaceTest extends TransparentTestBase { 24 25 private static final int NODE_COUNT = 3; 26 27 public void setUp() throws Exception { 28 super.setUp(); 29 getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1); 30 initializeTestRunner(); 31 } 32 33 protected Class getApplicationClass() { 34 return NewObjectCreationRaceTestApp.class; 35 } 36 37 public static class NewObjectCreationRaceTestApp extends AbstractErrorCatchingTransparentApp { 38 39 private Map root; 42 43 private final CyclicBarrier barrier; 44 45 public NewObjectCreationRaceTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 46 super(appId, cfg, listenerProvider); 47 48 if (getParticipantCount() < 2) { throw new AssertionError ("need at least two nodes for this test to work"); } 49 50 barrier = new CyclicBarrier(getParticipantCount()); 51 } 52 53 protected void runTest() throws Throwable { 54 int n = barrier.barrier(); 55 56 if (n == 0) { 57 runCreateNode(); 58 } else { 59 runFaultNode(); 60 } 61 } 62 63 private void runFaultNode() throws Exception { 64 barrier.barrier(); 65 66 final Map faultedRoot; 67 68 synchronized (barrier) { 71 faultedRoot = root; 72 } 73 74 for (Iterator i = faultedRoot.entrySet().iterator(); i.hasNext();) { 75 Map.Entry entry = (Entry) i.next(); 76 Object key = entry.getKey(); 77 System.out.println(key); 78 Object value = entry.getValue(); 79 System.out.println(value); 80 81 if (value instanceof Ref) { 82 Object ref = ((Ref) value).getRef(); 83 System.out.println(ref); 84 } 85 } 86 87 barrier.barrier(); 89 90 barrier.barrier(); 92 93 synchronized (faultedRoot) { 95 Object o = faultedRoot.get("delta"); 96 Assert.assertEquals("non-null", o); 97 Assert.assertEquals(5, faultedRoot.size()); 98 } 99 } 100 101 private void runCreateNode() throws Throwable { 102 final SynchronizedRef error = new SynchronizedRef(null); 103 104 root = new HashMap (); 106 107 final Object newObj = new Object (); 108 final Ref newRefToOtherNewObject = new Ref(new Object ()); 109 110 synchronized (root) { 111 root.put("delta", null); 112 root.put("new object", newObj); 113 root.put("new ref to new obj", newRefToOtherNewObject); 114 115 Runnable otherTxn = new Runnable () { 116 public void run() { 117 try { 118 synchronized (barrier) { 119 root.put("ref to new with ref to created", new Ref(newObj)); 120 root.put("ref to created with ref to created", newRefToOtherNewObject); 121 } 122 } catch (Throwable err) { 123 error.set(err); 124 } 125 } 126 }; 127 128 Thread t1 = new Thread (otherTxn); 129 t1.start(); 130 t1.join(); 131 132 checkError(error); 133 134 Runnable doBarrier = new Runnable () { 137 public void run() { 138 try { 139 barrier.barrier(); 140 } catch (Throwable err) { 141 error.set(err); 142 } 143 } 144 }; 145 Thread t2 = new Thread (doBarrier); 146 t2.start(); 147 t2.join(); 148 149 checkError(error); 150 151 Thread t3 = new Thread (doBarrier); 154 t3.start(); 155 t3.join(); 156 157 checkError(error); 158 159 root.put("delta", "non-null"); 161 162 barrier.barrier(); 163 } 164 165 } 166 167 private void checkError(SynchronizedRef error) throws Throwable { 168 Throwable t = (Throwable ) error.get(); 169 if (t != null) { throw t; } 170 } 171 172 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 173 new CyclicBarrierSpec().visit(visitor, config); 174 175 String testClass; 176 TransparencyClassSpec spec; 177 String methodExpression; 178 179 testClass = Ref.class.getName(); 180 spec = config.getOrCreateSpec(testClass); 181 182 testClass = NewObjectCreationRaceTest.class.getName(); 183 spec = config.getOrCreateSpec(testClass); 184 methodExpression = "* " + testClass + "*.*(..)"; 185 config.addWriteAutolock(methodExpression); 186 187 config.addIncludePattern(testClass + "$*"); 188 189 testClass = NewObjectCreationRaceTestApp.class.getName(); 190 spec = config.getOrCreateSpec(testClass); 191 192 methodExpression = "* " + testClass + "*.*(..)"; 193 config.addWriteAutolock(methodExpression); 194 spec.addRoot("barrier", "barrier"); 195 spec.addRoot("root", "root"); 196 } 197 198 } 199 200 private static class Ref { 201 private final Object ref; 202 203 Ref(Object ref) { 204 this.ref = ref; 205 } 206 207 Object getRef() { 208 return ref; 209 } 210 } 211 212 } 213
| Popular Tags
|