1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 8 import com.tc.object.config.ConfigVisitor; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.object.config.spec.CyclicBarrierSpec; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tc.util.Assert; 14 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 15 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Vector ; 21 22 25 public class TransparentVectorTestApp extends AbstractErrorCatchingTransparentApp { 26 27 public TransparentVectorTestApp(String globalId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 28 super(globalId, cfg, listenerProvider); 29 barrier = new CyclicBarrier(getParticipantCount()); 30 } 31 32 private static final int INITIAL_STAGE = 0; 33 private static final int ADD_COMPLETE_STAGE = 1; 34 private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2; 35 private static final int REMOVE_COMPLETE_STAGE = 3; 36 private static final int ASSERT_REMOVE_SIZE_STAGE = 4; 37 38 private final Vector vector = new Vector (); 39 41 private final CyclicBarrier barrier; 42 43 public void moveToStageAndWait(int stage) { 44 super.moveToStageAndWait(stage); 45 } 46 47 protected void runTest() throws Throwable { 48 int maxCount = getParticipantCount() * getIntensity(); 49 List testObjects = new Vector (); 50 moveToStage(INITIAL_STAGE); 51 for (int i = 0; i < getIntensity(); i++) { 52 TestObject to = new TestObject(getApplicationId(), i); 53 testObjects.add(to); 54 synchronized (vector) { 55 int size = vector.size(); 56 vector.add(to); 57 Assert.eval(vector.size() == size + 1); 58 } 59 } 60 moveToStageAndWait(ADD_COMPLETE_STAGE); 61 62 checkSize(maxCount); 63 64 moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE); 65 int removeCount = getIntensity() / 2; 66 for (int i = 0; i < removeCount; i++) { 67 synchronized (vector) { 68 int size = vector.size(); 69 Object toRemove = testObjects.get(i); 70 boolean wasRemoved = vector.remove(toRemove); 71 if (!wasRemoved) { 72 System.out.println("List:" + vector + " list hash:" + System.identityHashCode(vector)); 73 Assert.eval("Test object should have been removed but wasn't: " + testObjects.get(i), wasRemoved); 74 Assert.eval(vector.size() == size - 1); 75 } 76 } 77 } 78 79 moveToStageAndWait(REMOVE_COMPLETE_STAGE); 80 81 checkSize(maxCount - getParticipantCount() * removeCount); 82 83 moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE); 84 85 synchronized (vector) { 86 vector.clear(); 87 Assert.eval(vector.size() == 0); 88 } 89 90 checkSize(0); 91 92 int num = barrier.barrier(); 93 94 if (num == 0) { 95 synchronized (vector) { 96 vector.setSize(5); 97 } 98 } 99 100 barrier.barrier(); 101 102 checkSize(5); 103 104 for (int i = 0; i < 5; i++) { 105 Object val = vector.get(i); 106 if (val != null) { 107 notifyError("Expected null at index " + i + ", but found " + val); 108 return; 109 } 110 } 111 112 barrier.barrier(); 113 114 119 notifyResult(Boolean.TRUE); 120 } 121 122 private void checkSize(int s) { 123 synchronized (vector) { 124 if (vector.size() != s) { 125 Map res = new HashMap (); 126 for (Iterator i = vector.iterator(); i.hasNext();) { 127 TestObject to = (TestObject) i.next(); 128 String key = to.getId(); 129 if (!res.containsKey(key)) { 130 res.put(key, new Long (0)); 131 } else { 132 long v = ((Long ) res.get(key)).longValue(); 133 res.put(key, new Long (++v)); 134 } 135 } 136 throw new AssertionError ("" + res); 137 } 138 } 139 } 140 141 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 142 new CyclicBarrierSpec().visit(visitor, config); 143 String testClassName = TransparentVectorTestApp.class.getName(); 144 config.addRoot(testClassName, "vector", "vector", true); 145 config.addRoot(testClassName, "subList", "subList", true); 146 config.addRoot(testClassName, "barrier", "barrier", true); 147 String methodExpression = "* " + testClassName + ".*(..)"; 148 System.err.println("Adding autolock for: " + methodExpression); 149 config.addWriteAutolock(methodExpression); 150 config.addIncludePattern(TestObject.class.getName()); 151 } 152 153 static class TestObject { 154 private String id; 155 private int count; 156 157 TestObject(String id, int count) { 158 this.id = id; 159 this.count = count; 160 } 161 162 public String getId() { 163 return id; 164 } 165 166 public String toString() { 167 return "TestObject(" + id + "," + count + ")"; 168 } 169 } 170 171 } | Popular Tags |