[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/lease/synchronization ...
Ron Sigal
ron_sigal at yahoo.com
Thu Aug 2 02:45:13 EDT 2007
User: rsigal
Date: 07/08/02 02:45:13
Added: src/tests/org/jboss/test/remoting/lease/synchronization
Tag: remoting_2_2_0_GA
MultipleClientSynchronizationTestCase.java
Log:
JBREM-783: New unit tests.
Revision Changes Path
No revision
No revision
1.1.4.2 +273 -0 JBossRemoting/src/tests/org/jboss/test/remoting/lease/synchronization/Attic/MultipleClientSynchronizationTestCase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: MultipleClientSynchronizationTestCase.java
===================================================================
RCS file: MultipleClientSynchronizationTestCase.java
diff -N MultipleClientSynchronizationTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MultipleClientSynchronizationTestCase.java 2 Aug 2007 06:45:13 -0000 1.1.4.2
@@ -0,0 +1,273 @@
+/*
+* 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.synchronization;
+
+import java.net.InetAddress;
+import java.util.HashMap;
+
+import javax.management.MBeanServer;
+
+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.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;
+
+
+/**
+ *
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1.4.2 $
+ * <p>
+ * Copyright Jul 31, 2007
+ * </p>
+ */
+public class MultipleClientSynchronizationTestCase extends TestCase
+{
+ protected static Logger log = Logger.getLogger(MultipleClientSynchronizationTestCase.class);
+ protected static boolean firstTime = true;
+ protected static BooleanHolder go1 = new BooleanHolder();
+ protected static BooleanHolder go2 = new BooleanHolder();
+ protected static BooleanHolder stop1 = new BooleanHolder();
+ protected static BooleanHolder stop2 = new BooleanHolder();
+ protected static InvokerLocator locator;
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+ 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 testMultipleClientsStoppingAndStartingSimultaneously() throws Throwable
+ {
+ log.info("entering " + getName());
+ String host = InetAddress.getLocalHost().getHostAddress();
+ int port = PortUtil.findFreePort(host);
+ String locatorURI = "socket://" + host + ":" + port;
+ locator = new InvokerLocator(locatorURI);
+ Connector connector = new Connector(locator);
+ connector.create();
+ connector.setLeasePeriod(1000);
+ connector.addConnectionListener(new TestListener());
+ TestHandler handler = new TestHandler();
+ connector.addInvocationHandler("test", handler);
+ connector.start();
+
+ int INVOCATIONS = 1000;
+ ClientThread[] threads = new ClientThread[INVOCATIONS];
+
+ // Start clients 0..499.
+ for (int i = 0; i < INVOCATIONS / 2; i++)
+ {
+ threads[i] = new ClientThread(i, go1, stop1);
+ threads[i].start();
+ }
+
+ go1.value = true;
+ Thread.sleep(5000);
+
+ // Stop clients 0..499 and start clients 500..999.
+ for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
+ {
+ threads[i] = new ClientThread(i, go2, stop2);
+ threads[i].start();
+ }
+
+ stop1.value = true;
+ go2.value = true;
+ Thread.sleep(5000);
+
+ // Stop clients 500..999.
+ stop2.value = true;
+ Thread.sleep(10000);
+
+ // Verify that all clients started and stopped successfully.
+ for (int i = 0; i < INVOCATIONS; i++)
+ {
+ assertTrue("failure in thread: " + i, threads[i].ok);
+ }
+
+ connector.stop();
+ log.info(getName() + " PASSES");
+ }
+
+ public void testMultipleClientsStartingStoppingStarting() throws Throwable
+ {
+ log.info("entering " + getName());
+ String host = InetAddress.getLocalHost().getHostAddress();
+ int port = PortUtil.findFreePort(host);
+ String locatorURI = "socket://" + host + ":" + port;
+ locator = new InvokerLocator(locatorURI);
+ Connector connector = new Connector(locator);
+ connector.create();
+ connector.setLeasePeriod(1000);
+ connector.addConnectionListener(new TestListener());
+ TestHandler handler = new TestHandler();
+ connector.addInvocationHandler("test", handler);
+ connector.start();
+
+ int INVOCATIONS = 1000;
+ ClientThread[] threads = new ClientThread[INVOCATIONS];
+
+ // Start clients 0..499.
+ for (int i = 0; i < INVOCATIONS / 2; i++)
+ {
+ threads[i] = new ClientThread(i, go1, stop1);
+ threads[i].start();
+ }
+
+ go1.value = true;
+ Thread.sleep(5000);
+
+ // Stop clients 0..499.
+ stop1.value = true;
+
+ // Start clients 500..999.
+ for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
+ {
+ threads[i] = new ClientThread(i, go2, stop2);
+ threads[i].start();
+ }
+
+ go2.value = true;
+ Thread.sleep(5000);
+
+ // Stop clients 500..999.
+ stop2.value = true;
+ Thread.sleep(10000);
+
+ // Verify that all clients started and stopped successfully.
+ for (int i = 0; i < INVOCATIONS; i++)
+ {
+ assertTrue("failure in thread: " + i, threads[i].ok);
+ }
+
+ connector.stop();
+ log.info(getName() + " PASSES");
+ }
+
+
+ public class ClientThread extends Thread
+ {
+ boolean ok;
+ int id;
+ BooleanHolder startFlag;
+ BooleanHolder stopFlag;
+ Client client;
+
+ public ClientThread(int id, BooleanHolder startFlag, BooleanHolder stopFlag) throws Exception
+ {
+ this.id = id;
+ this.startFlag = startFlag;
+ this.stopFlag = stopFlag;
+
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ config.put(Client.ENABLE_LEASE, "true");
+ config.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
+ client = new Client(locator, config);
+ log.debug("client created (" + id + "): " + client.getSessionId());
+ }
+
+ public void run()
+ {
+ try
+ {
+ while (!startFlag.value)
+ {
+ try {Thread.sleep(10);} catch (InterruptedException e) {}
+ }
+
+ log.debug("client got start flag (" + id + "): " + client.getSessionId());
+ client.connect();
+ log.info("client connected (" + id + "): " + client.getSessionId());
+ }
+ catch (Throwable e)
+ {
+ e.printStackTrace();
+ }
+
+ try
+ {
+ log.debug("client waiting for stop flag (" + id + "): " + client.getSessionId());
+ while (!stopFlag.value)
+ {
+ try {Thread.sleep(10);} catch (InterruptedException e) {}
+ }
+
+ log.debug("client got stop flag (" + id + "): " + client.getSessionId());
+ client.disconnect();
+ log.info("client disconnected (" + id + "): " + client.getSessionId());
+ ok = true;
+ }
+ catch (Throwable e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+ static class TestHandler implements ServerInvocationHandler
+ {
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
+ public void addListener(InvokerCallbackHandler callbackHandler) {}
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ }
+
+
+ public static class TestListener implements ConnectionListener
+ {
+ public void handleConnectionException(Throwable throwable, Client client)
+ {
+ log.debug("got connection exception: " + throwable);
+ }
+ }
+
+
+ static class BooleanHolder
+ {
+ public boolean value;
+ }
+}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list