[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/lease/broken ...
Ron Sigal
ron_sigal at yahoo.com
Fri Feb 16 00:05:44 EST 2007
User: rsigal
Date: 07/02/16 00:05:44
Added: src/tests/org/jboss/test/remoting/lease/broken Tag:
remoting_2_x QuickDisconnectTestCase.java
Log:
JBREM-657: Unit test for quickly terminating lease.
Revision Changes Path
No revision
No revision
1.1.2.1 +230 -0 JBossRemoting/src/tests/org/jboss/test/remoting/lease/broken/Attic/QuickDisconnectTestCase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: QuickDisconnectTestCase.java
===================================================================
RCS file: QuickDisconnectTestCase.java
diff -N QuickDisconnectTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ QuickDisconnectTestCase.java 16 Feb 2007 05:05:44 -0000 1.1.2.1
@@ -0,0 +1,230 @@
+/*
+* 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.lease.broken;
+
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.ConnectionListener;
+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.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.socket.LRUPool;
+import org.jboss.remoting.transport.socket.ServerThread;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+
+/**
+ * QuickTimeoutConnectionValidatorTestCase verifies that LeasePinger sets its
+ * own short timeout values when it is called during Client.disconnect(), so that,
+ * even if the server is unavailable, Client.disconnect() can finish quickly.
+ *
+ * This test uses the socket transport. It should be independent of the choice of
+ * transport, as long as the transport supports per-invocation timeouts.
+ *
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1.2.1 $
+ * <p>
+ * Copyright Jan 24, 2007
+ * </p>
+ */
+public class QuickDisconnectTestCase
+ extends TestCase
+ implements ConnectionListener
+{
+ protected static Logger log = Logger.getLogger(QuickDisconnectTestCase.class);
+ protected static boolean firstTime = true;
+
+ protected boolean receivedConnectionException;
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(XLevel.TRACE);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+ }
+
+
+ public void testTimeout() throws Throwable
+ {
+ log.info("entering " + getName());
+ String host = InetAddress.getLocalHost().getHostAddress();
+ int port = PortUtil.findFreePort(host);
+ String locatorURI = getTransport() + "://" + host + ":" + port;
+ InvokerLocator locator = new InvokerLocator(locatorURI);
+ HashMap serverConfig = new HashMap();
+ addServerConfig(serverConfig);
+ final Connector connector = new Connector(locator, serverConfig);
+ connector.create();
+ connector.addInvocationHandler("test", new TestHandler());
+ connector.addConnectionListener(this);
+ connector.start();
+
+ new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ // Give the client a chance to connect to the server, then
+ // disable the server.
+ Thread.sleep(2000);
+ ServerInvoker si = connector.getServerInvoker();
+ assertTrue(si instanceof SocketServerInvoker);
+ SocketServerInvoker ssi = (SocketServerInvoker) si;
+ Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
+ field.setAccessible(true);
+ LRUPool clientpool = (LRUPool) field.get(ssi);
+ Set threads = clientpool.getContents();
+ Iterator it = threads.iterator();
+ while (it.hasNext())
+ {
+ ServerThread t = (ServerThread) it.next();
+ t.shutdown();
+ }
+
+ ssi.setMaxPoolSize(0);
+ log.info("server is disabled");
+ }
+ catch (Exception e)
+ {
+ log.info(e);
+ }
+ }
+ }.start();
+
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ clientConfig.put(ServerInvoker.TIMEOUT, "20000");
+ clientConfig.put(Client.ENABLE_LEASE, "true");
+ addClientConfig(clientConfig);
+ final Client client = new Client(locator, clientConfig);
+ client.connect();
+ Object response = client.invoke("test");
+ assertEquals("test", response);
+
+ new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ // Wait for the server to be disabled.
+ Thread.sleep(4000);
+
+ // This invocation will cause the ServerThread to shut down.
+ client.invoke("test");
+ log.debug("calling client.disconnect()");
+ client.disconnect();
+ log.debug("returned from client.disconnect()");
+ }
+ catch (Throwable e)
+ {
+ log.info("error in client.disconnect()", e);
+ }
+ }
+ }.start();
+
+ // It should take the Client a little while for LeasePinger's attempts to contact
+ // the server to time out. Wait for 4 seconds after the call to Client.disconnect()
+ // and then verify that the Client has successfully disconnected even though the
+ // server is disabled.
+ Thread.sleep(8000);
+ assertFalse(client.isConnected());
+
+ client.disconnect();
+ connector.stop();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void handleConnectionException(Throwable throwable, Client client)
+ {
+ receivedConnectionException = true;
+ }
+
+
+ protected String getTransport()
+ {
+ return "socket";
+ }
+
+
+ protected void addServerConfig(Map config)
+ {
+ }
+
+
+ protected void addClientConfig(Map config)
+ {
+ }
+
+
+ public class TestHandler implements ServerInvocationHandler
+ {
+
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+
+ public Object invoke(InvocationRequest invocation) throws Throwable
+ {
+ return invocation.getParameter();
+ }
+
+ public void addListener(InvokerCallbackHandler callbackHandler) {}
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ }
+
+
+ public class TestListener implements ConnectionListener
+ {
+ public boolean notified;
+
+ public void handleConnectionException(Throwable throwable, Client client)
+ {
+ notified = true;
+ }
+ }
+}
More information about the jboss-cvs-commits
mailing list