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.server.appserver.unit;
006:
007: import com.meterware.httpunit.WebConversation;
008: import com.tc.process.HeartBeatService;
009: import com.tc.test.ProcessInfo;
010: import com.tc.test.server.appserver.deployment.AbstractTwoServerDeploymentTest;
011: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
012: import com.tc.test.server.appserver.deployment.GenericServer;
013: import com.tc.test.server.appserver.deployment.WebApplicationServer;
014: import com.tc.test.server.util.TcConfigBuilder;
015: import com.tc.util.runtime.Os;
016: import com.tctest.webapp.servlets.ShutdownNormallyServlet;
017:
018: public class AppServerShutdownTestBase extends
019: AbstractTwoServerDeploymentTest {
020: private static final String CONTEXT = "AppServerShutdownTest";
021: private static final String SERVLET = "ShutdownNormallyServlet";
022:
023: private static final int TIME_WAIT_FOR_SHUTDOWN = 3 * 60 * 1000;
024: private final boolean dsoEnabled;
025:
026: public AppServerShutdownTestBase(boolean dsoEnabled) {
027: this .dsoEnabled = dsoEnabled;
028: }
029:
030: public final void testShutdown() throws Exception {
031: WebConversation wc = new WebConversation();
032:
033: final String response2;
034: if (dsoEnabled) {
035: response2 = "OK";
036: } else {
037: response2 = "ERROR: null";
038: }
039:
040: assertEquals("OK", request(server0, "cmd=insert", wc));
041: assertEquals(response2, request(server1, "cmd=query", wc));
042:
043: System.out.println("Shut down app server normally...");
044: getServerManager().stopAllWebServers();
045: System.out.println("Shutting down completed.");
046:
047: if (!Os.isLinux()) { // can't get full command line args in linux
048: System.out.println("Grepping for java processes...");
049: assertFalse("Cargo processes still linger",
050: checkProcesses());
051: }
052:
053: System.out.println("Polling heartbeat threads...");
054: assertFalse("Linked child processes are still alive",
055: checkAlive());
056: }
057:
058: private String request(WebApplicationServer server, String params,
059: WebConversation con) throws Exception {
060: return server.ping(
061: "/" + CONTEXT + "/" + SERVLET + "?" + params, con)
062: .getText().trim();
063: }
064:
065: /**
066: * return true if app server processes are found
067: */
068: private boolean checkProcesses() throws Exception {
069: boolean found = false;
070: String processes_after;
071: long start = System.currentTimeMillis();
072: do {
073: Thread.sleep(1000);
074: processes_after = ProcessInfo.ps_grep_java();
075: found = processes_after.indexOf("CargoLinkedChildProcess") > 0;
076: } while (found
077: && System.currentTimeMillis() - start < TIME_WAIT_FOR_SHUTDOWN);
078:
079: if (found) {
080: System.out.println(processes_after);
081: }
082:
083: return found;
084: }
085:
086: /**
087: * check server status by pinging its linked-child-process return true if any app server is still alive
088: */
089: private boolean checkAlive() throws Exception {
090: long start = System.currentTimeMillis();
091: boolean foundAlive = false;
092: do {
093: Thread.sleep(1000);
094: foundAlive = HeartBeatService.anyAppServerAlive();
095: } while (foundAlive
096: && System.currentTimeMillis() - start < TIME_WAIT_FOR_SHUTDOWN);
097:
098: return foundAlive;
099: }
100:
101: protected static class AppServerShutdownTestSetup extends
102: TwoServerTestSetup {
103:
104: public AppServerShutdownTestSetup(Class testClass,
105: boolean enableDso) {
106: super (testClass, CONTEXT);
107: GenericServer.setDsoEnabled(enableDso);
108: }
109:
110: protected void configureWar(DeploymentBuilder builder) {
111: builder.addServlet(SERVLET, "/" + SERVLET + "/*",
112: ShutdownNormallyServlet.class, null, false);
113: }
114:
115: protected void configureTcConfig(TcConfigBuilder clientConfig) {
116: clientConfig.addWebApplication(CONTEXT, true);
117: }
118: }
119: }
|