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.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
012:
013: public abstract class AbstractMutateValidateTransparentApp extends
014: AbstractErrorCatchingTransparentApp {
015: public static final String VALIDATOR_COUNT = "validator-count";
016: public static final String MUTATOR_COUNT = "mutator-count";
017: private static final boolean DEBUG = true;
018:
019: protected final int validatorCount;
020: protected final int mutatorCount;
021: private final boolean isMutator;
022: private final ListenerProvider listenerProvider;
023: private final String appId;
024:
025: public AbstractMutateValidateTransparentApp(String appId,
026: ApplicationConfig cfg, ListenerProvider listenerProvider) {
027: super (appId, cfg, listenerProvider);
028: this .appId = appId;
029: this .listenerProvider = listenerProvider;
030:
031: validatorCount = cfg.getValidatorCount();
032: mutatorCount = cfg.getGlobalParticipantCount();
033: isMutator = Boolean.valueOf(cfg.getAttribute(appId))
034: .booleanValue();
035:
036: debugPrintln("***** appId=[" + appId + "]: isMutator=["
037: + isMutator + "]");
038: }
039:
040: protected void debugPrintln(String s) {
041: if (DEBUG) {
042: System.err.println(s);
043: }
044: }
045:
046: // mutator: mutate, wait until all mutation is done, then validate
047: // validator: wait until all mutation is done, then validate
048: public void runTest() throws Throwable {
049: if (isMutator) {
050: debugPrintln("***** appId[" + appId + "]: starting mutate");
051: mutate();
052: debugPrintln("***** appId[" + appId + "]: finished mutate");
053: notifyMutationComplete();
054: debugPrintln("***** appId["
055: + appId
056: + "]: notified mutate-listener... waiting for mutate stage to finish");
057: waitForMutationComplete();
058: debugPrintln("***** appId[" + appId
059: + "]: mutate stage complete");
060: }
061:
062: Thread.sleep(5000);
063:
064: notifyValidationStart();
065: debugPrintln("***** appId["
066: + appId
067: + "]: notified mutate-listener... waiting for validat stage to start");
068: waitForValidationStart();
069:
070: debugPrintln("***** appId[" + appId + "]: starting validate");
071: validate();
072: debugPrintln("***** appId[" + appId + "]: finished validate");
073: }
074:
075: private final void waitForValidationStart() throws Exception {
076: listenerProvider.getMutationCompletionListener()
077: .waitForValidationStartTestWide();
078: }
079:
080: private final void notifyMutationComplete() {
081: listenerProvider.getMutationCompletionListener()
082: .notifyMutationComplete();
083: }
084:
085: private final void notifyValidationStart() {
086: listenerProvider.getMutationCompletionListener()
087: .notifyValidationStart();
088: }
089:
090: private final void waitForMutationComplete() throws Exception {
091: listenerProvider.getMutationCompletionListener()
092: .waitForMutationCompleteTestWide();
093: }
094:
095: protected boolean isMutator() {
096: return isMutator;
097: }
098:
099: protected abstract void mutate() throws Throwable;
100:
101: protected abstract void validate() throws Throwable;
102:
103: public static void visitL1DSOConfig(ConfigVisitor visitor,
104: DSOClientConfigHelper config) {
105: String testClass = AbstractMutateValidateTransparentApp.class
106: .getName();
107: config.getOrCreateSpec(testClass);
108: String methodExpression = "* " + testClass + "*.*(..)";
109: config.addWriteAutolock(methodExpression);
110: }
111:
112: }
|