01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test.restart;
06:
07: import com.tc.exception.TCRuntimeException;
08: import com.tc.objectserver.control.ServerControl;
09: import com.tc.test.proxyconnect.ProxyConnectManager;
10: import com.tc.util.concurrent.ThreadUtil;
11: import com.tctest.TestState;
12:
13: public class ServerCrasher implements Runnable {
14: private final ServerControl server;
15: private final Thread myThread = new Thread(this , "ServerCrasher");
16: private final long crashInterval;
17: private final TestState testState;
18: private boolean proxyConnectMode = false;
19: private final ProxyConnectManager proxyMgr;
20:
21: public ServerCrasher(final ServerControl server,
22: final long crashInterval, final boolean crash,
23: TestState testState, ProxyConnectManager proxyMgr) {
24: super ();
25: this .server = server;
26: this .crashInterval = crashInterval;
27: this .testState = testState;
28: this .proxyMgr = proxyMgr;
29: }
30:
31: public void startAutocrash() throws Exception {
32: testState.setTestState(TestState.RUNNING);
33: myThread.start();
34: }
35:
36: public void setProxyConnectMode(boolean onoff) {
37: proxyConnectMode = onoff;
38: }
39:
40: public void run() {
41: // initial server start
42: try {
43: synchronized (testState) {
44: if (testState.isRunning()) {
45: System.err.println("Starting server...");
46: server.start();
47: }
48: }
49: } catch (Exception e) {
50: throw new TCRuntimeException(e);
51: }
52:
53: while (true) {
54: ThreadUtil.reallySleep(crashInterval);
55: synchronized (testState) {
56: if (testState.isRunning()) {
57: try {
58: System.err.println("Crashing server...");
59: if (proxyConnectMode) {
60: proxyMgr.stopProxyTest();
61: proxyMgr.proxyDown();
62: }
63: server.crash();
64:
65: if (server.isRunning())
66: throw new AssertionError(
67: "Server is still running even after shutdown or crash.");
68:
69: System.err.println("Starting server...");
70: server.start();
71: if (proxyConnectMode) {
72: proxyMgr.proxyUp();
73: proxyMgr.startProxyTest();
74: }
75:
76: } catch (Exception e) {
77: throw new TCRuntimeException(e);
78: }
79: } else {
80: System.err.println("Shutting down server crasher.");
81: break;
82: }
83: }
84: }
85: }
86: }
|