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 EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
008:
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.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.text.DateFormat;
018: import java.text.SimpleDateFormat;
019: import java.util.Date;
020: import java.util.HashMap;
021: import java.util.Hashtable;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: public class MapValuesIteratorFaultBreadthTestApp extends
026: AbstractErrorCatchingTransparentApp {
027:
028: private static final int COUNT = 10000;
029: private final Map root1 = new HashMap();
030: private final Map root2 = new Hashtable();
031: private CyclicBarrier barrier;
032:
033: public MapValuesIteratorFaultBreadthTestApp(String appId,
034: ApplicationConfig cfg, ListenerProvider listenerProvider) {
035: super (appId, cfg, listenerProvider);
036: }
037:
038: public static void visitL1DSOConfig(ConfigVisitor visitor,
039: DSOClientConfigHelper config) {
040: String testClass = MapValuesIteratorFaultBreadthTestApp.class
041: .getName();
042: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
043: spec.addRoot("root1", "root1");
044: spec.addRoot("root2", "root2");
045: spec.addRoot("barrier", "barrier");
046: String methodExpression = "* " + testClass + ".read(..)";
047: config.addReadAutolock(methodExpression);
048: methodExpression = "* " + testClass + ".*(..)";
049: config.addWriteAutolock(methodExpression);
050: testClass = MapValuesIteratorFaultBreadthTestApp.Value.class
051: .getName();
052: spec = config.getOrCreateSpec(testClass);
053: new CyclicBarrierSpec().visit(visitor, config);
054: }
055:
056: protected void runTest() throws Throwable {
057: setCyclicBarrier();
058: runTestFor(root1);
059: runTestFor(root2);
060: }
061:
062: private void runTestFor(Map m) throws Throwable {
063: faultRootMap(m);
064: int count = barrier.barrier();
065: if (count == 0) {
066: fillUpRootMap(m);
067: log("Population done");
068: barrier.barrier();
069: } else {
070: barrier.barrier();
071: faultValuesInMap(m);
072: }
073: }
074:
075: private void faultValuesInMap(Map root) {
076: synchronized (root) {
077: long start = System.currentTimeMillis();
078: Iterator i = root.values().iterator();
079: while (i.hasNext()) {
080: Object v = i.next();
081: if (v == null) {
082: throw new AssertionError("Value null for " + i);
083: }
084: }
085: log("Faulting doing in "
086: + (System.currentTimeMillis() - start) + " ms for "
087: + root.size() + " elements in "
088: + root.getClass().getName());
089: }
090: }
091:
092: private void fillUpRootMap(Map root) {
093: for (int i = 0; i < COUNT; i++) {
094: synchronized (root) {
095: root.put("Key-" + i, new Value());
096: }
097: }
098: }
099:
100: private void faultRootMap(Map root) {
101: for (int i = 0; i < 10; i++) {
102: synchronized (root) {
103: if (i == 9) {
104: root.clear();
105: } else {
106: root.put("Init" + i, "Hello");
107: }
108: }
109: }
110:
111: }
112:
113: private void setCyclicBarrier() {
114: int participationCount = getParticipantCount();
115: log("Participation Count = " + participationCount);
116: barrier = new CyclicBarrier(participationCount);
117: }
118:
119: static DateFormat formatter = new SimpleDateFormat("hh:mm:ss,S");
120:
121: private static void log(String message) {
122: System.err.println(Thread.currentThread().getName()
123: + " :: "
124: + formatter
125: .format(new Date(System.currentTimeMillis()))
126: + " : " + message);
127: }
128:
129: private class Value {
130: long time = System.currentTimeMillis();
131:
132: public String toString() {
133: return "Value [ " + formatter.format(new Date(time)) + "]";
134: }
135: }
136:
137: }
|