[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi ...
Ron Sigal
ron_sigal at yahoo.com
Tue Mar 27 04:22:43 EDT 2007
User: rsigal
Date: 07/03/27 04:22:43
Added: src/tests/org/jboss/test/remoting/detection/jndi Tag:
remoting_2_x CleanDetectionTestServer.java
CleanDetectionTestClient.java
CleanDetectionTestCase.java
Log:
JBREM-730: New unit test.
Revision Changes Path
No revision
No revision
1.1.2.2 +310 -0 JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/CleanDetectionTestServer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CleanDetectionTestServer.java
===================================================================
RCS file: CleanDetectionTestServer.java
diff -N CleanDetectionTestServer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CleanDetectionTestServer.java 27 Mar 2007 08:22:43 -0000 1.1.2.2
@@ -0,0 +1,310 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This 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.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.detection.jndi;
+
+import org.jboss.jrunit.extensions.ServerTestCase;
+import org.jboss.logging.Logger;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.detection.AbstractDetector;
+import org.jboss.remoting.detection.jndi.JNDIDetector;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jnp.server.Main;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Timer;
+
+/**
+ * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
+ * @author <a href="mailto:mazz at jboss.com">John Mazzitelli</a>
+ * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
+ */
+public class CleanDetectionTestServer extends ServerTestCase
+{
+ public static int syncPort = 6502;
+
+ protected static Logger log = Logger.getLogger(CleanDetectionTestServer.class);
+ protected static String transport;
+ protected static int port = 5402;
+ protected static Thread serverThread;
+
+ protected int detectorPort = 1099;
+ protected String contextFactory = "org.jnp.interfaces.NamingContextFactory";
+ protected String urlPackage = "org.jboss.naming:org.jnp.interfaces";
+ protected Main jserver;
+ protected JNDIDetector detector;
+ protected Connector connector;
+
+ public void setUp() throws Exception
+ {
+ final String host = InetAddress.getLocalHost().getHostName();
+
+ serverThread = new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ setupJNDI();
+ setupServer(host);
+ setupDetector();
+ ServerSocket ss = new ServerSocket(syncPort, 0, InetAddress.getLocalHost());
+ log.info("bound to: " + InetAddress.getLocalHost() + ":" + syncPort);
+ Socket s = ss.accept();
+ InputStream is = s.getInputStream();
+ OutputStream os = s.getOutputStream();
+
+ // Indicate server is started.
+ os.write(3);
+ log.info("indicated server started");
+
+ is.read();
+ log.info("got request to shut down server");
+ shutdownServer();
+ disableDetector();
+ log.info("shut down server");
+
+ is.read();
+ log.info("got request to restart server");
+ setupServer(host);
+ setupDetector();
+ os.write(7);
+ log.info("restarted server");
+
+ is.read();
+ log.info("got request to shut down server");
+ shutdownServer();
+ shutdownDetector();
+ shutdownJNDI();
+ log.info("shut down server");
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ e.printStackTrace();
+ }
+ }
+ };
+
+ serverThread.start();
+ }
+
+ protected void setupJNDI() throws Exception
+ {
+ String host = InetAddress.getLocalHost().getHostAddress();
+
+ jserver = new Main();
+ jserver.setPort(detectorPort);
+ jserver.setBindAddress(host);
+ jserver.setRmiPort(31000);
+ jserver.start();
+ System.out.println("Started JNDI server on " + host + ":" + port);
+ }
+
+ protected void shutdownJNDI()
+ {
+ jserver.stop();
+ }
+
+ protected void setupDetector() throws Exception
+ {
+ // we need an MBeanServer to store our network registry and multicast detector services
+ MBeanServer server = MBeanServerFactory.createMBeanServer();
+
+ String detectorHost = InetAddress.getLocalHost().getHostName();
+
+ detector = new JNDIDetector(getConfiguration());
+ // set config info for detector and start it.
+ detector.setPort(detectorPort);
+ detector.setHost(detectorHost);
+ detector.setContextFactory(contextFactory);
+ detector.setURLPackage(urlPackage);
+
+ server.registerMBean(detector, new ObjectName("remoting:type=JNDIDetector"));
+ detector.setCleanDetectionNumber(detector.getCleanDetectionNumber() * 2);
+ detector.start();
+ log.info("JNDIDetector has been created and is listening for new NetworkRegistries to come online");
+ }
+
+
+ protected void shutdownDetector() throws Exception
+ {
+ detector.stop();
+ }
+
+
+ protected void disableDetector() throws Exception
+ {
+ Field field = AbstractDetector.class.getDeclaredField("heartbeatTimer");
+ field.setAccessible(true);
+ Timer heartbeatTimer = (Timer) field.get(detector);
+ heartbeatTimer.cancel();
+ }
+
+
+ /**
+ * Sets up our JBoss/Remoting server by creating our Connector on the given locator URI
+ * and installing our invocation handler that will handle incoming messages.
+ *
+ * @param locatorURI defines our server endpoing
+ * @throws Exception
+ */
+ protected void setupServer(String host) throws Exception
+ {
+ int port = PortUtil.findFreePort(host);
+ String locatorURI = getTransport() + "://" + host + ":" + port;
+ InvokerLocator locator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ connector = new Connector(locator, getConfiguration());
+ connector.create();
+ SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
+ connector.addInvocationHandler("sample", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer()
+ {
+ String locatorURI = connector.getLocator().getLocatorURI();
+ connector.stop();
+ log.info("Stopped remoting server with locator uri of: " + locatorURI);
+ }
+
+
+ protected String getTransport()
+ {
+ return "socket";
+ }
+
+
+ /**
+ * @return configuration map for Connector and JNDIDetector
+ */
+ protected Map getConfiguration()
+ {
+ return new HashMap();
+ }
+
+
+ public static void main(String[] args)
+ {
+ try
+ {
+ CleanDetectionTestServer server = new CleanDetectionTestServer();
+ server.setUp();
+ serverThread.join();
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Simple invocation handler implementation. This is the handler that processes incoming messages from clients.
+ */
+ public static class SampleInvocationHandler implements ServerInvocationHandler
+ {
+ /**
+ * This is the method that is called when a new message comes in from a client.
+ *
+ * @param invocation the incoming client invocation, encapsulates the message object
+ * @return the response object we send back to the client.
+ * @throws Throwable
+ */
+ public Object invoke(InvocationRequest invocation) throws Throwable
+ {
+ // Print out the invocation request
+ String msg = invocation.getParameter().toString();
+
+ log.info("RECEIVED A CLIENT MESSAGE: " + msg);
+
+ String response = "Server received your message that said [" + msg + "]";
+
+ if(msg.indexOf("Welcome") > -1)
+ {
+ response = "Received your welcome message. Thank you!";
+ }
+
+ log.info("Returning the following message back to the client: " + response);
+
+ return response;
+ }
+
+ /**
+ * Adds a callback handler that will listen for callbacks from the server invoker handler.
+ *
+ * @param callbackHandler
+ */
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ // NO OP as we do not handle callback listeners in this example
+ }
+
+ /**
+ * Removes the callback handler that was listening for callbacks from the server invoker handler.
+ *
+ * @param callbackHandler
+ */
+ public void removeListener(InvokerCallbackHandler callbackHandler)
+ {
+ // NO OP as we do not handle callback listeners in this example
+ }
+
+ /**
+ * set the mbean server that the handler can reference
+ *
+ * @param server
+ */
+ public void setMBeanServer(MBeanServer server)
+ {
+ // NO OP as we do not need a reference to the MBeanServer for this handler
+ }
+
+ /**
+ * set the invoker that owns this handler
+ *
+ * @param invoker
+ */
+ public void setInvoker(ServerInvoker invoker)
+ {
+ // NO OP as we do not need a reference back to the server invoker
+ }
+ }
+}
1.1.2.2 +176 -0 JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/CleanDetectionTestClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CleanDetectionTestClient.java
===================================================================
RCS file: CleanDetectionTestClient.java
diff -N CleanDetectionTestClient.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CleanDetectionTestClient.java 27 Mar 2007 08:22:43 -0000 1.1.2.2
@@ -0,0 +1,176 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This 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.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.detection.jndi;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.util.Properties;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameAlreadyBoundException;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+import junit.framework.TestCase;
+
+import org.jboss.logging.Logger;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.detection.Detection;
+import org.jboss.remoting.detection.jndi.JNDIDetector;
+
+/**
+ * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
+ * @author <a href="mailto:mazz at jboss.com">John Mazzitelli</a>
+ * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
+ */
+public class CleanDetectionTestClient extends TestCase //implements NotificationListener
+{
+ private static Logger log = Logger.getLogger(CleanDetectionTestClient.class);
+
+ private String detectorHost;
+ private int detectorPort = 1099;
+ private String contextFactory = "org.jnp.interfaces.NamingContextFactory";
+ private String urlPackage = "org.jboss.naming:org.jnp.interfaces";
+
+ protected JNDIDetector detector;
+ protected int serversDetected;
+ protected boolean invocationSucceeded;
+ protected Object lock = new Object();
+ protected boolean notified;
+
+ private Context context;
+
+
+ public void testCleanDetect()
+ {
+ try
+ {
+ String host = InetAddress.getLocalHost().getHostName();
+
+ Socket s = null;
+ for (int i = 0; i < 5; i++)
+ {
+ try
+ {
+ s = new Socket(host, CleanDetectionTestServer.syncPort);
+ break;
+ }
+ catch (Exception e)
+ {
+ log.info("Unable to connect to " + host + ":" + CleanDetectionTestServer.syncPort);
+ log.info("Will try again");
+ try
+ {
+ Thread.sleep(2000);
+ }
+ catch (InterruptedException ignored) {}
+ }
+ }
+ InputStream is = s.getInputStream();
+ OutputStream os = s.getOutputStream();
+
+ // Wait until server has been started.
+ is.read();
+
+ // Get detection message from JNDI server.
+ createContext();
+ NamingEnumeration enumeration = context.listBindings("");
+ assertTrue(enumeration.hasMore());
+ Binding binding = (Binding) enumeration.next();
+ assertFalse(enumeration.hasMore());
+ log.info(binding);
+ assertTrue(binding.getObject() instanceof Detection);
+ Detection detection = (Detection) binding.getObject();
+ assertEquals(1, detection.getLocators().length);
+ InvokerLocator locator = detection.getLocators()[0];
+ log.info("locator: " + locator);
+
+ // Tell server to shut down.
+ os.write(5);
+
+ // Tell server to restart.
+ os.write(7);
+ Thread.sleep(2000);
+
+ // Get new detection message from JNDI server.
+ enumeration = context.listBindings("");
+ assertTrue(enumeration.hasMore());
+ binding = (Binding) enumeration.next();
+ log.info(binding);
+ assertFalse(enumeration.hasMore());
+ assertTrue(binding.getObject() instanceof Detection);
+ detection = (Detection) binding.getObject();
+ assertEquals(1, detection.getLocators().length);
+ InvokerLocator newLocator = detection.getLocators()[0];
+ log.info("new locator: " + newLocator);
+
+ // Verify that JNDIDetector has already discovered that old server is dead and
+ // has registered new server.
+ assertFalse(locator.equals(newLocator));
+
+ // Tell server test is over.
+ os.write(9);
+ }
+ catch (Exception e)
+ {
+ log.error(e);
+ e.printStackTrace();
+ fail();
+ }
+ }
+
+
+ private void createContext() throws Exception
+ {
+ detectorHost = InetAddress.getLocalHost().getHostName();
+
+ Properties env = new Properties();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);
+ env.put(Context.PROVIDER_URL, detectorHost + ":" + detectorPort);
+ env.put(Context.URL_PKG_PREFIXES, urlPackage);
+
+ InitialContext initialContext = new InitialContext(env);
+
+ String subContextName = JNDIDetector.DETECTION_SUBCONTEXT_NAME;
+ try
+ {
+ context = (Context) initialContext.lookup(subContextName);
+ }
+ catch(NamingException e)
+ {
+ try
+ {
+ context = initialContext.createSubcontext(subContextName);
+ }
+ catch(NameAlreadyBoundException e1)
+ {
+ log.debug("The sub context " + subContextName + " was created before we could.");
+ context = (Context) initialContext.lookup(subContextName);
+ }
+ }
+ }
+
+}
1.1.2.2 +99 -0 JBossRemoting/src/tests/org/jboss/test/remoting/detection/jndi/CleanDetectionTestCase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: CleanDetectionTestCase.java
===================================================================
RCS file: CleanDetectionTestCase.java
diff -N CleanDetectionTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ CleanDetectionTestCase.java 27 Mar 2007 08:22:43 -0000 1.1.2.2
@@ -0,0 +1,99 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This 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.1 of
+* the License, or (at your option) any later version.
+*
+* This software 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 software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.remoting.detection.jndi;
+
+import org.apache.log4j.Level;
+import org.jboss.jrunit.harness.TestDriver;
+
+/**
+ * In this JNDIDetector test case, the server will
+ *
+ * <ol>
+ * <li>start a Connector and a JNDIDetector
+ * <li>stop the Connector and disable the JNDIDetector, leaving a stale reference to the
+ * Connector in the JNDI server
+ * <li>start a new Connector and JNDIDetector
+ * </ol>
+ *
+ * The client will get the JNDI bindings after the first Connector has been started, then
+ * get the JNDI bindings shortly after the second Connector has been started. The JNDIConnector
+ * should have done a clean detection when the heartbeat started and registered the new
+ * Connector with the JNDI server.
+ *
+ * See JIRA issue JBREM-730.
+ *
+ * @author <a href="mailto:telrod at e2technologies.net">Tom Elrod</a>
+ * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
+ */
+public class CleanDetectionTestCase extends TestDriver
+{
+
+ /**
+ * This method should call the addTestClasses() method with the client class to run, number of clients to run
+ * and the server class to run.
+ */
+ public void declareTestClasses()
+ {
+ addTestClasses(CleanDetectionTestClient.class.getName(),
+ 1,
+ CleanDetectionTestServer.class.getName());
+ }
+
+ protected Level getTestLogLevel()
+ {
+ return Level.INFO;
+ }
+
+ /**
+ * How long to wait for test results to be returned from the client(s). If goes longer than the
+ * specified limit, will throw an exception and kill the running test cases. Default value is
+ * RESULTS_TIMEOUT.
+ *
+ * @return
+ */
+ protected long getResultsTimeout()
+ {
+ return 300000;
+ }
+
+ /**
+ * How long for the server test case to wait for tear down message. If exceeds timeout,
+ * will throw exception. The default value is TEARDOWN_TIMEOUT.
+ *
+ * @return
+ */
+ protected long getTearDownTimeout()
+ {
+ return 300000;
+ }
+
+ /**
+ * How long to allow each of the test cases to run their tests. If exceeds this timeout
+ * will throw exception and kill tests. The default value is RUN_TEST_TIMEOUT.
+ *
+ * @return
+ */
+ protected long getRunTestTimeout()
+ {
+ return 300000;
+ }
+}
More information about the jboss-cvs-commits
mailing list