Author: ron.sigal(a)jboss.com
Date: 2009-04-22 23:39:13 -0400 (Wed, 22 Apr 2009)
New Revision: 5088
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SocketWriteTimeoutTestCase.java
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/WriteTimeoutTestParent.java
Log:
JBREM-1120: New unit tests.
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SocketWriteTimeoutTestCase.java
===================================================================
---
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SocketWriteTimeoutTestCase.java
(rev 0)
+++
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/SocketWriteTimeoutTestCase.java 2009-04-23
03:39:13 UTC (rev 5088)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.transport.socket.timeout;
+
+/**
+ * Unit tests for JBREM-1120.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Rev$
+ * <p>
+ * Copyright Apr 22, 2009
+ * </p>
+ */
+public class SocketWriteTimeoutTestCase extends WriteTimeoutTestParent
+{
+ protected String getTransport()
+ {
+ return "socket";
+ }
+}
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/WriteTimeoutTestParent.java
===================================================================
---
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/WriteTimeoutTestParent.java
(rev 0)
+++
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/transport/socket/timeout/WriteTimeoutTestParent.java 2009-04-23
03:39:13 UTC (rev 5088)
@@ -0,0 +1,652 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.transport.socket.timeout;
+
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.management.MBeanServer;
+import javax.net.ServerSocketFactory;
+import javax.net.SocketFactory;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationFailureException;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+import org.jboss.remoting.transport.bisocket.Bisocket;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+import org.jboss.remoting.transport.socket.SocketWrapper;
+
+
+/**
+ * Unit tests for JBREM-1120.
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version $Rev$
+ * <p>
+ * Copyright Apr 22, 2009
+ * </p>
+ */
+public abstract class WriteTimeoutTestParent extends TestCase
+{
+ private static Logger log = Logger.getLogger(WriteTimeoutTestParent.class);
+
+ private static boolean firstTime = true;
+
+ protected static int SECONDARY_SERVER_SOCKET_PORT = 8765;
+ protected static String SECONDARY_SERVER_SOCKET_PORT_STRING = "8765";
+ protected static boolean callbackTest;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(Level.TRACE);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ 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 tearDown()
+ {
+ }
+
+
+ public void testClientWriteTimeout() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer(false, false, "", -1);
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ clientConfig.put(SocketWrapper.WRITE_TIMEOUT, "1000");
+ clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, new TestSocketFactory(5000));
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ log.info("**************************************");
+ log.info("*** WorkerThread error is expected ***");
+ log.info("**************************************");
+
+ // Test client side write timeout.
+ try
+ {
+ client.invoke("abc");
+ }
+ catch (InvocationFailureException e)
+ {
+ log.info(e.getMessage());
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().startsWith("Unable to perform
invocation"));
+ assertTrue(e.getCause() instanceof IOException);
+ IOException ioe = (IOException) e.getCause();
+ assertEquals("closed", ioe.getMessage());
+ log.info("got expected Exception");
+ }
+ catch (Throwable t)
+ {
+ log.error("got unexpected Exception", t);
+ fail("got unexpected Exception");
+ }
+
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testServerWriteTimeout() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer(true, false, "1000", 5000);
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ clientConfig.put("numberOfCallRetries", "1");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ log.info("**************************************");
+ log.info("*** WorkerThread error is expected ***");
+ log.info("**************************************");
+
+ // Test server side write timeout.
+ try
+ {
+ client.invoke("abc");
+ }
+ catch (InvocationFailureException e)
+ {
+ log.info(e.getMessage());
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().startsWith("Unable to perform
invocation"));
+ assertTrue(e.getCause() instanceof EOFException);
+ log.info("got expected Exception");
+ }
+ catch (Throwable t)
+ {
+ log.error("got unexpected Exception", t);
+ fail("got unexpected Exception");
+ }
+
+ // Test server invoker state.
+ Thread.sleep(4000);
+ SocketServerInvoker serverInvoker = (SocketServerInvoker)
connector.getServerInvoker();
+ assertEquals(0, serverInvoker.getCurrentClientPoolSize());
+ assertEquals(1, serverInvoker.getCurrentThreadPoolSize());
+ log.info("used ServerThread has returned to threadPool");
+
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testClientCallbackWriteTimeout() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer(false, false, "", -1);
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connection
+ client.invoke("abc");
+ log.info("connection is good");
+
+ // Test client callback write timeout.
+ log.info("registering callback handler");
+ log.info("**************************************");
+ log.info("*** WorkerThread error is expected ***");
+ log.info("**************************************");
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ HashMap metadata = new HashMap();
+ if (isBisocket(getTransport()))
+ {
+ metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000");
+ metadata.put(Remoting.SOCKET_FACTORY_NAME, TestSocketFactory.class.getName());
+ metadata.put("numberOfCallRetries", "1");
+ metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
+ metadata.put(Bisocket.PING_FREQUENCY, "11111111");
+ }
+ else
+ {
+ metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000");
+ metadata.put(ServerInvoker.SERVER_SOCKET_FACTORY,
TestServerSocketFactory.class.getName());
+ metadata.put("numberOfCallRetries", "1");
+ }
+ client.addListener(callbackHandler, metadata, null, true);
+
+ // Test server invoker state.
+ Thread.sleep(4000);
+ Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
+ assertEquals(1, callbackConnectors.size());
+ Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
+ SocketServerInvoker serverInvoker = (SocketServerInvoker)
callbackConnector.getServerInvoker();
+ assertEquals(0, serverInvoker.getCurrentClientPoolSize());
+ assertEquals(1, serverInvoker.getCurrentThreadPoolSize());
+ log.info("used ServerThread has returned to threadPool");
+
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public void testServerCallbackWriteTimeout() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ if (isBisocket(getTransport()))
+ {
+ callbackTest = true;
+ setupServer(true, false, "1000", 5000);
+ }
+ else
+ {
+ setupServer(false, true, "1000", 5000);
+ }
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ clientConfig.put("numberOfCallRetries", "1");
+ addExtraClientConfig(clientConfig);
+ Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connection
+ client.invoke("abc");
+ log.info("connection is good");
+
+ // Test server callback write timeout.
+ log.info("registering callback handler");
+ log.info("**************************************");
+ log.info("*** WorkerThread error is expected ***");
+ log.info("**************************************");
+ HashMap metadata = new HashMap();
+ if (isBisocket(getTransport()))
+ {
+ metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
+ }
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+
+ // Test server invoker state.
+ Thread.sleep(4000);
+ Throwable t = invocationHandler.t;
+ assertTrue(t instanceof HandleCallbackException);
+ assertTrue(t.getCause() instanceof InvocationFailureException);
+ InvocationFailureException e = (InvocationFailureException) t.getCause();
+ assertNotNull(e.getMessage());
+ assertTrue(e.getMessage().startsWith("Unable to perform invocation"));
+ assertTrue(e.getCause() instanceof IOException);
+ IOException ioe = (IOException) e.getCause();
+ assertEquals("closed", ioe.getMessage());
+ log.info("got expected Exception");
+
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected abstract String getTransport();
+
+ protected boolean isBisocket(String transport)
+ {
+ return transport.indexOf("bisocket") >= 0;
+ }
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ protected void setupServer(boolean setWriteTimeout, boolean setCallbackWriteTimeout,
+ String writeTimeout, int blockingTime) throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port;
+ String metadata = System.getProperty("remoting.metadata");
+ if (metadata != null)
+ {
+ locatorURI += "/?" + metadata;
+ }
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ if (isBisocket(getTransport()))
+ {
+ config.put(Bisocket.SECONDARY_BIND_PORT, SECONDARY_SERVER_SOCKET_PORT_STRING);
+ config.put(Bisocket.PING_FREQUENCY, "11111111");
+ }
+ if (setWriteTimeout)
+ {
+ config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout);
+ config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, new
TestServerSocketFactory(blockingTime));
+ }
+ if (setCallbackWriteTimeout)
+ {
+ config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout);
+ config.put(Remoting.CUSTOM_SOCKET_FACTORY, new
TestSocketFactory(blockingTime));
+ }
+ if (callbackTest)
+ {
+ config.put("numberOfCallRetries", "1");
+ }
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ Throwable t;
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ try
+ {
+ callbackHandler.handleCallback(new Callback("callback"));
+ }
+ catch (Throwable t)
+ {
+ this.t = t;
+ }
+ }
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ return invocation.getParameter();
+ }
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ log.info("received callback");
+ }
+ }
+
+
+ static public class TestServerSocketFactory extends ServerSocketFactory
+ {
+ int timeout;
+ public TestServerSocketFactory()
+ {
+ this.timeout = 5000;
+ }
+ public TestServerSocketFactory(int timeout)
+ {
+ this.timeout = timeout;
+ }
+ public ServerSocket createServerSocket() throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest)
+ {
+ ss = ServerSocketFactory.getDefault().createServerSocket();
+ }
+ else
+ {
+ ss = new TestServerSocket(timeout);
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+ public ServerSocket createServerSocket(int port) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = ServerSocketFactory.getDefault().createServerSocket(port);
+ }
+ else
+ {
+ ss = new TestServerSocket(port, timeout);
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = ServerSocketFactory.getDefault().createServerSocket(port, backlog);
+ }
+ else
+ {
+ ss = new TestServerSocket(port, backlog, timeout);
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+
+ public ServerSocket createServerSocket(int port, int backlog, InetAddress
ifAddress) throws IOException
+ {
+ ServerSocket ss = null;
+ if (callbackTest && port != SECONDARY_SERVER_SOCKET_PORT)
+ {
+ ss = ServerSocketFactory.getDefault().createServerSocket(port, backlog,
ifAddress);
+ }
+ else
+ {
+ ss = new TestServerSocket(port, backlog, ifAddress, timeout);
+ }
+ log.info("returning: " + ss);
+ return ss;
+ }
+ }
+
+
+ static class TestServerSocket extends ServerSocket
+ {
+ int timeout;
+
+ public TestServerSocket(int timeout) throws IOException
+ {
+ super();
+ this.timeout = timeout;
+ }
+ public TestServerSocket(int port, int timeout) throws IOException
+ {
+ super(port);
+ this.timeout = timeout;
+ }
+ public TestServerSocket(int port, int backlog, int timeout) throws IOException
+ {
+ super(port, backlog);
+ this.timeout = timeout;
+ }
+ public TestServerSocket(int port, int backlog, InetAddress bindAddr, int timeout)
throws IOException
+ {
+ super(port, backlog, bindAddr);
+ this.timeout = timeout;
+ }
+ public Socket accept() throws IOException
+ {
+ Socket s = new TestSocket(timeout);
+ implAccept(s);
+ return s;
+ }
+ public String toString()
+ {
+ return "TestServerSocket[" + getLocalPort() + "]";
+ }
+ }
+
+
+ public static class TestSocketFactory extends SocketFactory
+ {
+ int timeout;
+
+ public TestSocketFactory()
+ {
+ timeout = 5000;
+ }
+ public TestSocketFactory(int timeout)
+ {
+ this.timeout = timeout;
+ }
+ public Socket createSocket()
+ {
+ return new TestSocket(timeout);
+ }
+ public Socket createSocket(String arg0, int arg1) throws IOException,
UnknownHostException
+ {
+ return new TestSocket(arg0, arg1, timeout);
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1) throws IOException
+ {
+ return new TestSocket(arg0, arg1, timeout);
+ }
+
+ public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3)
throws IOException, UnknownHostException
+ {
+ return new TestSocket(arg0, arg1, arg2, arg3, timeout);
+ }
+
+ public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3)
throws IOException
+ {
+ return new TestSocket(arg0, arg1, arg2, arg3, timeout);
+ }
+ }
+
+ static class TestSocket extends Socket
+ {
+ int timeout;
+
+ public TestSocket(int timeout)
+ {
+ this.timeout = timeout;
+ }
+ public TestSocket(String host, int port, int timeout) throws UnknownHostException,
IOException
+ {
+ super(host, port);
+ this.timeout = timeout;
+ }
+ public TestSocket(InetAddress address, int port, int timeout) throws IOException
+ {
+ super(address, port);
+ this.timeout = timeout;
+ }
+ public TestSocket(String host, int port, InetAddress localAddr, int localPort, int
timeout) throws IOException
+ {
+ super(host, port, localAddr, localPort);
+ this.timeout = timeout;
+ }
+ public TestSocket(InetAddress address, int port, InetAddress localAddr, int
localPort, int timeout) throws IOException
+ {
+ super(address, port, localAddr, localPort);
+ this.timeout = timeout;
+ }
+ public OutputStream getOutputStream() throws IOException
+ {
+ return new TestOutputStream(super.getOutputStream(), timeout);
+ }
+ }
+
+ static class TestOutputStream extends OutputStream
+ {
+ OutputStream os;
+ int timeout;
+ boolean closed;
+
+ public TestOutputStream(OutputStream os, int timeout)
+ {
+ this.os = os;
+ this.timeout = timeout;
+ }
+ public void close()throws IOException
+ {
+ closed = true;
+ super.close();
+ log.info("closed");
+ }
+ public void write(int b) throws IOException
+ {
+ if (closed)
+ {
+ log.info("TestOutputStream closed, cannot write");
+ throw new IOException("closed");
+ }
+ os.write(b);
+ }
+ public void write(byte b[], int off, int len) throws IOException
+ {
+ if (closed)
+ {
+ log.info("TestOutputStream closed, cannot write");
+ throw new IOException("closed");
+ }
+ try
+ {
+ log.info("TestOutputStream.write() sleeping: " + timeout);
+ Thread.sleep(timeout);
+ }
+ catch (InterruptedException e)
+ {
+ e.printStackTrace();
+ }
+ log.info("TestOutputStream writing");
+ os.write(b, off, len);
+ }
+ }
+}
\ No newline at end of file