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 public class NestedReadOnlyTransactionTestApp extends AbstractTransparentApp { 20 private final CyclicBarrier barrier; 21 private final DataRoot dataRoot = new DataRoot(); 22 23 public NestedReadOnlyTransactionTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 24 super(appId, cfg, listenerProvider); 25 barrier = new CyclicBarrier(getParticipantCount()); 26 } 27 28 public void run() { 29 try { 30 int index = barrier.barrier(); 31 nestedReadLockTest(index); 32 nestedWriteLockTest(index); 33 } catch (Throwable t) { 34 notifyError(t); 35 } 36 } 37 38 private void nestedReadLockTest(int index) throws Exception { 39 if (index == 0) { 40 ManagerUtil.monitorEnter(dataRoot, LockLevel.WRITE); 41 dataRoot.setLongValue(15); 42 Assert.assertEquals(15, dataRoot.getSynchronizedLongValue()); 43 } 44 try { 45 dataRoot.assertLongValue(index, 15); 46 } finally { 47 if (index == 0) { 48 dataRoot.setCommit(true); 49 ManagerUtil.monitorExit(dataRoot); 50 } 51 } 52 53 barrier.barrier(); 54 } 55 56 private void nestedWriteLockTest(int index) throws Exception { 57 if (index == 0) { 58 dataRoot.clear(); 59 } 60 61 barrier.barrier(); 62 63 if (index == 0) { 64 ManagerUtil.monitorEnter(dataRoot, LockLevel.WRITE); 65 dataRoot.setLongValue(15); 66 } 67 try { 68 if (index == 0) { 69 synchronized (dataRoot) { 70 long l = dataRoot.getLongValue(); 71 Assert.assertEquals(15, l); 72 } 73 } 74 75 barrier.barrier(); 76 77 long l = dataRoot.getLongValue(); 78 Assert.assertEquals(15, l); 79 80 barrier.barrier(); 81 } finally { 82 if (index == 0) { 83 ManagerUtil.monitorExit(dataRoot); 84 } 85 } 86 87 barrier.barrier(); 88 89 Assert.assertEquals(15, dataRoot.getLongValue()); 90 91 barrier.barrier(); 92 } 93 94 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 95 String testClass = NestedReadOnlyTransactionTestApp.class.getName(); 96 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 97 String methodExpression = "* " + testClass + "*.*(..)"; 98 config.addWriteAutolock(methodExpression); 99 config.addReadAutolock("* " + testClass + "*.*getSynchronizedLongValue(..)"); 100 config.addReadAutolock("* " + testClass + "*.*assertLongValue(..)"); 101 102 config.addIncludePattern(testClass + "$*"); 103 104 new CyclicBarrierSpec().visit(visitor, config); 105 106 spec.addRoot("barrier", "barrier"); 107 spec.addRoot("dataRoot", "dataRoot"); 108 } 109 110 private static class DataRoot { 111 private final Object readLockObject = new Object (); 112 private boolean commit; 113 private long longValue; 114 115 public DataRoot() { 116 super(); 117 } 118 119 public long getSynchronizedLongValue() { 120 synchronized(readLockObject) { 121 return longValue; 122 } 123 } 124 125 public long getLongValue() { 126 return longValue; 127 } 128 129 public void setLongValue(long longValue) { 130 this.longValue = longValue; 131 } 132 133 public void setCommit(boolean commit) { 134 this.commit = commit; 135 } 136 137 public boolean isCommitted() { 138 return this.commit; 139 } 140 141 public void assertLongValue(int index, int newValue) { 142 synchronized(readLockObject) { 143 if (index == 0) { 144 Assert.assertEquals(newValue, longValue); 145 } else { 146 if (commit) { 147 Assert.assertEquals(newValue, longValue); 148 } else { 149 Assert.assertEquals(0, longValue); 150 } 151 } 152 } 153 } 154 155 public synchronized void clear() { 156 this.longValue = 0; 157 this.commit = false; 158 } 159 } 160 } 161 | Popular Tags |