01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test.activepassive;
06:
07: import com.tctest.TestState;
08:
09: public class ActivePassiveServerCrasher implements Runnable {
10: private static boolean DEBUG = false;
11: private final ActivePassiveServerManager serverManger;
12: private final long serverCrashWaitTimeInSec;
13: private final int maxCrashCount;
14:
15: private int crashCount = 0;
16: private final TestState testState;
17:
18: public ActivePassiveServerCrasher(
19: ActivePassiveServerManager serverManager,
20: long serverCrashWaitTimeInSec, int maxCrashCount,
21: TestState testState) {
22: this .serverManger = serverManager;
23: this .serverCrashWaitTimeInSec = serverCrashWaitTimeInSec;
24: this .maxCrashCount = maxCrashCount;
25: this .testState = testState;
26: }
27:
28: public void run() {
29: while (true) {
30: try {
31: Thread.sleep(serverCrashWaitTimeInSec * 1000);
32: } catch (InterruptedException e1) {
33: serverManger.storeErrors(e1);
34: }
35:
36: synchronized (testState) {
37: if (testState.isRunning()
38: && (maxCrashCount - crashCount) > 0
39: && serverManger.getErrors().isEmpty()) {
40: try {
41: debugPrintln("***** ActivePassiveServerCrasher: about to crash server threadID=["
42: + Thread.currentThread().getName()
43: + "]");
44: serverManger.crashServer();
45:
46: debugPrintln("***** ActivePassiveServerCrasher: about to restart crashed server threadID=["
47: + Thread.currentThread().getName()
48: + "]");
49: serverManger.restartLastCrashedServer();
50:
51: crashCount++;
52: } catch (Exception e) {
53: debugPrintln("***** ActivePassiveServerCrasher: error occured while crashing/restarting server threadID=["
54: + Thread.currentThread().getName()
55: + "]");
56:
57: e.printStackTrace();
58:
59: serverManger.storeErrors(e);
60: }
61: } else {
62: debugPrintln("***** ActivePassiveServerCrasher is done: testStateRunning["
63: + testState.isRunning()
64: + "] errors["
65: + serverManger.getErrors().size()
66: + "] crashCount[" + crashCount + "]");
67: break;
68: }
69: }
70: }
71: }
72:
73: public void debugPrintln(String s) {
74: if (DEBUG) {
75: System.err.println(s);
76: }
77: }
78: }
|