1 5 package com.tctest.restart.system; 6 7 import org.apache.commons.io.FileUtils; 8 9 import com.tc.objectserver.control.ExtraL1ProcessControl; 10 import com.tc.simulator.app.ApplicationConfig; 11 import com.tc.simulator.listener.ListenerProvider; 12 import com.tctest.ServerCrashingAppBase; 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.Random ; 18 19 public class ClientTerminatingTestApp extends ServerCrashingAppBase { 20 21 public static final String FORCE_KILL = "force-kill"; 22 23 private static final int LOOP_COUNT = 2; 24 private static final List queue = new ArrayList (); 25 26 private int id = -1; 27 private long count = 0; 28 private ExtraL1ProcessControl client; 29 private final boolean forceKill; 30 31 public ClientTerminatingTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 32 super(appId, cfg, listenerProvider); 33 34 String forceKillVal = cfg.getAttribute(FORCE_KILL); 35 if (forceKillVal != null && !forceKillVal.equals("")) { 36 forceKill = true; 37 } else { 38 forceKill = false; 39 } 40 } 41 42 public void run() { 43 List myList = new ArrayList (); 44 synchronized (queue) { 45 if (id != -1) { throw new AssertionError ("Only one controller per Instance allowed. Check the Execution count"); } 46 id = queue.size(); 47 queue.add(myList); 48 } 49 50 Random random = new Random (); 51 int times = LOOP_COUNT; 52 try { 53 while (times-- > 0) { 54 long toAdd = random.nextInt(10) * 50L + 1; 55 File workingDir = new File (getConfigFileDirectoryPath(), "client-" + id + "-" + times); 56 FileUtils.forceMkdir(workingDir); 57 System.err.println(this + "Creating Client with args " + id + " , " + toAdd); 58 client = new ExtraL1ProcessControl(getHostName(), getPort(), Client.class, getConfigFilePath(), new String [] { 59 "" + id, "" + toAdd, "" + forceKill }, workingDir); 60 client.start(20000); 61 int exitCode = client.waitFor(); 62 if (exitCode == 0) { 63 System.err.println(this + "Client existed Normally"); 64 verify(myList, toAdd); 65 } else { 66 String errorMsg = this + "Client existed Abnormally. Exit code = " + exitCode; 67 System.err.println(errorMsg); 68 throw new AssertionError (errorMsg); 69 } 70 } 71 } catch (Exception e) { 72 System.err.println(this + " Got - " + e); 73 throw new AssertionError (e); 74 } 75 76 } 77 78 private void verify(List myList, long toAdd) { 79 synchronized (myList) { 80 if (toAdd != myList.size()) { 81 String errorMsg = this + " Expected " + toAdd + " elements in the list. But Found " + myList.size(); 82 System.err.println(errorMsg); 83 throw new AssertionError (errorMsg); 84 } 85 } 86 for (int i = 0; i < myList.size(); i++) { 87 synchronized (myList) { 88 if ((++count != ((Long ) myList.get(i)).longValue())) { 89 String errorMsg = this + " Expected " + count + " value in the list. But Found " + myList.get(i); 90 System.err.println(errorMsg); 91 throw new AssertionError (errorMsg); 92 } 93 } 94 } 95 } 96 97 public String toString() { 98 return "Controller(" + id + ") :"; 99 } 100 101 public static class Client { 102 private int id; 103 private long addCount; 104 private boolean shouldForceKill; 105 106 public Client(int i, long addCount, boolean shouldForceKill) { 107 this.id = i; 108 this.addCount = addCount; 109 this.shouldForceKill = shouldForceKill; 110 } 111 112 public static void main(String args[]) { 113 if (args.length < 2 || args.length > 3) { throw new AssertionError ( 114 "Usage : Client <id> <num of increments> [shouldForceKill]"); } 115 116 boolean shouldForceKill; 117 if (args.length == 3 && args[2] != null && !args[2].equals("")) { 118 shouldForceKill = Boolean.valueOf(args[2]).booleanValue(); 119 } else { 120 shouldForceKill = false; 121 } 122 123 Client client = new Client(Integer.parseInt(args[0]), Long.parseLong(args[1]), shouldForceKill); 124 client.execute(); 125 } 126 127 public void execute() { 129 List myList = null; 130 long count = 0; 131 System.err.println(this + " execute : addCount = " + addCount); 132 synchronized (queue) { 133 myList = (List ) queue.get(id); 134 } 135 synchronized (myList) { 136 if (myList.size() > 0) { 137 count = ((Long ) myList.get(myList.size() - 1)).longValue(); 138 myList.clear(); 139 } 140 } 141 while (addCount-- > 0) { 142 synchronized (myList) { 143 myList.add(new Long (++count)); 144 } 145 } 146 147 if (shouldForceKill) { 148 System.err.println(this + " killed forceably :" + count); 149 Runtime.getRuntime().halt(0); 150 } else { 151 System.err.println(this + " put till :" + count); 152 System.exit(0); 153 } 154 } 155 156 public String toString() { 157 return "Client(" + id + ") :"; 158 } 159 160 } 161 162 } 163 | Popular Tags |