1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 8 import com.tc.exception.TCRuntimeException; 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.tctest.runner.AbstractTransparentApp; 16 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Random ; 22 23 public class SimplePrimitiveArrayTestApp extends AbstractTransparentApp { 24 25 private ArrayRoot root; 26 private CyclicBarrier barrier; 27 28 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 29 String testClass = SimplePrimitiveArrayTestApp.class.getName(); 30 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 31 String methodExpression = "* " + testClass + "*.*(..)"; 32 config.addWriteAutolock(methodExpression); 33 spec.addRoot("root", "the-data-root-yo"); 34 spec.addRoot("barrier", "barrier"); 35 config.addIncludePattern(ArrayRoot.class.getName()); 36 new CyclicBarrierSpec().visit(visitor, config); 37 } 38 39 public SimplePrimitiveArrayTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 40 super(appId, cfg, listenerProvider); 41 barrier = new CyclicBarrier(getParticipantCount()); 42 } 43 44 public void run() { 45 try { 46 int length = new Random ().nextInt(100); 47 root = new ArrayRoot(getParticipantCount(), length); 48 Double [] original = root.get(); 49 50 ArrayRoot.validateWithEquals(1, original); 51 ArrayRoot.validate(1, original); 52 barrier.barrier(); 53 54 ArrayRoot.modify(2, original); 55 barrier.barrier(); 56 57 root.validateWithEquals(2); 58 root.validate(2); 59 barrier.barrier(); 60 61 Double [] toCopyFrom = new Double [original.length]; 62 for (int i = 0; i < toCopyFrom.length; i++) { 63 toCopyFrom[i] = new Double (3); 64 } 65 66 synchronized (original) { 67 System.arraycopy(toCopyFrom, 0, original, 0, toCopyFrom.length); 68 } 69 70 barrier.barrier(); 71 root.validateWithEquals(3); 72 root.validate(3); 73 74 } catch (InterruptedException e) { 75 throw new TCRuntimeException(e); 76 } 77 78 } 79 80 private static final class ArrayRoot { 81 private final List arrays; 82 private int index; 83 84 public ArrayRoot(int count, int length) { 85 arrays = new ArrayList (); 86 for (int i = 0; i < count; i++) { 87 Double [] sub = new Double [length]; 88 arrays.add(sub); 89 for (int j = 0; j < sub.length; j++) { 90 sub[j] = new Double (1); 91 } 92 } 93 } 94 95 public int size() { 96 return arrays.size(); 97 } 98 99 public synchronized Double [] get() { 100 return (Double []) arrays.get(index++); 101 } 102 103 public static void modify(double newValue, Double [] array) { 104 synchronized (array) { 105 for (int i = 0; i < array.length; i++) { 106 array[i] = new Double (newValue); 107 } 108 } 109 } 110 111 public synchronized void validate(double expectedValue) { 112 for (Iterator i = arrays.iterator(); i.hasNext();) { 113 validate(expectedValue, (Double []) i.next()); 114 } 115 } 116 117 public synchronized void validateWithEquals(double expectedValue) { 118 for (Iterator i = arrays.iterator(); i.hasNext();) { 119 validateWithEquals(expectedValue, (Double []) i.next()); 120 } 121 } 122 123 public static void validate(double expectedValue, Double [] array) { 124 synchronized (array) { 125 for (int i = 0; i < array.length; i++) { 126 double value = array[i].doubleValue(); 127 if (expectedValue != value) { throw new RuntimeException ("Expected " + expectedValue + " but was " + value); } 128 } 129 } 130 } 131 132 private static void validateWithEquals(double expectedValue, Double [] array) { 133 Double [] compare = new Double [array.length]; 134 for (int i = 0; i < compare.length; i++) { 135 compare[i] = new Double (expectedValue); 136 } 137 if (!Arrays.equals(compare, array)) throw new RuntimeException ("Arrays aren't equal!"); 138 } 139 140 } 141 142 } 143 | Popular Tags |