1 4 package com.tcverify; 5 6 import com.tc.logging.TCLogger; 7 import com.tc.logging.TCLogging; 8 import com.tc.util.Assert; 9 import com.tc.verify.VerificationException; 10 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 17 public class DSOVerifier { 18 19 private static final TCLogger logger = TCLogging.getTestingLogger(DSOVerifier.class); 20 21 private static final long TIMEOUT = 1 * 60 * 1000; private static final long POLL_PERIOD = 1 * 1000; 24 private final int myID; 25 private final int otherID; 26 private Map verifierMap = new HashMap (); 27 private final Integer myIDInteger; 28 private final Integer otherIDInteger; 29 30 public DSOVerifier(int myID, int otherID) { 31 Assert.eval(myID != otherID); 32 this.myID = myID; 33 this.otherID = otherID; 34 this.myIDInteger = new Integer (this.myID); 35 this.otherIDInteger = new Integer (this.otherID); 36 } 37 38 public void verify() throws VerificationException { 39 setValue(this.myID); 40 41 long startTime = System.currentTimeMillis(); 42 boolean correct = false; 43 44 while ((System.currentTimeMillis() - startTime) < TIMEOUT) { 45 logger.debug(myID + ": Fetching value from map."); 46 String value = (String ) getValue(this.otherID); 47 logger.debug(myID + ": Got: " + value); 48 if (value != null) { 49 if (value.equals("PRESENT-" + otherID)) { 50 correct = true; 51 break; 52 } else { 53 throw new VerificationException("Got unexpected value '" + value + "' from other VM."); 54 } 55 } 56 57 try { 58 Thread.sleep(POLL_PERIOD); 59 } catch (InterruptedException ie) { 60 } 62 } 63 64 logger.debug("All done. Correct? " + correct); 65 if (!correct) throw new VerificationException("Waited " + (System.currentTimeMillis() - startTime) 66 + " milliseconds, but didn't get other VM's signal. Is DSO broken?"); 67 } 68 69 private Object getValue(int id) { 70 logger.debug("Returning value for " + id); 71 return verifierMap.get(otherIDInteger); 72 } 73 74 private void setValue(int id) { 75 logger.debug("Setting value for " + id + " to " + id); 76 verifierMap.put(myIDInteger, "PRESENT-" + this.myID); 77 } 78 79 public static void main(String [] args) { 80 if (args.length != 2) { 81 System.err.println("Usage:"); 82 System.err.println(" java " + DSOVerifier.class.getName() + " my-id other-id"); 83 System.exit(2); 84 } 85 86 DSOVerifier verifier = new DSOVerifier(Integer.parseInt(args[0]), Integer.parseInt(args[1])); 87 try { 88 verifier.verify(); 89 System.out.println("L2-DSO-OK: Terracotta L2 DSO server is running and DSOing properly."); 90 System.exit(0); 91 } catch (Throwable t) { 92 System.out.println("L2-DSO-FAIL: " + t.getMessage()); 93 t.printStackTrace(); 94 System.exit(1); 95 } 96 } 97 }
| Popular Tags
|