From jboss-remoting-commits at lists.jboss.org Fri Jul 30 15:37:16 2010 Content-Type: multipart/mixed; boundary="===============1930875944654504733==" MIME-Version: 1.0 From: jboss-remoting-commits at lists.jboss.org To: jboss-remoting-commits at lists.jboss.org Subject: [jboss-remoting-commits] JBoss Remoting SVN: r5940 - remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socket. Date: Fri, 30 Jul 2010 15:37:16 -0400 Message-ID: <201007301937.o6UJbGYj027196@svn01.web.mwc.hst.phx2.redhat.com> --===============1930875944654504733== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: ron.sigal(a)jboss.com Date: 2010-07-30 15:37:15 -0400 (Fri, 30 Jul 2010) New Revision: 5940 Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/socke= t/IdleTimerTaskNotifyTestCase.java Log: JBREM-1230: New unit test. Added: remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/s= ocket/IdleTimerTaskNotifyTestCase.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/IdleTimerTaskNotifyTestCase.java (rev 0) +++ remoting2/branches/2.2/src/tests/org/jboss/test/remoting/transport/sock= et/IdleTimerTaskNotifyTestCase.java 2010-07-30 19:37:15 UTC (rev 5940) @@ -0,0 +1,260 @@ +/* + * JBoss, Home of Professional Open Source. + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * as indicated by the @author tags. See the copyright.txt file 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; + +import java.net.InetAddress; +import java.util.HashMap; +import java.util.Map; + +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.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.SocketServerInvoker; + +/** + * Unit test for JBREM-1230. + * = + * @author Ron Sigal + * @version $Revision: 1.1 $ + *
+ * Copyright Jul 30, 2010 + */ +public class IdleTimerTaskNotifyTestCase extends TestCase +{ + private static Logger log =3D Logger.getLogger(IdleTimerTaskNotifyTestC= ase.class); + = + private static boolean firstTime =3D true; + private static String DELAY =3D "delay"; + private static String MAX_POOL_SIZE =3D "2"; + private static String IDLE_TIMEOUT =3D "10"; + = + 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 =3D false; + Logger.getLogger("org.jboss.remoting").setLevel(XLevel.TRACE); + Logger.getLogger("org.jboss.test.remoting").setLevel(XLevel.INFO); + String pattern =3D "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n"; + PatternLayout layout =3D new PatternLayout(pattern); + ConsoleAppender consoleAppender =3D new ConsoleAppender(layout); + Logger.getRootLogger().addAppender(consoleAppender); = + } + } + + = + public void tearDown() + { + } + = + = + public void testIdleTimerTaskNotify() throws Throwable + { + log.info("entering " + getName()); + = + // Start server and verify key parameters. + setupServer(); + SocketServerInvoker serverInvoker =3D (SocketServerInvoker) connecto= r.getServerInvoker(); + assertEquals(MAX_POOL_SIZE, Integer.toString(serverInvoker.getMaxPoo= lSize())); + assertEquals(IDLE_TIMEOUT, Integer.toString(serverInvoker.getIdleTim= eout())); + = + // Set up invocations. + InvokerLocator clientLocator =3D new InvokerLocator(locatorURI); + HashMap clientConfig =3D new HashMap(); + clientConfig.put(InvokerLocator.FORCE_REMOTE, "true"); + addExtraClientConfig(clientConfig); + + TestThread tt1 =3D new TestThread("1", new Integer(25000), clientLoc= ator, clientConfig); + TestThread tt2 =3D new TestThread("2", new Integer(5000), clientLoca= tor, clientConfig); + TestThread tt3 =3D new TestThread("3", new Integer(0), clientLocator= , clientConfig); + tt1.start(); + Thread.sleep(1000); + tt2.start(); + Thread.sleep(1000); + tt3.start(); + Thread.sleep(10000); + = + // At this point, SocketServerInvoker has created a socket for the i= nvocation from tt3, + // has called clientpool.evict(), and is waiting on WorkerThread#0 t= o shut down. = + // * WorkerThread#0 is still running the invocation from tt1. + // * WorkerThread#1 has completed the invocation from tt2 and is wai= ting for another one. + // * The invocation from tt2 is still waiting for a thread. + assertFalse(tt1.ok); + assertTrue(tt2.ok); + assertFalse(tt3.ok); + Thread.sleep(15000); + = + // At this point, WorkerThread#0 is still executing the invocation f= rom tt1, but IdleTimerTask + // has detected that WorkerThread#1 has been idle. It shuts down Wo= rkerThread#1 and calls + // clientpool.notify(). SocketServerInvoker wakes up and creates Wo= rkerThread#2, which processes + // the invocation from tt3. + assertTrue(tt3.ok); + Thread.sleep(5000); + = + // At this point, the invocation from tt1 has completed. + assertTrue(tt1.ok); + = + shutdownServer(); + log.info(getName() + " PASSES"); + } + = + = + protected String getTransport() + { + return "socket"; + } + = + = + protected void addExtraClientConfig(Map config) {} + protected void addExtraServerConfig(Map config) {} + = + + protected void setupServer() throws Exception + { + host =3D InetAddress.getLocalHost().getHostAddress(); + port =3D PortUtil.findFreePort(host); + locatorURI =3D getTransport() + "://" + host + ":" + port + "/?"; + String metadata =3D System.getProperty("remoting.metadata"); + if (metadata !=3D null) + { + locatorURI +=3D "&" + metadata; + } + serverLocator =3D new InvokerLocator(locatorURI); + log.info("Starting remoting server with locator uri of: " + locatorU= RI); + HashMap config =3D new HashMap(); + config.put(InvokerLocator.FORCE_REMOTE, "true"); + config.put("maxPoolSize", MAX_POOL_SIZE); + config.put("idleTimeout", IDLE_TIMEOUT); + addExtraServerConfig(config); + connector =3D new Connector(serverLocator, config); + connector.create(); + invocationHandler =3D new TestInvocationHandler(); + connector.addInvocationHandler("test", invocationHandler); + connector.start(); + } + = + = + protected void shutdownServer() throws Exception + { + if (connector !=3D null) + connector.stop(); + } + = + = + static class TestInvocationHandler implements ServerInvocationHandler + { + public void addListener(InvokerCallbackHandler callbackHandler) {} + public Object invoke(final InvocationRequest invocation) throws Thro= wable + { + Map metadata =3D invocation.getRequestPayload(); + int delay =3D 5000; + if (metadata !=3D null) + { + Object o =3D metadata.get(DELAY); + if (o !=3D null && o instanceof Integer) + { + delay =3D ((Integer)o).intValue(); + } + } + log.info(this + " sleeping for " + delay + " ms"); + Thread.sleep(delay); + log.info(this + " returned from sleep"); + return invocation.getParameter(); + } + public void removeListener(InvokerCallbackHandler callbackHandler) {} + public void setMBeanServer(MBeanServer server) {} + public void setInvoker(ServerInvoker invoker) {} + } + = + = + static class TestThread extends Thread + { + public boolean ok; + String id; + Integer delay; + InvokerLocator locator; + Map config; + = + public TestThread(String id, Integer delay, InvokerLocator locator, = Map config) + { + this.id =3D id; + this.delay =3D delay; + this.locator =3D locator; + this.config =3D config; + setName("TestThread[" + id + "]"); + } + = + public void run() + { + Client client =3D null; + = + try + { + client =3D new Client(locator, config); + client.connect(); + log.info(this + " client connected"); + Map metadata =3D new HashMap(); + metadata.put(DELAY, delay); + if (id.equals(client.invoke(id, metadata))) + { + ok =3D true; + log.info(this + " invocation successful"); + } + } + catch (Throwable e) + { + log.error("invocation on " + client + " failed", e); + } + finally + { + client.disconnect(); + } + } + = + public String toString() + { + return getName(); + } + } +} \ No newline at end of file Property changes on: remoting2/branches/2.2/src/tests/org/jboss/test/remoti= ng/transport/socket/IdleTimerTaskNotifyTestCase.java ___________________________________________________________________ Name: svn:executable + * --===============1930875944654504733==--