001: package com.tctest;
002:
003: import org.apache.commons.collections.LRUMap;
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 LRUMapTestApp extends
019: AbstractErrorCatchingTransparentApp {
020:
021: static final int EXPECTED_THREAD_COUNT = 2;
022:
023: private final CyclicBarrier barrier;
024:
025: private final LRUMap clusteredLRUMap;
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 = LRUMapTestApp.class.getName();
033: config.addIncludePattern(testClass + "$*");
034:
035: final TransparencyClassSpec spec = config
036: .getOrCreateSpec(testClass);
037: spec.addRoot("barrier", "barrier");
038: spec.addRoot("clusteredLRUMap", "clusteredLRUMap");
039: new CyclicBarrierSpec().visit(visitor, config);
040: }
041:
042: public LRUMapTestApp(final String appId,
043: final ApplicationConfig cfg,
044: final ListenerProvider listenerProvider) {
045: super (appId, cfg, listenerProvider);
046: barrier = new CyclicBarrier(getParticipantCount());
047: clusteredLRUMap = new LRUMap();
048: }
049:
050: protected void runTest() throws Throwable {
051: if (barrier.barrier() == 0) {
052: addDataToMap(2);
053: letOtherNodeProceed();
054: waitForPermissionToProceed();
055: verifyEntries(4);
056: removeDataFromMap(2);
057: letOtherNodeProceed();
058: waitForPermissionToProceed();
059: verifyEntries(0);
060: } else {
061: waitForPermissionToProceed();
062: verifyEntries(2);
063: addDataToMap(2);
064: letOtherNodeProceed();
065: waitForPermissionToProceed();
066: verifyEntries(2);
067: clusteredLRUMap.clear();
068: letOtherNodeProceed();
069: }
070: barrier.barrier();
071: }
072:
073: // This is lame but it makes runTest() slightly more readable
074: private void letOtherNodeProceed() throws InterruptedException,
075: BrokenBarrierException {
076: barrier.barrier();
077: }
078:
079: // This is lame but it makes runTest() slightly more readable
080: private void waitForPermissionToProceed()
081: throws InterruptedException, BrokenBarrierException {
082: barrier.barrier();
083: }
084:
085: private void addDataToMap(final int count) {
086: for (int pos = 0; pos < count; ++pos) {
087: clusteredLRUMap.put(new Object(), new Object());
088: }
089: }
090:
091: private void removeDataFromMap(final int count) {
092: for (int pos = 0; pos < count; ++pos) {
093: clusteredLRUMap.remove(clusteredLRUMap.keySet().iterator()
094: .next());
095: }
096: }
097:
098: private void verifyEntries(final int count) {
099: Assert.assertEquals(count, clusteredLRUMap.size());
100: Assert.assertEquals(count, clusteredLRUMap.keySet().size());
101: Assert.assertEquals(count, clusteredLRUMap.values().size());
102: }
103:
104: }
|