001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.management.JMXConnectorProxy;
008: import com.tc.management.beans.L2MBeanNames;
009: import com.tc.management.beans.TCServerInfoMBean;
010: import com.tc.object.config.ConfigVisitor;
011: import com.tc.object.config.DSOClientConfigHelper;
012: import com.tc.object.config.TransparencyClassSpec;
013: import com.tc.simulator.app.ApplicationConfig;
014: import com.tc.simulator.listener.ListenerProvider;
015: import com.tc.util.Assert;
016: import com.tc.util.concurrent.ThreadUtil;
017: import com.tctest.runner.AbstractTransparentApp;
018:
019: import java.io.IOException;
020: import java.util.concurrent.CyclicBarrier;
021:
022: import javax.management.MBeanServerConnection;
023: import javax.management.MBeanServerInvocationHandler;
024: import javax.management.remote.JMXConnector;
025:
026: public class JMXHeartBeatTestApp extends AbstractTransparentApp {
027:
028: public static final String CONFIG_FILE = "config-file";
029: public static final String PORT_NUMBER = "port-number";
030: public static final String HOST_NAME = "host-name";
031: public static final String JMX_PORT = "jmx-port";
032:
033: private final ApplicationConfig config;
034:
035: private final int initialNodeCount = getParticipantCount();
036: private final CyclicBarrier stage1 = new CyclicBarrier(
037: initialNodeCount);
038:
039: private MBeanServerConnection mbsc = null;
040: private JMXConnector jmxc;
041: private TCServerInfoMBean serverMBean;
042:
043: public JMXHeartBeatTestApp(String appId, ApplicationConfig config,
044: ListenerProvider listenerProvider) {
045: super (appId, config, listenerProvider);
046: this .config = config;
047: }
048:
049: public static void visitL1DSOConfig(ConfigVisitor visitor,
050: DSOClientConfigHelper config) {
051:
052: String testClass = JMXHeartBeatTestApp.class.getName();
053: String methodExpression = "* " + testClass + "*.*(..)";
054: config.addWriteAutolock(methodExpression);
055: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
056: config.addIncludePattern(testClass + "$*");
057:
058: // roots
059: spec.addRoot("stage1", "stage1");
060: }
061:
062: public void run() {
063: try {
064: runTest();
065: } catch (Throwable t) {
066: notifyError(t);
067: }
068: }
069:
070: private boolean isServerAlive() {
071: boolean isAlive = false;
072:
073: try {
074: echo("connecting to jmx server....");
075: jmxc = new JMXConnectorProxy("localhost", Integer
076: .parseInt(config.getAttribute(JMX_PORT)));
077: mbsc = jmxc.getMBeanServerConnection();
078: echo("obtained mbeanserver connection");
079: serverMBean = (TCServerInfoMBean) MBeanServerInvocationHandler
080: .newProxyInstance(mbsc,
081: L2MBeanNames.TC_SERVER_INFO,
082: TCServerInfoMBean.class, false);
083: String result = serverMBean.getHealthStatus();
084: echo("got health status: " + result);
085: jmxc.close();
086: isAlive = result.startsWith("OK");
087: } catch (Throwable e) {
088: e.printStackTrace();
089: } finally {
090: if (jmxc != null) {
091: try {
092: jmxc.close();
093: } catch (IOException e) {/**/
094: }
095: }
096: }
097:
098: return isAlive;
099: }
100:
101: private void runTest() throws Throwable {
102:
103: Assert.assertEquals(true, isServerAlive());
104: echo("Server is alive");
105: echo("About to crash server...");
106: config.getServerControl().crash();
107: // has to sleep longer than l1-reconnect timeout
108: ThreadUtil.reallySleep(30 * 1000);
109: Assert.assertEquals(false, isServerAlive());
110: echo("Server is crashed.");
111: echo("About to restart server");
112: config.getServerControl().start();
113: echo("Server restarted.");
114: stage1.await();
115: echo("Server restarted successfully.");
116: Assert.assertEquals(true, isServerAlive());
117: }
118:
119: private static void echo(String msg) {
120: System.out.println(msg);
121: }
122:
123: }
|