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.TransparencyClassSpec; 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.AbstractTransparentApp; 15 16 import java.util.concurrent.atomic.AtomicLong ; 17 18 public class AtomicLongTestApp extends AbstractTransparentApp { 19 20 private final CyclicBarrier barrier; 21 22 private final DataRoot root = new DataRoot(); 23 24 public AtomicLongTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 25 super(appId, cfg, listenerProvider); 26 barrier = new CyclicBarrier(getParticipantCount()); 27 } 28 29 public void run() { 30 try { 31 atomicIntegerTesting(); 32 } catch (Throwable t) { 33 notifyError(t); 34 } 35 } 36 37 private void atomicIntegerTesting() throws Exception { 38 basicSetTesting(); 39 basicGetTesting(); 40 addAndGetTesting(); 41 compareAndSetTesting(); 42 weakCompareAndSetTesting(); 43 getAndIncrementTesting(); 44 getAndDecrementTesting(); 45 getAndSetTesting(); 46 getAndAddTesting(); 47 incrementAndGetTesting(); 48 decrementAndGetTesting(); 49 } 50 51 private void clear() throws Exception { 52 synchronized (root) { 53 root.clear(); 54 } 55 56 barrier.barrier(); 57 } 58 59 private void initialize() throws Exception { 60 61 int index = barrier.barrier(); 62 if (index == 0) { 63 root.getLongValue().set(10); 64 } 65 66 barrier.barrier(); 67 } 68 69 private void basicSetTesting() throws Exception { 70 clear(); 71 initialize(); 72 73 Assert.assertEquals(10, root.getLongValue().get()); 74 75 barrier.barrier(); 76 } 77 78 private void basicGetTesting() throws Exception { 79 clear(); 80 initialize(); 81 82 Assert.assertEquals(10, root.getLongValue().get()); 83 Assert.assertEquals(10.0D, root.getLongValue().doubleValue()); 84 Assert.assertEquals((byte) 10, root.getLongValue().byteValue()); 85 Assert.assertEquals(10.0f, root.getLongValue().floatValue()); 86 Assert.assertEquals(10, root.getLongValue().intValue()); 87 Assert.assertEquals(10L, root.getLongValue().longValue()); 88 Assert.assertEquals(10, root.getLongValue().shortValue()); 89 90 barrier.barrier(); 91 } 92 93 private void addAndGetTesting() throws Exception { 94 clear(); 95 initialize(); 96 97 int index = barrier.barrier(); 98 if (index == 0) { 99 long val = root.getLongValue().addAndGet(4); 100 Assert.assertEquals(14, val); 101 } 102 103 barrier.barrier(); 104 105 Assert.assertEquals(14, root.getLongValue().get()); 106 107 barrier.barrier(); 108 } 109 110 private void compareAndSetTesting() throws Exception { 111 clear(); 112 initialize(); 113 114 int index = barrier.barrier(); 115 if (index == 0) { 116 root.getLongValue().compareAndSet(10, 18); 117 } 118 119 barrier.barrier(); 120 121 Assert.assertEquals(18, root.getLongValue().get()); 122 123 barrier.barrier(); 124 } 125 126 private void weakCompareAndSetTesting() throws Exception { 127 clear(); 128 initialize(); 129 130 int index = barrier.barrier(); 131 if (index == 0) { 132 root.getLongValue().weakCompareAndSet(10, 20); 133 } 134 135 barrier.barrier(); 136 137 Assert.assertEquals(20, root.getLongValue().get()); 138 139 barrier.barrier(); 140 } 141 142 private void getAndIncrementTesting() throws Exception { 143 clear(); 144 initialize(); 145 146 int index = barrier.barrier(); 147 if (index == 0) { 148 long val = root.getLongValue().getAndIncrement(); 149 Assert.assertEquals(10, val); 150 } 151 152 barrier.barrier(); 153 154 Assert.assertEquals(11, root.getLongValue().get()); 155 156 barrier.barrier(); 157 } 158 159 private void getAndDecrementTesting() throws Exception { 160 clear(); 161 initialize(); 162 163 int index = barrier.barrier(); 164 if (index == 0) { 165 long val = root.getLongValue().getAndDecrement(); 166 Assert.assertEquals(10, val); 167 } 168 169 barrier.barrier(); 170 171 Assert.assertEquals(9, root.getLongValue().get()); 172 173 barrier.barrier(); 174 } 175 176 private void getAndSetTesting() throws Exception { 177 clear(); 178 initialize(); 179 180 int index = barrier.barrier(); 181 if (index == 0) { 182 long val = root.getLongValue().getAndSet(200); 183 Assert.assertEquals(10, val); 184 } 185 186 barrier.barrier(); 187 188 Assert.assertEquals(200, root.getLongValue().get()); 189 190 barrier.barrier(); 191 } 192 193 private void getAndAddTesting() throws Exception { 194 clear(); 195 initialize(); 196 197 int index = barrier.barrier(); 198 if (index == 0) { 199 long val = root.getLongValue().getAndAdd(5); 200 Assert.assertEquals(10, val); 201 } 202 203 barrier.barrier(); 204 205 Assert.assertEquals(15, root.getLongValue().get()); 206 207 barrier.barrier(); 208 } 209 210 private void incrementAndGetTesting() throws Exception { 211 clear(); 212 initialize(); 213 214 int index = barrier.barrier(); 215 if (index == 0) { 216 long val = root.getLongValue().incrementAndGet(); 217 Assert.assertEquals(11, val); 218 } 219 220 barrier.barrier(); 221 222 Assert.assertEquals(11, root.getLongValue().get()); 223 224 barrier.barrier(); 225 } 226 227 private void decrementAndGetTesting() throws Exception { 228 clear(); 229 initialize(); 230 231 int index = barrier.barrier(); 232 if (index == 0) { 233 long val = root.getLongValue().decrementAndGet(); 234 Assert.assertEquals(9, val); 235 } 236 237 barrier.barrier(); 238 239 Assert.assertEquals(9, root.getLongValue().get()); 240 241 barrier.barrier(); 242 } 243 244 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 245 TransparencyClassSpec spec = config.getOrCreateSpec(CyclicBarrier.class.getName()); 246 config.addWriteAutolock("* " + CyclicBarrier.class.getName() + "*.*(..)"); 247 248 String testClass = AtomicLongTestApp.class.getName(); 249 spec = config.getOrCreateSpec(testClass); 250 251 config.addIncludePattern(testClass + "$*"); 252 253 String methodExpression = "* " + testClass + "*.*(..)"; 254 config.addWriteAutolock(methodExpression); 255 256 spec.addRoot("barrier", "barrier"); 257 spec.addRoot("root", "root"); 258 } 259 260 private static class DataRoot { 261 private AtomicLong longValue = new AtomicLong (0); 262 263 public DataRoot() { 264 super(); 265 } 266 267 public AtomicLong getLongValue() { 268 return longValue; 269 } 270 271 public void setLongValue(AtomicLong longValue) { 272 this.longValue = longValue; 273 } 274 275 public void clear() { 276 longValue.set(0); 277 } 278 } 279 } 280 | Popular Tags |