1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 8 import com.tc.object.bytecode.ManagerUtil; 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.object.lockmanager.api.LockLevel; 14 import com.tc.simulator.app.ApplicationConfig; 15 import com.tc.simulator.listener.ListenerProvider; 16 import com.tc.util.Assert; 17 import com.tctest.runner.AbstractTransparentApp; 18 19 import java.sql.Timestamp ; 20 import java.util.Date ; 21 import java.util.Iterator ; 22 import java.util.Map.Entry; 23 import java.util.concurrent.ConcurrentHashMap ; 24 import java.util.concurrent.LinkedBlockingQueue ; 25 26 public class JavaUtilConcurrentCloneTestApp extends AbstractTransparentApp { 27 28 public static final int NODE_COUNT = 2; 29 30 private MyStuff root; 31 private CyclicBarrier barrier = new CyclicBarrier(NODE_COUNT); 32 private int nodeCount[] = new int[1]; 33 34 public JavaUtilConcurrentCloneTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 35 super(appId, cfg, listenerProvider); 36 } 37 38 public void run() { 39 try { 40 int myId = 0; 41 synchronized (nodeCount) { 42 myId = nodeCount[0]++; 43 } 44 MyStuff copy = null; 45 if (myId == 0) { 46 MyStuff s = new MyStuff(null); 47 s.linkedBlockingQueue = new LinkedBlockingQueue (); 48 s.linkedBlockingQueue.put("first element"); 49 50 s.map = new ConcurrentHashMap (); 51 MapInnerObject innerObject = new MapInnerObject(true); 52 MapObject mapObject = new MapObject(10, innerObject); 53 s.map.put(s, s); 55 56 s.linkedBlockingQueue.put(s.map); 57 s.one = 2; 58 s.date = new Date (); 59 s.myStuffArray = new MyStuff[10]; 60 s.shortArray = new short[200]; 61 s.myStuffArray[3] = s; 62 s.myStuffArray[9] = new MyStuff(null); 63 s.shortArray[5] = 4; 64 65 root = new MyStuff(s); 66 67 synchronized (root) { 68 s.obj1 = root; 69 root.one = 1; 70 root.date = s.date; 71 root.mapObject = mapObject; 72 } 73 ManagerUtil.optimisticBegin(); 74 copy = (MyStuff) ManagerUtil.deepCopy(root); 75 76 Assert.eval(copy != root); 77 Assert.eval(copy.obj1 != root.obj1); 78 79 System.out.println("Root:" + root); 80 System.out.println("Root obj:" + root.obj1 + " one:" + root.one); 81 System.out.println("sub Root obj:" + root.obj1.obj1 + " one:" + root.obj1.one); 82 83 System.out.println("Copy:" + copy); 84 System.out.println("Copy obj:" + copy.obj1 + " one:" + copy.one); 85 System.out.println("sub Copy obj:" + copy.obj1.obj1 + " one:" + copy.obj1.one); 86 87 Assert.eval(copy == copy.obj1.obj1); 88 Assert.eval(copy.inner != root.inner); 89 Assert.eval(copy.inner != null); 90 Assert.eval(copy.one == 1); 91 Assert.eval(copy.obj1.one == 2); 92 Assert.eval(copy.obj1.map != root.obj1.map); 93 Assert.eval(copy.obj1.myStuffArray[3] == copy.obj1); 94 Assert.eval(copy.obj1.myStuffArray[4] == null); 95 Assert.eval(copy.obj1.myStuffArray[2] == null); 96 Assert.eval(copy.obj1.myStuffArray != root.obj1.myStuffArray); 97 98 Assert.eval(copy.obj1.shortArray[4] != 4); 99 Assert.eval(copy.obj1.shortArray[5] == 4); 100 Assert.eval(copy.obj1.shortArray[6] != 4); 101 Assert.eval(copy.obj1.shortArray != root.obj1.shortArray); 102 103 int size = 0; 104 for (Iterator i = copy.obj1.map.entrySet().iterator(); i.hasNext();) { 105 size++; 106 Entry e = (Entry) i.next(); 107 Assert.eval(e.getValue() == copy.obj1); 108 Assert.eval(e.getKey() == copy.obj1); 109 } 110 111 Assert.eval(size == 1); 112 113 Assert.eval((copy.obj1.date != root.obj1.date)); 114 Assert.eval((copy.obj1.date.equals(root.obj1.date))); 115 Assert.eval(copy.obj1.date == copy.date); 116 117 Assert.eval(copy.obj1.linkedBlockingQueue.size() == root.obj1.linkedBlockingQueue.size()); 118 Assert.eval(copy.obj1.linkedBlockingQueue.poll().equals("first element")); 119 Assert.eval(copy.obj1.linkedBlockingQueue.poll() == copy.obj1.map); 120 121 Date d = new Date (); 122 copy.obj1.map.put("date", d); 123 copy.obj1.map.put(copy, d); 124 copy.date = d; 125 copy.one = 500; 126 127 Assert.eval(root.date != d); 128 Assert.eval(!root.obj1.map.containsKey("date")); 129 Assert.eval(root.one != 500); 130 131 ManagerUtil.beginLock("test", LockLevel.WRITE); 132 ManagerUtil.optimisticCommit(); 133 ManagerUtil.commitLock("test"); 134 135 Assert.eval(root.date == d); 136 Assert.eval(root.one == 500); 137 } 138 System.out.println("NODE:" + myId + " is about to enter"); 139 barrier(); 140 if (myId == 0) { 141 ManagerUtil.optimisticBegin(); 142 synchronized ("test") { 143 copy = (MyStuff) ManagerUtil.deepCopy(root); 144 } 145 } 146 barrier(); 147 if (myId == 0) { 148 copy.obj1.one = 250; 149 copy.obj1.map.put("steve", "steve"); 150 copy.obj1.map.put("doh", copy); 151 Assert.eval(copy != null); 152 copy.obj1.myStuffArray[7] = new MyStuff(null); 153 copy.obj1.myStuffArray[7].one = 7; 154 copy.obj1.myStuffArray[3] = copy; 155 } 156 barrier(); 157 if (myId == 1) { 158 synchronized ("test") { 159 root.obj1.one = 150; 160 } 161 } 162 barrier(); 163 if (myId == 0) { 164 ManagerUtil.beginLock("test", LockLevel.WRITE); 165 ManagerUtil.optimisticCommit(); 166 ManagerUtil.commitLock("test"); 167 } 168 barrier(); 169 System.out.println("NODE:" + myId + " is about to exit"); 170 synchronized ("test") { 171 System.out.println("NODE:" + myId + " Value:" + root.obj1.one); 172 Assert.eval(root.obj1.one == 250); 173 Assert.eval(root.obj1.myStuffArray[7] != null); 174 Assert.eval(root.obj1.myStuffArray[7].one == 7); 175 } 176 } catch (Throwable t) { 177 notifyError(t); 178 } 179 } 180 181 private void barrier() { 182 183 try { 184 barrier.barrier(); 185 } catch (InterruptedException ie) { 186 throw new AssertionError (); 187 } 188 } 189 190 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 191 String testClass = JavaUtilConcurrentCloneTestApp.class.getName(); 192 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 193 spec.addRoot("barrier", "barrier"); 194 spec.addRoot("root", "root"); 195 spec.addRoot("nodeCount", "nodeCount"); 196 spec = config.getOrCreateSpec(MyStuff.class.getName()); 197 spec = config.getOrCreateSpec(MyStuff.MyInner.class.getName()); 198 spec = config.getOrCreateSpec(MapObject.class.getName()); 199 spec = config.getOrCreateSpec(MapInnerObject.class.getName()); 200 201 String methodExpression = "* " + testClass + "*.*(..)"; 202 config.addWriteAutolock(methodExpression); 203 new CyclicBarrierSpec().visit(visitor, config); 204 205 } 206 207 private static class MyStuff { 208 public MyStuff obj1; 209 public long one = 1; 210 public LinkedBlockingQueue linkedBlockingQueue; 211 public ConcurrentHashMap map; 212 public Date date; 213 public Timestamp timestamp; 214 public MyStuff[] myStuffArray; 215 public short[] shortArray; 216 public MyInner inner = new MyInner(); 217 public MapObject mapObject; 218 219 public MyStuff(MyStuff stuff) { 220 this.obj1 = stuff; 221 } 222 223 private class MyInner { 224 public int two; 225 } 226 } 227 228 private static class MapObject { 229 private int i; 230 private MapInnerObject innerObject; 231 232 public MapObject(int i, MapInnerObject innerObject) { 233 this.i = i; 234 this.innerObject = innerObject; 235 } 236 237 public int getI() { 238 return i; 239 } 240 241 public MapInnerObject getInnerObject() { 242 return innerObject; 243 } 244 } 245 246 private static class MapInnerObject { 247 private boolean flag; 248 249 public MapInnerObject(boolean flag) { 250 this.flag = flag; 251 } 252 253 public boolean isFlag() { 254 return flag; 255 } 256 } 257 } 258
| Popular Tags
|