001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.bytecode.ManagerUtil;
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.object.config.spec.CyclicBarrierSpec;
013: import com.tc.object.lockmanager.api.LockLevel;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tc.util.Assert;
017: import com.tctest.runner.AbstractTransparentApp;
018:
019: public class NestedReadOnlyTransactionTestApp extends
020: AbstractTransparentApp {
021: private final CyclicBarrier barrier;
022: private final DataRoot dataRoot = new DataRoot();
023:
024: public NestedReadOnlyTransactionTestApp(String appId,
025: ApplicationConfig cfg, ListenerProvider listenerProvider) {
026: super (appId, cfg, listenerProvider);
027: barrier = new CyclicBarrier(getParticipantCount());
028: }
029:
030: public void run() {
031: try {
032: int index = barrier.barrier();
033: nestedReadLockTest(index);
034: nestedWriteLockTest(index);
035: } catch (Throwable t) {
036: notifyError(t);
037: }
038: }
039:
040: private void nestedReadLockTest(int index) throws Exception {
041: if (index == 0) {
042: ManagerUtil.monitorEnter(dataRoot, LockLevel.WRITE);
043: dataRoot.setLongValue(15);
044: Assert
045: .assertEquals(15, dataRoot
046: .getSynchronizedLongValue());
047: }
048: try {
049: dataRoot.assertLongValue(index, 15);
050: } finally {
051: if (index == 0) {
052: dataRoot.setCommit(true);
053: ManagerUtil.monitorExit(dataRoot);
054: }
055: }
056:
057: barrier.barrier();
058: }
059:
060: private void nestedWriteLockTest(int index) throws Exception {
061: if (index == 0) {
062: dataRoot.clear();
063: }
064:
065: barrier.barrier();
066:
067: if (index == 0) {
068: ManagerUtil.monitorEnter(dataRoot, LockLevel.WRITE);
069: dataRoot.setLongValue(15);
070: }
071: try {
072: if (index == 0) {
073: synchronized (dataRoot) {
074: long l = dataRoot.getLongValue();
075: Assert.assertEquals(15, l);
076: }
077: }
078:
079: barrier.barrier();
080:
081: long l = dataRoot.getLongValue();
082: Assert.assertEquals(15, l);
083:
084: barrier.barrier();
085: } finally {
086: if (index == 0) {
087: ManagerUtil.monitorExit(dataRoot);
088: }
089: }
090:
091: barrier.barrier();
092:
093: Assert.assertEquals(15, dataRoot.getLongValue());
094:
095: barrier.barrier();
096: }
097:
098: public static void visitL1DSOConfig(ConfigVisitor visitor,
099: DSOClientConfigHelper config) {
100: String testClass = NestedReadOnlyTransactionTestApp.class
101: .getName();
102: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
103: String methodExpression = "* " + testClass + "*.*(..)";
104: config.addWriteAutolock(methodExpression);
105: config.addReadAutolock("* " + testClass
106: + "*.*getSynchronizedLongValue(..)");
107: config.addReadAutolock("* " + testClass
108: + "*.*assertLongValue(..)");
109:
110: config.addIncludePattern(testClass + "$*");
111:
112: new CyclicBarrierSpec().visit(visitor, config);
113:
114: spec.addRoot("barrier", "barrier");
115: spec.addRoot("dataRoot", "dataRoot");
116: }
117:
118: private static class DataRoot {
119: private final Object readLockObject = new Object();
120: private boolean commit;
121: private long longValue;
122:
123: public DataRoot() {
124: super ();
125: }
126:
127: public long getSynchronizedLongValue() {
128: synchronized (readLockObject) {
129: return longValue;
130: }
131: }
132:
133: public long getLongValue() {
134: return longValue;
135: }
136:
137: public void setLongValue(long longValue) {
138: this .longValue = longValue;
139: }
140:
141: public void setCommit(boolean commit) {
142: this .commit = commit;
143: }
144:
145: public boolean isCommitted() {
146: return this .commit;
147: }
148:
149: public void assertLongValue(int index, int newValue) {
150: synchronized (readLockObject) {
151: if (index == 0) {
152: Assert.assertEquals(newValue, longValue);
153: } else {
154: if (commit) {
155: Assert.assertEquals(newValue, longValue);
156: } else {
157: Assert.assertEquals(0, longValue);
158: }
159: }
160: }
161: }
162:
163: public synchronized void clear() {
164: this .longValue = 0;
165: this .commit = false;
166: }
167: }
168: }
|