Author: jfrederic.clere(a)jboss.com
Date: 2008-07-08 03:33:25 -0400 (Tue, 08 Jul 2008)
New Revision: 1740
Added:
trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestFailover.java
Modified:
trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java
trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java
trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java
Log:
Add failover tests.
Modified: trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java 2008-07-07 06:59:42 UTC
(rev 1739)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/Client.java 2008-07-08 07:33:25 UTC
(rev 1740)
@@ -36,6 +36,7 @@
private String URL = null;
private String command = null;
private int nbtest = 10;
+ private int delay = 1000;
private boolean checkcookie = true;
private boolean success = false;
/*
@@ -109,11 +110,13 @@
if (jsessionid == null) {
jsessionid = cookie.getValue();
System.out.println("cookie first time:
" + jsessionid);
+ pm.releaseConnection();
return 0; // first time ok.
} else {
System.out.println("cookie second time:
" + jsessionid);
if (jsessionid.compareTo(cookie.getValue()) == 0)
{
System.out.println("cookie ok");
+ pm.releaseConnection();
return 0;
}
}
@@ -122,8 +125,10 @@
} else {
// Look in the response to make sure that there is a cookie.
int len = (int) pm.getResponseContentLength();
- if (pm.getResponseBodyAsString(len).indexOf(jsessionid) !=
-1)
+ if (pm.getResponseBodyAsString(len).indexOf(jsessionid) !=
-1) {
+ pm.releaseConnection();
return 0;
+ }
System.out.println("No cookies");
}
} else {
@@ -134,9 +139,14 @@
e.printStackTrace();
}
System.out.println("DONE: " + httpResponseCode);
+ pm.releaseConnection();
return httpResponseCode;
}
public void run() {
+ try {
+ sleep(delay);
+ } catch (InterruptedException e) {}
+
for (int i = 0; i < nbtest; i++) {
try {
if (runit() != 0) return;
@@ -150,5 +160,8 @@
public boolean getresultok() {
return success;
}
+ public void setdelay(int delay) {
+ this.delay = delay;
+ }
}
Modified: trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java 2008-07-07 06:59:42
UTC (rev 1739)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/Maintest.java 2008-07-08 07:33:25
UTC (rev 1740)
@@ -34,14 +34,43 @@
import java.lang.Exception;
+import org.apache.catalina.ServerFactory;
+import org.apache.catalina.Service;
+import org.apache.catalina.Engine;
+import org.apache.catalina.core.StandardServer;
+import org.apache.catalina.connector.Connector;
public class Maintest extends TestCase {
+
+ static StandardServer server = null;
public static void main( String args[] ) {
TestRunner.run(suite());
}
public static Test suite() {
TestSuite suite = new TestSuite();
+ server = (StandardServer) ServerFactory.getServer();
suite.addTest(new TestSuite(TestBase.class));
+ System.gc();
+ suite.addTest(new TestSuite(TestFailover.class));
+ System.gc();
return suite;
}
+ static StandardServer getServer() {
+ return server;
+ }
+
+ /* Print the service and connectors the server knows */
+ static void listServices() {
+ Service[] services = server.findServices();
+ for (int i = 0; i < services.length; i++) {
+ System.out.println("service[" + i + "]: " +
services[i]);
+ Engine engine = (Engine) services[i].getContainer();
+ System.out.println("engine: " + engine);
+ System.out.println("connectors: " +
services[i].findConnectors());
+ Connector [] connectors = services[i].findConnectors();
+ for (int j = 0; j < connectors.length; j++) {
+ System.out.println("connector: " + connectors[j]);
+ }
+ }
+ }
}
Modified: trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java 2008-07-07 06:59:42
UTC (rev 1739)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestBase.java 2008-07-08 07:33:25
UTC (rev 1740)
@@ -46,11 +46,12 @@
/* Test */
public void testBase() {
- StandardServer server = null;
+ StandardServer server = Maintest.getServer();
JBossWeb service = null;
JBossWeb service2 = null;
+ ClusterListener cluster = null;
try {
- server = (StandardServer) ServerFactory.getServer();
+ // server = (StandardServer) ServerFactory.getServer();
service = new JBossWeb("node1", "localhost");
service.addConnector(8009);
@@ -60,26 +61,15 @@
service2.addConnector(8000);
server.addService(service2);
- ClusterListener cluster = new ClusterListener();
+ cluster = new ClusterListener();
cluster.setAdvertiseGroupAddress("232.0.0.2");
cluster.setAdvertisePort(23364);
cluster.setSsl(false);
// SSL ?
server.addLifecycleListener((LifecycleListener) cluster);
- Service[] services = server.findServices();
- for (int i = 0; i < services.length; i++) {
- System.out.println("service[" + i + "]: " +
services[i]);
- Engine engine = (Engine) services[i].getContainer();
- System.out.println("engine: " + engine);
- // System.out.println("service: " + engine.getService());
- // System.out.println("connectors: " +
engine.getService().findConnectors());
- System.out.println("JFC connectors: " +
services[i].findConnectors());
- Connector [] connectors = services[i].findConnectors();
- for (int j = 0; j < connectors.length; j++) {
- System.out.println("JFC connector: " + connectors[j]);
- }
- }
+ // Debug Stuff
+ Maintest.listServices();
server.start();
@@ -91,7 +81,7 @@
// Wait until httpd as received the nodes information.
try {
- Thread.sleep(15000);
+ Thread.sleep(20000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
@@ -114,16 +104,20 @@
// Stop the server or services.
try {
+ /// service.stop();
+ /// service2.stop();
server.stop();
- // service.stop();
- // service2.stop();
+ server.removeService(service);
+ server.removeService(service2);
+ server.removeLifecycleListener(cluster);
} catch (LifecycleException ex) {
ex.printStackTrace();
}
// Wait until httpd as received the stop messages.
+ System.gc();
try {
- Thread.sleep(5000);
+ Thread.sleep(20000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
Added: trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestFailover.java
===================================================================
--- trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestFailover.java
(rev 0)
+++ trunk/mod_cluster/test/java/org/jboss/mod_cluster/TestFailover.java 2008-07-08
07:33:25 UTC (rev 1740)
@@ -0,0 +1,195 @@
+/*
+ * mod_cluster
+ *
+ * Copyright(c) 2008 Red Hat Middleware, LLC,
+ * and individual contributors as indicated by the @authors tag.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library in the file COPYING.LIB;
+ * if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ *
+ * @author Jean-Frederic Clere
+ * @version $Revision$
+ */
+
+package org.jboss.mod_cluster;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.catalina.Engine;
+import org.apache.catalina.ServerFactory;
+import org.apache.catalina.Service;
+import org.apache.catalina.LifecycleListener;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.core.StandardServer;
+
+import org.jboss.web.cluster.ClusterListener;
+
+public class TestFailover extends TestCase {
+
+ StandardServer server = null;
+
+ /* Test failover */
+ public void testFailover() {
+
+ boolean clienterror = false;
+ server = Maintest.getServer();
+ JBossWeb service = null;
+ ClusterListener cluster = null;
+ try {
+ // server = (StandardServer) ServerFactory.getServer();
+
+ service = new JBossWeb("node3", "localhost");
+ service.addConnector(8009);
+ server.addService(service);
+
+ cluster = new ClusterListener();
+ cluster.setAdvertiseGroupAddress("232.0.0.2");
+ cluster.setAdvertisePort(23364);
+ cluster.setSsl(false);
+
+ // SSL ?
+ server.addLifecycleListener((LifecycleListener) cluster);
+ Maintest.listServices();
+
+ } catch(IOException ex) {
+ ex.printStackTrace();
+ fail("can't start service");
+ }
+
+ // start a thread.
+ ServerThread wait = new ServerThread(3000);
+ wait.start();
+
+ // Wait until httpd as received the nodes information.
+ try {
+ Thread.sleep(30000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+
+ // Start the client and wait for it.
+ Client client = new Client();
+
+ // Wait for it.
+ try {
+ if (client.runit("http://localhost:7779/ROOT/MyCount",
"cmd", 10, true) != 0)
+ clienterror = true;
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ clienterror = true;
+ }
+
+ // Stop jboss.
+ try {
+ wait.stopit();
+ wait.join();
+ server.removeService(service);
+ server.removeLifecycleListener(cluster);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ fail("can't stop server");
+ }
+
+ if (clienterror)
+ fail("Client error");
+
+ // Start a new one.
+ try {
+ service = new JBossWeb("node4", "localhost");
+ service.addConnector(8000);
+ server.addService(service);
+
+ cluster = new ClusterListener();
+ cluster.setAdvertiseGroupAddress("232.0.0.2");
+ cluster.setAdvertisePort(23364);
+ cluster.setSsl(false);
+
+ // SSL ?
+ server.addLifecycleListener((LifecycleListener) cluster);
+
+ } catch(IOException ex) {
+ ex.printStackTrace();
+ fail("Server start() failed");
+ }
+
+ // Start a server thread.
+ wait = new ServerThread(3000);
+ wait.start();
+
+ // Run a test on it. (it waits until httpd as received the nodes information).
+ try {
+ client.setdelay(30000);
+ client.start();
+ client.join();
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ if (client.getresultok())
+ System.out.println("Test DONE");
+ else {
+ System.out.println("Test FAILED");
+ clienterror = true;
+ }
+
+ // Stop the server or services.
+ try {
+ wait.stopit();
+ wait.join();
+ server.removeService(service);
+ server.removeLifecycleListener(cluster);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+
+ // Wait until httpd as received the stop messages.
+ try {
+ Thread.sleep(5000);
+ } catch (InterruptedException ex) {
+ ex.printStackTrace();
+ }
+
+ // Test client result.
+ if (clienterror)
+ fail("Client test failed");
+ }
+ public class ServerThread extends Thread {
+ int delay;
+ boolean ok = true;
+ public ServerThread(int delay) {
+ this.delay = delay;
+ }
+ public void run() {
+ try {
+ server.start();
+ while (ok) {
+ Thread.sleep(delay);
+ }
+ // sleep(delay);
+ server.stop();
+ } catch (InterruptedException e) {
+ } catch (LifecycleException ex) {
+ ex.printStackTrace();
+ }
+ }
+ public void stopit() {
+ ok = false;
+ }
+ }
+}
Show replies by date