[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/callback/asynch ...
Ron Sigal
ron_sigal at yahoo.com
Tue Nov 28 12:24:57 EST 2006
User: rsigal
Date: 06/11/28 12:24:57
Added: src/tests/org/jboss/test/remoting/callback/asynch Tag:
remoting_2_x AsynchCallbackTestClient.java
AsynchCallbackTestCase.java
AsynchCallbackTestServer.java
Log:
JBREM-640: Unit tests for asynchronous callbacks.
Revision Changes Path
No revision
No revision
1.1.2.1 +145 -0 JBossRemoting/src/tests/org/jboss/test/remoting/callback/asynch/Attic/AsynchCallbackTestClient.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AsynchCallbackTestClient.java
===================================================================
RCS file: AsynchCallbackTestClient.java
diff -N AsynchCallbackTestClient.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ AsynchCallbackTestClient.java 28 Nov 2006 17:24:57 -0000 1.1.2.1
@@ -0,0 +1,145 @@
+/*
+* 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.callback.asynch;
+
+import java.net.InetAddress;
+
+import junit.framework.TestCase;
+
+import org.jboss.logging.Logger;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+
+/**
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1.2.1 $
+ * <p>
+ * Copyright Nov 25, 2006
+ * </p>
+ */
+public class AsynchCallbackTestClient extends TestCase
+{
+ private static Logger log = Logger.getLogger(AsynchCallbackTestClient.class);
+
+
+ public void testSynchronousCallback() throws Throwable
+ {
+ String transport = AsynchCallbackTestServer.transport;
+ String host = InetAddress.getLocalHost().getHostName();
+ int port = AsynchCallbackTestServer.port;
+ String locatorURI = transport + "://" + host + ":" + port;
+ InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+ log.info("Connecting to: " + serverLocator);
+ Client client = new Client(serverLocator);
+ client.connect();
+ log.info("client is connected");
+ SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+ log.info("client added callback handler");
+ client.invokeOneway(AsynchCallbackTestServer.SYNCHRONOUS_TEST);
+ Thread.sleep(1000);
+ // Should still be waiting on client.
+ Boolean done = (Boolean) client.invoke(AsynchCallbackTestServer.GET_STATUS);
+ assertFalse(done.booleanValue());
+ Thread.sleep(2000);
+ done = (Boolean) client.invoke(AsynchCallbackTestServer.GET_STATUS);
+ assertTrue(done.booleanValue());
+ assertTrue(callbackHandler.receivedCallback);
+ client.invoke(AsynchCallbackTestServer.RESET);
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ }
+
+
+ public void testASynchronousCallbackClientSide() throws Throwable
+ {
+ String transport = AsynchCallbackTestServer.transport;
+ String host = InetAddress.getLocalHost().getHostName();
+ int port = AsynchCallbackTestServer.port;
+ String locatorURI = transport + "://" + host + ":" + port;
+ InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+ log.info("Connecting to: " + serverLocator);
+ Client client = new Client(serverLocator);
+ client.connect();
+ log.info("client is connected");
+ SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+ log.info("client added callback handler");
+ client.invokeOneway(AsynchCallbackTestServer.ASYNCHRONOUS_CLIENT_SIDE_TEST);
+ Thread.sleep(100);
+ // Should have returned.
+ Boolean done = (Boolean) client.invoke(AsynchCallbackTestServer.GET_STATUS);
+ assertTrue(done.booleanValue());
+ assertTrue(callbackHandler.receivedCallback);
+ client.invoke(AsynchCallbackTestServer.RESET);
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ }
+
+
+ public void testASynchronousCallbackServerSide() throws Throwable
+ {
+ String transport = AsynchCallbackTestServer.transport;
+ String host = InetAddress.getLocalHost().getHostName();
+ int port = AsynchCallbackTestServer.port;
+ String locatorURI = transport + "://" + host + ":" + port;
+ InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+ log.info("Connecting to: " + serverLocator);
+ Client client = new Client(serverLocator);
+ client.connect();
+ log.info("client is connected");
+ SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
+ client.addListener(callbackHandler, null, null, true);
+ log.info("client added callback handler");
+ client.invokeOneway(AsynchCallbackTestServer.ASYNCHRONOUS_SERVER_SIDE_TEST);
+ Thread.sleep(100);
+ // Should have returned.
+ Boolean done = (Boolean) client.invoke(AsynchCallbackTestServer.GET_STATUS);
+ assertTrue(done.booleanValue());
+ assertTrue(callbackHandler.receivedCallback);
+ client.invoke(AsynchCallbackTestServer.RESET);
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ }
+
+
+ static class SampleCallbackHandler implements InvokerCallbackHandler
+ {
+ boolean receivedCallback;
+
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ log.info("received callback");
+ receivedCallback = true;
+ try
+ {
+ Thread.sleep(2000);
+ }
+ catch (InterruptedException e)
+ {
+ }
+ }
+ }
+}
1.1.2.1 +80 -0 JBossRemoting/src/tests/org/jboss/test/remoting/callback/asynch/Attic/AsynchCallbackTestCase.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AsynchCallbackTestCase.java
===================================================================
RCS file: AsynchCallbackTestCase.java
diff -N AsynchCallbackTestCase.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ AsynchCallbackTestCase.java 28 Nov 2006 17:24:57 -0000 1.1.2.1
@@ -0,0 +1,80 @@
+/*
+* 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.callback.asynch;
+
+import org.apache.log4j.Level;
+import org.jboss.jrunit.harness.TestDriver;
+
+/**
+ * Tests asynchronous callback handling.
+ *
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1.2.1 $
+ * <p>
+ * Copyright Nov 28, 2006
+ * </p>
+ */
+public class AsynchCallbackTestCase extends TestDriver
+{
+ public void declareTestClasses()
+ {
+ addTestClasses(AsynchCallbackTestClient.class.getName(),
+ 1,
+ AsynchCallbackTestServer.class.getName());
+ }
+
+ /**
+ * 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 600000;
+ }
+
+ /**
+ * 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 600000;
+ }
+
+ /**
+ * 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 600000;
+ }
+
+}
\ No newline at end of file
1.1.2.1 +180 -0 JBossRemoting/src/tests/org/jboss/test/remoting/callback/asynch/Attic/AsynchCallbackTestServer.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AsynchCallbackTestServer.java
===================================================================
RCS file: AsynchCallbackTestServer.java
diff -N AsynchCallbackTestServer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ AsynchCallbackTestServer.java 28 Nov 2006 17:24:57 -0000 1.1.2.1
@@ -0,0 +1,180 @@
+/*
+* 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.callback.asynch;
+
+import java.net.InetAddress;
+
+import javax.management.MBeanServer;
+
+import junit.framework.TestCase;
+
+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.AsynchInvokerCallbackHandler;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+
+/**
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1.2.1 $
+ * <p>
+ * Copyright Nov 25, 2006
+ * </p>
+ */
+public class AsynchCallbackTestServer extends ServerTestCase
+{
+ public static String transport = "socket";
+ public static int port = 5413;
+ public static String SYNCHRONOUS_TEST = "synchronousTest";
+ public static String ASYNCHRONOUS_SERVER_SIDE_TEST = "asynchronousServerSideTest";
+ public static String ASYNCHRONOUS_CLIENT_SIDE_TEST = "asynchronousClientSideTest";
+ public static String GET_STATUS = "getStatus";
+ public static String RESET = "reset";
+
+ private static Logger log = Logger.getLogger(AsynchCallbackTestServer.class);
+
+ // remoting server connector
+ private Connector connector;
+ String serverLocatorURI;
+
+
+ /**
+ * Sets up target remoting server.
+ */
+ public void setUp() throws Exception
+ {
+ log.info("entering setUp()");
+ String locatorURI = transport + "://" + InetAddress.getLocalHost().getHostAddress() + ":" + port;
+ InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+ System.out.println("Starting remoting server with locator uri of: " + locatorURI);
+ connector = new Connector(serverLocator);
+ connector.create();
+ SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
+ connector.addInvocationHandler("sample", invocationHandler);
+ connector.start();
+ }
+
+
+ /**
+ * Shuts down the server
+ */
+ public void tearDown()
+ {
+ connector.stop();
+ connector.destroy();
+ }
+
+
+ public static void main(String[] args)
+ {
+ AsynchCallbackTestServer testCase = new AsynchCallbackTestServer();
+ try
+ {
+ testCase.setUp();
+ Thread.sleep(60000);
+ testCase.tearDown();
+ }
+ catch(Throwable e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Simple invocation handler implementation. When callback client's are registered, will
+ * generate callbacks periodically.
+ */
+ static class SampleInvocationHandler implements ServerInvocationHandler
+ {
+ boolean done;
+ AsynchInvokerCallbackHandler callbackHandler;
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ System.out.println("Adding callback listener.");
+ assertTrue(callbackHandler instanceof AsynchInvokerCallbackHandler);
+ this.callbackHandler = (AsynchInvokerCallbackHandler) callbackHandler;
+ }
+
+ public Object invoke(InvocationRequest invocation) throws Throwable
+ {
+ try
+ {
+ String test = (String) invocation.getParameter();
+ if (test.equals(SYNCHRONOUS_TEST))
+ {
+ log.info("making synchronous callback");
+ callbackHandler.handleCallback(new Callback(test));
+ }
+ else if (test.equals(ASYNCHRONOUS_SERVER_SIDE_TEST))
+ {
+ log.info("making asynchronous callback - server side");
+ callbackHandler.handleCallbackOneway(new Callback("callback"), true);
+ }
+ else if (test.equals(ASYNCHRONOUS_CLIENT_SIDE_TEST))
+ {
+ log.info("making asynchronous callback - client side");
+ callbackHandler.handleCallbackOneway(new Callback("callback"));
+ }
+ else if (test.equals(GET_STATUS))
+ {
+ synchronized (this)
+ {
+ log.info("returning status: " + done);
+ return new Boolean(done);
+ }
+ }
+ else if (test.equals(RESET))
+ {
+ synchronized (this)
+ {
+ done = false;
+ }
+ }
+ else
+ {
+ log.error("unrecognized test: " + test);
+ }
+ }
+ catch (HandleCallbackException e)
+ {
+ log.error("Unable to send callback");
+ }
+ synchronized (this)
+ {
+ done = true;
+ log.info("done");
+ }
+ return null;
+ }
+
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+}
More information about the jboss-cvs-commits
mailing list