1 4 package com.tc.test.restart; 5 6 import org.apache.commons.io.FileUtils; 7 8 import com.tc.config.schema.MockIllegalConfigurationChangeHandler; 9 import com.tc.config.schema.setup.ConfigurationSetupException; 10 import com.tc.config.schema.setup.StandardTVSConfigurationSetupManagerFactory; 11 import com.tc.config.schema.test.L2ConfigBuilder; 12 import com.tc.config.schema.test.TerracottaConfigBuilder; 13 import com.tc.logging.TCLogger; 14 import com.tc.logging.TCLogging; 15 import com.tc.objectserver.control.ExtraProcessServerControl; 16 import com.tc.objectserver.control.IntraProcessServerControl; 17 import com.tc.objectserver.control.NullServerControl; 18 import com.tc.objectserver.control.ServerControl; 19 import com.tc.objectserver.control.ExtraProcessServerControl.DebugParams; 20 import com.tc.util.PortChooser; 21 import com.tctest.restart.TestThreadGroup; 22 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.PrintWriter ; 27 import java.util.Collection ; 28 29 public class RestartTestEnvironment { 30 31 public static final OperatingMode DEV_MODE = new OperatingMode(); 32 public static final OperatingMode PROD_MODE = new OperatingMode(); 33 34 private static TCLogger logger = TCLogging 35 .getTestingLogger(RestartTestEnvironment.class); 36 private final PortChooser portChooser; 37 private StandardTVSConfigurationSetupManagerFactory config; 38 private File configFile; 39 40 private File dbhome; 41 private ServerControl server; 42 private final ServerControl serverWrapper = new ServerWrapper(); 43 private TestThreadGroup threadGroup; 44 45 private boolean isPersistent = true; 46 private boolean isParanoid = true; 47 private final File tempDirectory; 48 private boolean setUp; 49 private boolean mergeServerOutput = true; 50 private int serverPort; 51 private int adminPort; 52 private final OperatingMode operatingMode; 53 54 public RestartTestEnvironment(File tempDirectory, PortChooser portChooser, OperatingMode operatingMode) { 55 this.tempDirectory = tempDirectory; 56 this.portChooser = portChooser; 57 this.operatingMode = operatingMode; 58 if (!tempDirectory.isDirectory()) { 59 throw new AssertionError ("Temp directory is not a directory: " + tempDirectory); 61 } 62 this.configFile = new File (this.tempDirectory, "restart-test-config.xml"); 63 } 64 65 public void setIsPersistent(boolean b) { 66 this.isPersistent = b; 67 } 68 69 public void setIsParanoid(boolean b) { 70 this.isParanoid = b; 71 } 72 73 public void setUp() throws Exception { 74 writeL2Config(); 75 initConfig(); 76 77 dbhome = new File (this.tempDirectory, "l2-data/objectdb"); 78 System.err.println("DBHome: " + dbhome.getAbsolutePath()); 79 System.out.println("dbhome: " + dbhome); 80 if (dbhome.exists()) FileUtils.cleanDirectory(dbhome); 81 if (server != null && server.isRunning()) { 82 logger.info("L2 is running... Shutting it down."); 83 server.shutdown(); 84 for (int i = 0; i < 3 && server.isRunning(); i++) { 85 Thread.sleep(1000); 86 } 87 } 88 logger.info("Making sure L2 isn't running..."); 89 if (server != null && server.isRunning()) throw new AssertionError ("L2 is currently running, but shouldn't be."); 90 threadGroup = new TestThreadGroup(Thread.currentThread().getThreadGroup(), "TEST THREAD GROUP"); 91 92 this.server = new NullServerControl(); 93 this.setUp = true; 94 } 95 96 private void initConfig() throws Exception { 97 config = new StandardTVSConfigurationSetupManagerFactory(new String [] { 99 StandardTVSConfigurationSetupManagerFactory.CONFIG_SPEC_ARGUMENT_WORD, this.configFile.getAbsolutePath() }, 100 true, new MockIllegalConfigurationChangeHandler()); 101 } 102 103 private void writeL2Config() throws Exception { 104 assertServerOff(); 105 106 TerracottaConfigBuilder builder = TerracottaConfigBuilder.newMinimalInstance(); 107 108 String configurationModel; 109 if (operatingMode == DEV_MODE) { 110 configurationModel = "development"; 111 } else if (operatingMode == PROD_MODE) { 112 configurationModel = "production"; 113 } else { 114 throw new AssertionError ("Unknown operating mode."); 115 } 116 builder.getSystem().setConfigurationModel(configurationModel); 117 118 if (this.adminPort > 0) builder.getServers().getL2s()[0].setJMXPort(this.adminPort); 119 if (this.serverPort > 0) builder.getServers().getL2s()[0].setDSOPort(this.serverPort); 120 121 String persistenceMode = L2ConfigBuilder.PERSISTENCE_MODE_TEMPORARY_SWAP_ONLY; 122 if (isPersistent && isParanoid) { 123 persistenceMode = L2ConfigBuilder.PERSISTENCE_MODE_PERMANENT_STORE; 124 } else if (isPersistent) persistenceMode = L2ConfigBuilder.PERSISTENCE_MODE_TEMPORARY_SWAP_ONLY; 125 126 builder.getServers().getL2s()[0].setPersistenceMode(persistenceMode); 127 builder.getServers().getL2s()[0].setData(tempDirectory.getAbsolutePath()); 128 129 String configAsString = builder.toString(); 130 131 System.err.println("Writing config to file:" + configFile.getAbsolutePath() + configAsString); 132 FileOutputStream fileOutputStream = new FileOutputStream (configFile); 133 PrintWriter out = new PrintWriter ((fileOutputStream)); 134 out.println(configAsString); 135 out.flush(); 136 out.close(); 137 138 initConfig(); 139 } 140 141 public Collection getThreadGroupErrors() { 142 return this.threadGroup.getErrors(); 143 } 144 145 public void startNewClientThread(Runnable runnable) { 146 new Thread (this.threadGroup, runnable).start(); 147 } 148 149 public boolean hasThreadGroupErrors() { 150 return threadGroup.getErrors().size() > 0; 151 } 152 153 public boolean hasActiveClients() { 154 return this.threadGroup.activeCount() > 0; 155 } 156 157 public ThreadGroup getThreadGroup() { 158 return this.threadGroup; 159 } 160 161 public ServerControl getServer() { 162 return this.server; 163 } 164 165 public void startServer(long timeout) throws Exception { 166 assertServerOff(); 167 assertSetUp(); 168 server.start(timeout); 169 } 170 171 public void shutdownServer() throws Exception { 172 assertServerNotNull(); 173 server.shutdown(); 174 } 175 176 public ServerControl newExtraProcessServer() throws FileNotFoundException { 177 assertServerOff(); 178 this.server = new ExtraProcessServerControl(new DebugParams(), "localhost", serverPort, adminPort, this.configFile 179 .getAbsolutePath(), mergeServerOutput); 180 return serverWrapper; 181 } 182 183 public ServerControl newIntraProcessServer() throws ConfigurationSetupException { 184 assertServerOff(); 185 this.server = new IntraProcessServerControl(this.config.createL2TVSConfigurationSetupManager(null), "localhost"); 186 return serverWrapper; 187 } 188 189 private void assertServerNotNull() { 190 if (this.server == null) throw new AssertionError ("Server is null."); 191 } 192 193 private void assertServerOff() { 194 if (this.server != null && this.server.isRunning()) { 195 throw new AssertionError ("Server is not off."); 197 } 198 } 199 200 private void assertServerOn() { 201 assertServerNotNull(); 202 if (!this.server.isRunning()) { throw new AssertionError ("Server is not on."); } 203 } 204 205 private void assertSetUp() { 206 if (!this.setUp) throw new AssertionError ("Not set up."); 207 } 208 209 public File getDBHome() { 210 return this.dbhome; 211 } 212 213 public void setServerPort(int i) { 214 this.serverPort = i; 215 } 216 217 public void setAdminPort(int i) { 218 this.adminPort = i; 219 } 220 221 public int chooseServerPort() { 222 this.serverPort = portChooser.chooseRandomPort(); 223 return this.serverPort; 224 } 225 226 public int chooseAdminPort() { 227 this.adminPort = portChooser.chooseRandomPort(); 228 return this.adminPort; 229 } 230 231 public void choosePorts() { 232 chooseServerPort(); 233 chooseAdminPort(); 234 } 235 236 public int getServerPort() { 237 return this.serverPort; 238 } 239 240 public static final class OperatingMode { 241 private OperatingMode() { 242 } 244 } 245 246 private final class ServerWrapper implements ServerControl { 247 248 public void mergeSTDOUT() { 249 assertServerOff(); 250 server.mergeSTDOUT(); 251 } 252 253 public void mergeSTDERR() { 254 assertServerOff(); 255 server.mergeSTDERR(); 256 } 257 258 public void attemptShutdown() throws Exception { 259 assertServerOn(); 260 server.attemptShutdown(); 261 } 262 263 public void shutdown() throws Exception { 264 assertServerOn(); 265 server.shutdown(); 266 } 267 268 public void crash() throws Exception { 269 assertServerOn(); 270 server.crash(); 271 } 272 273 public void start(long timeout) throws Exception { 274 assertSetUp(); 275 assertServerNotNull(); 276 server.start(timeout); 277 } 278 279 public boolean isRunning() { 280 assertServerNotNull(); 281 return server.isRunning(); 282 } 283 284 public void clean() { 285 assertServerNotNull(); 286 assertServerOff(); 287 server.clean(); 288 } 289 290 public void waitUntilShutdown() throws Exception { 291 assertServerNotNull(); 292 server.waitUntilShutdown(); 293 } 294 295 public int getDsoPort() { 296 assertServerNotNull(); 297 return server.getDsoPort(); 298 } 299 } 300 } 301 | Popular Tags |