001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tc.util.Assert;
013: import com.tc.util.concurrent.ThreadUtil;
014: import com.tctest.runner.AbstractTransparentApp;
015:
016: import java.util.HashMap;
017: import java.util.Map;
018: import java.util.concurrent.CyclicBarrier;
019:
020: public class StateClassOnPassiveLosesInfoActivePassiveTestApp extends
021: AbstractTransparentApp {
022:
023: private static final int ITERATION_COUNT = 50;
024:
025: private final CyclicBarrier barrier;
026: // For this test to work(fail when there is a problem), this root needs a map
027: private final Map<Integer, StateObject> root = new HashMap<Integer, StateObject>();
028:
029: public StateClassOnPassiveLosesInfoActivePassiveTestApp(
030: String appId, ApplicationConfig cfg,
031: ListenerProvider listenerProvider) {
032: super (appId, cfg, listenerProvider);
033: Assert.assertEquals(2, getParticipantCount());
034: barrier = new CyclicBarrier(getParticipantCount());
035: }
036:
037: public void run() {
038: try {
039: int index = barrier.await();
040: if (index == 0) {
041: System.err.println("Entering stage 1");
042: run1();
043: System.err
044: .println("Waiting for several server crashes to happen ... 90 secs ");
045: ThreadUtil.reallySleep(90000);
046: System.err.println("Entering stage 2");
047: run2();
048: }
049: barrier.await();
050: if (index != 0) {
051: System.err.println("Entering stage 3 : verify stage");
052: verify2();
053: }
054:
055: } catch (Throwable t) {
056: notifyError(t);
057: }
058: }
059:
060: private void run1() {
061: int count = 0;
062: while (count < ITERATION_COUNT) {
063: StateObject s = new StateObject();
064: switch (count % 4) {
065: case 0:
066: s.ref = new Integer(77);
067: break;
068: case 1:
069: s.ref = new Long(1977);
070: break;
071: case 2:
072: s.ref = new Float(19.77);
073: break;
074: case 3:
075: s.ref = new Double(1.977d);
076: break;
077: }
078: synchronized (root) {
079: root.put(new Integer(count++), s);
080: }
081: ThreadUtil.reallySleep(10);
082: }
083: }
084:
085: private void run2() {
086: int count = 0;
087: while (count < ITERATION_COUNT) {
088: StateObject s = getStateObject(count++);
089: synchronized (s) {
090: switch (count % 4) {
091: case 0:
092: s.ref = new String("1977");
093: break;
094: case 1:
095: s.ref = new Object();
096: break;
097: case 2:
098: s.ref = new StateObject();
099: break;
100: case 3:
101: s.ref = State.START;
102: break;
103: }
104: }
105: ThreadUtil.reallySleep(10);
106: }
107: }
108:
109: private void verify2() {
110: int count = 0;
111: while (count < ITERATION_COUNT) {
112: StateObject s = getStateObject(count++);
113: synchronized (s) {
114: System.err.println(count + " - > " + s.ref);
115: switch (count % 4) {
116: case 0:
117: Assert.assertEquals(s.ref, new String("1977"));
118: break;
119: case 1:
120: Assert.assertTrue(s.ref != null
121: && s.ref.getClass().getName().equals(
122: Object.class.getName()));
123: break;
124: case 2:
125: Assert.assertTrue(s.ref instanceof StateObject);
126: break;
127: case 3:
128: Assert.assertEquals(State.START, s.ref);
129: break;
130: }
131: }
132: ThreadUtil.reallySleep(10);
133: }
134: }
135:
136: private StateObject getStateObject(int idx) {
137: synchronized (root) {
138: return root.get(new Integer(idx));
139: }
140: }
141:
142: public static void visitL1DSOConfig(ConfigVisitor visitor,
143: DSOClientConfigHelper config) {
144: String testClass = StateClassOnPassiveLosesInfoActivePassiveTestApp.class
145: .getName();
146: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
147:
148: config.addIncludePattern(testClass + "$*", false, false, false);
149:
150: String methodExpression = "* " + testClass + "*.*(..)";
151: config.addWriteAutolock(methodExpression);
152:
153: spec.addRoot("root", "root");
154: spec.addRoot("barrier", "barrier");
155: }
156:
157: private static final class StateObject {
158:
159: Object ref;
160: int i;
161:
162: }
163:
164: private static enum State {
165: START, END
166: }
167:
168: }
|