001: package com.tctest;
002:
003: import org.apache.commons.collections.FastHashMap;
004:
005: import EDU.oswego.cs.dl.util.concurrent.BrokenBarrierException;
006: import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.object.config.spec.CyclicBarrierSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tc.util.TIMUtil;
016: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
017:
018: public final class FastHashMapTestApp extends
019: AbstractErrorCatchingTransparentApp {
020:
021: static final int EXPECTED_THREAD_COUNT = 2;
022:
023: private final CyclicBarrier barrier;
024:
025: private final FastHashMap clusteredFastHashMap;
026:
027: public static void visitL1DSOConfig(final ConfigVisitor visitor,
028: final DSOClientConfigHelper config) {
029: config.addModule(TIMUtil.COMMONS_COLLECTIONS_3_1, TIMUtil
030: .getVersion(TIMUtil.COMMONS_COLLECTIONS_3_1));
031:
032: final String testClass = FastHashMapTestApp.class.getName();
033: config.addIncludePattern(testClass + "$*");
034:
035: final TransparencyClassSpec spec = config
036: .getOrCreateSpec(testClass);
037: spec.addRoot("barrier", "barrier");
038: spec.addRoot("clusteredFastHashMap", "clusteredFastHashMap");
039: new CyclicBarrierSpec().visit(visitor, config);
040: }
041:
042: public FastHashMapTestApp(final String appId,
043: final ApplicationConfig cfg,
044: final ListenerProvider listenerProvider) {
045: super (appId, cfg, listenerProvider);
046: barrier = new CyclicBarrier(getParticipantCount());
047: clusteredFastHashMap = new FastHashMap();
048: clusteredFastHashMap.setFast(true);
049: }
050:
051: protected void runTest() throws Throwable {
052: if (barrier.barrier() == 0) {
053: addDataToMap(2);
054: letOtherNodeProceed();
055: waitForPermissionToProceed();
056: verifyEntries(4);
057: removeDataFromMap(2);
058: letOtherNodeProceed();
059: waitForPermissionToProceed();
060: verifyEntries(0);
061: } else {
062: waitForPermissionToProceed();
063: verifyEntries(2);
064: addDataToMap(2);
065: letOtherNodeProceed();
066: waitForPermissionToProceed();
067: verifyEntries(2);
068: clusteredFastHashMap.clear();
069: letOtherNodeProceed();
070: }
071: barrier.barrier();
072: }
073:
074: // This is lame but it makes runTest() slightly more readable
075: private void letOtherNodeProceed() throws InterruptedException,
076: BrokenBarrierException {
077: barrier.barrier();
078: }
079:
080: // This is lame but it makes runTest() slightly more readable
081: private void waitForPermissionToProceed()
082: throws InterruptedException, BrokenBarrierException {
083: barrier.barrier();
084: }
085:
086: private void addDataToMap(final int count) {
087: for (int pos = 0; pos < count; ++pos) {
088: clusteredFastHashMap.put(new Object(), new Object());
089: }
090: }
091:
092: private void removeDataFromMap(final int count) {
093: for (int pos = 0; pos < count; ++pos) {
094: clusteredFastHashMap.remove(clusteredFastHashMap.keySet()
095: .iterator().next());
096: }
097: }
098:
099: private void verifyEntries(final int count) {
100: Assert.assertEquals(count, clusteredFastHashMap.size());
101: Assert
102: .assertEquals(count, clusteredFastHashMap.keySet()
103: .size());
104: Assert
105: .assertEquals(count, clusteredFastHashMap.values()
106: .size());
107: }
108:
109: }
|