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 com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.object.config.Root;
009: import com.tc.simulator.app.Application;
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.ArrayList;
017: import java.util.HashMap;
018: import java.util.HashSet;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022: import java.util.Set;
023:
024: /**
025: * Application for testing DSO sets.
026: */
027: public class TransparentSetTestApp extends AbstractTransparentApp
028: implements Application {
029: private static final int INITIAL_STAGE = 0;
030: private static final int ADD_COMPLETE_STAGE = 1;
031: private static final int ASSERT_MAX_COUNT_SIZE_STAGE = 2;
032: private static final int REMOVE_COMPLETE_STAGE = 3;
033: private static final int ASSERT_REMOVE_SIZE_STAGE = 4;
034:
035: private Set set = new HashSet();
036:
037: public TransparentSetTestApp(String globalId,
038: ApplicationConfig cfg, ListenerProvider listenerProvider) {
039: super (globalId, cfg, listenerProvider);
040: }
041:
042: public void run() {
043: int maxCount = getParticipantCount() * getIntensity();
044: List testObjects = new ArrayList();
045: moveToStage(INITIAL_STAGE);
046: for (int i = 0; i < getIntensity(); i++) {
047: TestObject to = new TestObject(getApplicationId(), 1);
048: testObjects.add(to);
049: synchronized (set) {
050: int size = set.size();
051: set.add(to);
052: Assert.eval(set.size() == size + 1);
053: if ((size + 1) % 50 == 0)
054: System.out.println("XXX added " + (size + 1));
055: }
056: ThreadUtil.reallySleep(20);
057: }
058: moveToStageAndWait(ADD_COMPLETE_STAGE);
059:
060: checkSetSize(maxCount);
061:
062: moveToStageAndWait(ASSERT_MAX_COUNT_SIZE_STAGE);
063:
064: int removeCount = getIntensity() / 2;
065: for (int i = 0; i < removeCount; i++) {
066: synchronized (set) {
067: int size = set.size();
068: boolean wasRemoved = set.remove(testObjects.get(i));
069: Assert.eval(
070: "Test object should have been removed but wasn't: "
071: + testObjects.get(i), wasRemoved);
072: Assert.eval(set.size() == size - 1);
073: if ((size - 1) % 50 == 0)
074: System.out.println("XXX remain " + (size - 1));
075: }
076: ThreadUtil.reallySleep(20);
077: }
078:
079: moveToStageAndWait(REMOVE_COMPLETE_STAGE);
080:
081: checkSetSize(maxCount - getParticipantCount() * removeCount);
082:
083: moveToStageAndWait(ASSERT_REMOVE_SIZE_STAGE);
084:
085: synchronized (set) {
086: set.clear();
087: Assert.eval(set.size() == 0);
088: }
089:
090: checkSetSize(0);
091: notifyResult(Boolean.TRUE);
092: }
093:
094: private void checkSetSize(int s) {
095: synchronized (set) {
096: String error = "set size:" + set.size() + " expecting: "
097: + s + " for: " + this .getApplicationId();
098: System.out.println(error);
099: Map res = new HashMap();
100: for (Iterator i = set.iterator(); i.hasNext();) {
101: TestObject to = (TestObject) i.next();
102: String key = to.getId();
103: if (!res.containsKey(key)) {
104: res.put(key, new Long(0));
105: } else {
106: long v = ((Long) res.get(key)).longValue();
107: res.put(key, new Long(++v));
108: }
109: }
110: if (set.size() != s) {
111:
112: throw new AssertionError("" + res + error);
113: }
114: }
115: }
116:
117: public static void visitL1DSOConfig(ConfigVisitor visitor,
118: DSOClientConfigHelper config) {
119: String testClassName = TransparentSetTestApp.class.getName();
120: config.addRoot(new Root(testClassName, "set", "set"), true);
121: String methodExpression = "* " + testClassName + ".*(..)";
122: System.err.println("Adding autolock for: " + methodExpression);
123:
124: config.addWriteAutolock(methodExpression);
125:
126: config.addIncludePattern(TestObject.class.getName());
127: }
128:
129: static class TestObject {
130:
131: private String id;
132: private int count;
133:
134: TestObject(String id, int count) {
135: this .id = id;
136: this .count = count;
137: }
138:
139: public String getId() {
140: return id;
141: }
142:
143: public String toString() {
144: return "TestObject(" + id + "," + count + ")";
145: }
146: }
147:
148: }
|