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.ConfigLockLevel; 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.object.tx.ReadOnlyException; 14 import com.tc.simulator.app.ApplicationConfig; 15 import com.tc.simulator.listener.ListenerProvider; 16 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 17 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Vector ; 29 30 public class LogicalIteratorTestApp extends AbstractErrorCatchingTransparentApp { 31 32 private final Set set = new HashSet (); 33 private final Map map = new HashMap (); 34 private final List llist = new LinkedList (); 35 private final List alist = new ArrayList (); 36 private final Vector vector = new Vector (); 37 private final Hashtable hashtable = new Hashtable (); 38 private final CyclicBarrier barrier; 39 40 public LogicalIteratorTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 41 super(appId, cfg, listenerProvider); 42 this.barrier = new CyclicBarrier(getParticipantCount()); 43 } 44 45 protected void runTest() throws Throwable { 46 String me = getApplicationId(); 47 init(me); 48 barrier.barrier(); 49 readOnly(); 50 barrier.barrier(); 51 remove(me); 52 barrier.barrier(); 53 verifySize(); 54 } 55 56 private void verifySize() { 57 synchronized (set) { 58 if (vector.size() != 0) { throw new RuntimeException ("vector not empty: " + vector.size()); } 59 if (map.size() != 0) { throw new RuntimeException ("map not empty: " + map.size()); } 60 if (alist.size() != 0) { throw new RuntimeException ("alist not empty" + alist.size()); } 61 if (set.size() != 0) { throw new RuntimeException ("set not empty: " + set.size()); } 62 if (llist.size() != 0) { throw new RuntimeException ("llist not empty: " + llist.size()); } 63 if (hashtable.size() != 0) { throw new RuntimeException ("hashtable not empty: " + hashtable.size()); } 64 } 65 } 66 67 private void init(String id) { 68 synchronized (barrier) { 69 vector.add(id); 70 map.put("key" + id, id); 71 map.put(id, "value" + id); 72 alist.add(id); 73 set.add(id); 74 llist.add(id); 75 hashtable.put("key" + id, id); 76 hashtable.put(id, "value" + id); 77 } 78 } 79 80 private void remove(String id) { 81 synchronized (barrier) { 82 remove(vector.iterator(), id); 83 remove(map.keySet().iterator(), "key" + id); 84 remove(map.values().iterator(), "value" + id); 85 remove(alist.iterator(), id); 86 remove(set.iterator(), id); 87 remove(llist.iterator(), id); 88 remove(hashtable.keySet().iterator(), "key" + id); 89 remove(hashtable.values().iterator(), "value" + id); 90 } 91 } 92 93 private void remove(Iterator iter, String id) { 94 while (iter.hasNext()) { 95 if (id.equals(iter.next())) { 96 iter.remove(); 97 return; 98 } 99 } 100 } 101 102 private void readOnly() { 103 synchronized (barrier) { 104 attemptRemove(map.keySet()); 105 attemptRemove(map.values()); 106 attemptRemove(alist); 107 attemptRemove(set); 108 attemptRemove(llist); 109 } 110 } 111 112 private void attemptRemove(Collection collection) { 113 String type = collection.getClass().getName(); 114 final int startSize = collection.size(); 115 116 Iterator iter = collection.iterator(); 117 iter.next(); 118 119 try { 120 iter.remove(); 121 throw new RuntimeException ("Iterator remove() allowed in read-only transaction on type " + type); 122 } catch (ReadOnlyException roe) { 123 } 125 126 int endSize = collection.size(); 127 if (startSize != endSize) { 128 throw new AssertionError ("Collection of type " + type + " changed size during read-only TXN: (" + startSize 130 + " != " + endSize + ")"); 131 } 132 133 } 134 135 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 136 String testClass = LogicalIteratorTestApp.class.getName(); 137 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 138 139 new CyclicBarrierSpec().visit(visitor, config); 140 141 String method = "* " + testClass + ".init(..)"; 142 config.addWriteAutolock(method); 143 144 method = "* " + testClass + ".readOnly(..)"; 145 config.addAutolock(method, ConfigLockLevel.READ); 146 147 method = "* " + testClass + ".remove(..)"; 148 config.addWriteAutolock(method); 149 150 method = "* " + testClass + ".runTest()"; 151 config.addWriteAutolock(method); 152 153 method = "* " + testClass + ".verifySize(..)"; 154 config.addWriteAutolock(method); 155 156 spec.addRoot("hashtable", "hashtableLock"); 157 spec.addRoot("set", "setLock"); 158 spec.addRoot("vector", "vectorLock"); 159 spec.addRoot("map", "mapLock"); 160 spec.addRoot("llist", "llistLock"); 161 spec.addRoot("alist", "alistLock"); 162 spec.addRoot("barrier", "barrierLock"); 163 } 164 165 } 166 | Popular Tags |