[jboss-cvs] JBossRemoting/src/tests/org/jboss/test/remoting/callback/acknowledge ...

Ron Sigal ron_sigal at yahoo.com
Mon Sep 25 19:48:34 EDT 2006


  User: rsigal  
  Date: 06/09/25 19:48:34

  Added:       src/tests/org/jboss/test/remoting/callback/acknowledge 
                        CallbackAcknowledgeTestCase.java
  Log:
  JBREM-605:  Added CallbackAcknowledgeTestCase.
  
  Revision  Changes    Path
  1.1      date: 2006/09/25 23:48:34;  author: rsigal;  state: Exp;JBossRemoting/src/tests/org/jboss/test/remoting/callback/acknowledge/CallbackAcknowledgeTestCase.java
  
  Index: CallbackAcknowledgeTestCase.java
  ===================================================================
  /*
  * 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.
  */
  
  
  /**
   * Tests Callback acknowledgements.
   * 
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   * <p/>
   * Copyright (c) 2006
   * </p>
   */
  package org.jboss.test.remoting.callback.acknowledge;
  
  import java.net.InetAddress;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import javax.management.MBeanServer;
  
  import org.jboss.logging.Logger;
  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.Callback;
  import org.jboss.remoting.callback.CallbackListener;
  import org.jboss.remoting.callback.CallbackPoller;
  import org.jboss.remoting.callback.HandleCallbackException;
  import org.jboss.remoting.callback.InvokerCallbackHandler;
  import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
  import org.jboss.remoting.transport.Connector;
  import org.jboss.remoting.transport.PortUtil;
  
  import junit.framework.TestCase;
  
  
  public class CallbackAcknowledgeTestCase extends TestCase
  {
     private static String PREPROCESS_TEST = "preprocessTest";
     private static String POSTPROCESS_TEST = "postprocessTest";
     private static Logger log = Logger.getLogger(CallbackAcknowledgeTestCase.class);
     
     private Connector connector;
     private InvokerLocator serverLocator;
     private String transport = "socket";
     private Client client;
  
     
     public void setUp() throws Exception
     {
        String host = InetAddress.getLocalHost().getHostAddress();
        int freePort = PortUtil.findFreePort(host);
        serverLocator = new InvokerLocator(transport + "://" + host + ":" + freePort);
        connector = new Connector(serverLocator);
        connector.start();
        connector.addInvocationHandler("test", new TestInvoocationHandler());
        
        HashMap config = new HashMap();
        config.put(InvokerLocator.FORCE_REMOTE, "true");
        client = new Client(serverLocator, config);
        client.connect();
        
        TestInvoocationHandler.acknowledgedPreprocess = false;
        TestInvoocationHandler.acknowledgedPostprocess = false;
        TestCallbackHandler.receivedCallback = false;
     }
     
     
     public void tearDown()
     {
        if (connector != null)
           connector.stop();
        
        if (client != null)
           client.disconnect();
     }
  
     
     /**
      * In this test the connection is configured for true pull callbacks, and
      * the acknowledgement should be made implicitly by ServerInvokerCallbackHandler
      * during the call to getCallbacks() (before the callbacks are retrieved by
      * the client).
      */
     public void testPullCallbackPreprocessAcknowledgement()
     {
        log.info("entering " + getName());
        try
        {
           TestCallbackHandler callbackHandler = new TestCallbackHandler();
           client.addListener(callbackHandler);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           client.invoke(PREPROCESS_TEST);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           List callbacks = client.getCallbacks(callbackHandler);
           assertTrue(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           Callback callback = (Callback) callbacks.get(0);
  
           try
           {
              client.acknowledgeCallback(callbackHandler, callback);
              log.error("didn't get expected exception");
              fail();
           }
           catch (Exception e)
           {
              assertTrue("Will not acknowledge Callback because it has no registered Listener.".equals(e.getMessage()));
           }
           
           Thread.sleep(1000);
           assertFalse(TestCallbackHandler.receivedCallback);
           log.info(getName() + " PASSES");
        }
        catch (Throwable e)
        {
           log.info(getName() + " FAILS");
           e.printStackTrace();
           fail();
        }
     }
     
     
     /**
      * In this test, the connection is configured for true pull callbacks, and the 
      * acknowledgement should be made by an explicit call to Client.acknowledgeCallback()
      * after the callback has been retrieved and (presumably) processed.
      */
     public void testPullCallbackPostprocessAcknowledgement()
     {
        log.info("entering " + getName());
        try
        {
           TestCallbackHandler callbackHandler = new TestCallbackHandler();
           client.addListener(callbackHandler);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           client.invoke(POSTPROCESS_TEST);
           List callbacks = client.getCallbacks(callbackHandler);
           Callback callback = (Callback) callbacks.get(0);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           client.acknowledgeCallback(callbackHandler, callback);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertTrue(TestInvoocationHandler.acknowledgedPostprocess);
           Thread.sleep(1000);
           assertFalse(TestCallbackHandler.receivedCallback);
           log.info(getName() + " PASSES");
        }
        catch (Throwable e)
        {
           log.info(getName() + " FAILS");
           e.printStackTrace();
           fail();
        }
     }
     
     
     /**
      * In this test the connection is configured for push callbacks implemented in
      * Remoting by polling the server for callbacks and pushing them (on the client 
      * side) to the InvokerCallbackHandler.  The acknowledgement should be made
      * implicitly by CallbackPoller, which it does by calling Client.acknowledgeCallback()
      * after it has pushed the callback to the InvokerCallbackHandler.
      */
     public void testPollCallbackPostprocessAcknowledgement()
     {
        log.info("entering " + getName());
        try
        {
           TestCallbackHandler callbackHandler = new TestCallbackHandler();
           HashMap metadata = new HashMap();
           metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "100");
           client.addListener(callbackHandler, metadata);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           client.invoke(POSTPROCESS_TEST);
           Thread.sleep(1000);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertTrue(TestInvoocationHandler.acknowledgedPostprocess);
           assertTrue(TestCallbackHandler.receivedCallback);
           log.info(getName() + " PASSES");
        }
        catch (Throwable e)
        {
           log.info(getName() + " FAILS");
           e.printStackTrace();
           fail();
        }
     }
     
     
     /**
      * In this test the connection is configured for true push callbacks, and the
      * acknowledgement should be made by ServerInvokerCallbackHandler.handleCallback()
      * after it has pushed the Callback to the client.
      */
     public void testPushCallbackPostprocessAcknowledgement()
     {
        log.info("entering " + getName());
        try
        {
           TestCallbackHandler callbackHandler = new TestCallbackHandler();
           client.addListener(callbackHandler, null, null, true);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertFalse(TestInvoocationHandler.acknowledgedPostprocess);
           client.invoke(POSTPROCESS_TEST);
           assertFalse(TestInvoocationHandler.acknowledgedPreprocess);
           assertTrue(TestInvoocationHandler.acknowledgedPostprocess);
           assertTrue(TestCallbackHandler.receivedCallback);
           log.info(getName() + " PASSES");
        }
        catch (Throwable e)
        {
           log.info(getName() + " FAILS");
           e.printStackTrace();
           fail();
        }
     }
     
     static class TestInvoocationHandler implements ServerInvocationHandler, CallbackListener
     {
        static boolean acknowledgedPreprocess;
        static boolean acknowledgedPostprocess;
        
        InvokerCallbackHandler callbackHandler;
        
        public void setMBeanServer(MBeanServer server)
        {
        }
  
        public void setInvoker(ServerInvoker invoker)
        {  
        }
  
        public Object invoke(InvocationRequest invocation) throws Throwable
        {
           String command = (String) invocation.getParameter();
           if (PREPROCESS_TEST.equals(command))
           {
              Callback cb = new Callback("preprocess");
              HashMap returnPayload = new HashMap();
              returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_PREPROCESS_LISTENER, this);
              cb.setReturnPayload(returnPayload);
              callbackHandler.handleCallback(cb);
           }
           else if (POSTPROCESS_TEST.equals(command))
           {
              Callback cb = new Callback("postprocess");
              HashMap returnPayload = new HashMap();
              returnPayload.put(ServerInvokerCallbackHandler.CALLBACK_POSTPROCESS_LISTENER, this);
              cb.setReturnPayload(returnPayload);
              callbackHandler.handleCallback(cb);
           }
           
           return null;
        }
  
        public void addListener(InvokerCallbackHandler callbackHandler)
        {
           this.callbackHandler = callbackHandler;
        }
  
        public void removeListener(InvokerCallbackHandler callbackHandler)
        {
        }
  
        public void callbackSent(Callback callback)
        {
           if ("preprocess".equals(callback.getParameter()))
           {
              log.info("received preprocess acknowledgment");
              acknowledgedPreprocess = true;
           }
           else if ("postprocess".equals(callback.getParameter()))
           {
              log.info("received postprocess acknowledgment");
              acknowledgedPostprocess = true;
           }
           else
              log.error("unrecognized callback");
        }
     }
     
     static class TestCallbackHandler implements InvokerCallbackHandler
     {
        public static boolean receivedCallback;
        
        public void handleCallback(Callback callback) throws HandleCallbackException
        {
           log.info("entering handleCallback()");
           receivedCallback = true;
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list