001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tcverify;
006:
007: import com.tc.util.Assert;
008: import com.tc.verify.VerificationException;
009:
010: import java.util.Date;
011: import java.util.HashMap;
012: import java.util.Map;
013:
014: /**
015: * Verifies that DSO is working correctly.
016: */
017: public class DSOVerifier {
018:
019: private static final boolean DEBUG = false;
020:
021: private static final long TIMEOUT = 1 * 60 * 1000; // 1 minute
022: private static final long POLL_PERIOD = 1 * 1000; // 1 second
023:
024: private final int myID;
025: private final int otherID;
026: private final Map<Integer, String> verifierMap = new HashMap<Integer, String>();
027: private final Integer myIDInteger;
028: private final Integer otherIDInteger;
029:
030: public DSOVerifier(int myID, int otherID) {
031: Assert.eval(myID != otherID);
032: this .myID = myID;
033: this .otherID = otherID;
034: this .myIDInteger = new Integer(this .myID);
035: this .otherIDInteger = new Integer(this .otherID);
036: }
037:
038: private static void debug(String message) {
039: if (DEBUG) {
040: System.err.println(new Date() + ": " + message);
041: }
042: }
043:
044: public void verify() throws VerificationException {
045: setValue(this .myID);
046:
047: long startTime = System.currentTimeMillis();
048: boolean correct = false;
049:
050: while ((System.currentTimeMillis() - startTime) < TIMEOUT) {
051: debug(myID + ": Fetching value from map.");
052: String value = getValue(this .otherID);
053: debug(myID + ": Got: " + value);
054: if (value != null) {
055: if (value.equals("PRESENT-" + otherID)) {
056: correct = true;
057: break;
058: } else {
059: throw new VerificationException(
060: "Got unexpected value '" + value
061: + "' from other VM.");
062: }
063: }
064:
065: try {
066: Thread.sleep(POLL_PERIOD);
067: } catch (InterruptedException ie) {
068: // whatever
069: }
070: }
071:
072: debug("All done. Correct? " + correct);
073: if (!correct)
074: throw new VerificationException(
075: "Waited "
076: + (System.currentTimeMillis() - startTime)
077: + " milliseconds, but didn't get other VM's signal. Is DSO broken?");
078: }
079:
080: private String getValue(int id) {
081: debug("Returning value for " + id);
082: return verifierMap.get(otherIDInteger);
083: }
084:
085: private void setValue(int id) {
086: debug("Setting value for " + id + " to " + id);
087: verifierMap.put(myIDInteger, "PRESENT-" + this .myID);
088: }
089:
090: public static void main(String[] args) {
091: if (args.length != 2) {
092: System.err.println("Usage:");
093: System.err.println(" java " + DSOVerifier.class.getName()
094: + " my-id other-id");
095: System.exit(2);
096: }
097:
098: DSOVerifier verifier = new DSOVerifier(Integer
099: .parseInt(args[0]), Integer.parseInt(args[1]));
100: try {
101: verifier.verify();
102: System.out
103: .println("L2-DSO-OK: Terracotta L2 DSO server is running and DSOing properly.");
104: System.exit(0);
105: } catch (Throwable t) {
106: System.out.println("L2-DSO-FAIL: " + t.getMessage());
107: t.printStackTrace();
108: System.exit(1);
109: }
110: }
111: }
|