~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/callback/asynch/AsynchCallbackTestClientRoot.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
package org.jboss.test.remoting.callback.asynch;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import junit.framework.TestCase;
 
29
 
 
30
import org.jboss.logging.Logger;
 
31
import org.jboss.remoting.Client;
 
32
import org.jboss.remoting.InvokerLocator;
 
33
import org.jboss.remoting.callback.Callback;
 
34
import org.jboss.remoting.callback.HandleCallbackException;
 
35
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
36
 
 
37
/** 
 
38
 * AsynchCallbackTestClientRoot and AsynchCallbackTestServerRoot are the parent classes
 
39
 * for a set of transport specific tests of the asynchronous callback facility.
 
40
 * 
 
41
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
42
 * @version $Revision: 2920 $
 
43
 * <p>
 
44
 * Copyright Nov 25, 2006
 
45
 * </p>
 
46
 */
 
47
public abstract class AsynchCallbackTestClientRoot extends TestCase
 
48
{
 
49
   private static Logger log = Logger.getLogger(AsynchCallbackTestClientRoot.class);
 
50
 
 
51
   
 
52
   public void testSynchronousCallback() throws Throwable
 
53
   {
 
54
      String transport = getTransport();
 
55
      String host = InetAddress.getLocalHost().getHostName();
 
56
      int port = AsynchCallbackTestServerRoot.port;
 
57
      String locatorURI = transport + "://" + host + ":" + port; 
 
58
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
59
      log.info("Connecting to: " + serverLocator);
 
60
      HashMap config = new HashMap();
 
61
      addTransportSpecificConfig(config);
 
62
      Client client = new Client(serverLocator, config);
 
63
      client.connect();
 
64
      log.info("client is connected");
 
65
      SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
 
66
      client.addListener(callbackHandler, null, null, true);
 
67
      log.info("client added callback handler");
 
68
      client.invokeOneway(AsynchCallbackTestServerRoot.SYNCHRONOUS_TEST);
 
69
      Thread.sleep(1000);
 
70
      // Should still be waiting on client.
 
71
      Boolean done = (Boolean) client.invoke(AsynchCallbackTestServerRoot.GET_STATUS);
 
72
      log.info("done 1: " + done);
 
73
      assertFalse(done.booleanValue());
 
74
      Thread.sleep(8000);
 
75
      done = (Boolean) client.invoke(AsynchCallbackTestServerRoot.GET_STATUS);
 
76
      log.info("done 2: " + done);
 
77
      assertTrue(done.booleanValue());
 
78
      assertTrue(callbackHandler.receivedCallback);
 
79
      client.invoke(AsynchCallbackTestServerRoot.RESET);
 
80
      client.removeListener(callbackHandler);
 
81
      log.info("disconnecting");
 
82
      client.disconnect();
 
83
      log.info("disconnected");
 
84
   }
 
85
   
 
86
   
 
87
   public void testASynchronousCallbackClientSide() throws Throwable
 
88
   {
 
89
      String transport = getTransport();
 
90
      String host = InetAddress.getLocalHost().getHostName();
 
91
      int port = AsynchCallbackTestServerRoot.port;
 
92
      String locatorURI = transport + "://" + host + ":" + port; 
 
93
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
94
      log.info("Connecting to: " + serverLocator);
 
95
      HashMap config = new HashMap();
 
96
      addTransportSpecificConfig(config);
 
97
      Client client = new Client(serverLocator, config);
 
98
      client.connect();
 
99
      log.info("client is connected");
 
100
      SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
 
101
      client.addListener(callbackHandler, null, null, true);
 
102
      log.info("client added callback handler");
 
103
      client.invokeOneway(AsynchCallbackTestServerRoot.ASYNCHRONOUS_CLIENT_SIDE_TEST);
 
104
      Thread.sleep(4000);
 
105
      // Should have returned.
 
106
      Boolean done = (Boolean) client.invoke(AsynchCallbackTestServerRoot.GET_STATUS);
 
107
      assertTrue(done.booleanValue());
 
108
      assertTrue(callbackHandler.receivedCallback);
 
109
      client.invoke(AsynchCallbackTestServerRoot.RESET);
 
110
      client.removeListener(callbackHandler);
 
111
      client.disconnect();
 
112
   }
 
113
   
 
114
   
 
115
   public void testASynchronousCallbackServerSide() throws Throwable
 
116
   {
 
117
      String transport = getTransport();
 
118
      String host = InetAddress.getLocalHost().getHostName();
 
119
      int port = AsynchCallbackTestServerRoot.port;
 
120
      String locatorURI = transport + "://" + host + ":" + port; 
 
121
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
122
      log.info("Connecting to: " + serverLocator);
 
123
      HashMap config = new HashMap();
 
124
      addTransportSpecificConfig(config);
 
125
      Client client = new Client(serverLocator, config);
 
126
      client.connect();
 
127
      log.info("client is connected");
 
128
      SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
 
129
      client.addListener(callbackHandler, null, null, true);
 
130
      log.info("client added callback handler");
 
131
      client.invokeOneway(AsynchCallbackTestServerRoot.ASYNCHRONOUS_SERVER_SIDE_TEST);
 
132
      Thread.sleep(4000);
 
133
      
 
134
      // Should have returned.
 
135
      Boolean done = (Boolean) client.invoke(AsynchCallbackTestServerRoot.GET_STATUS);
 
136
      assertTrue(done.booleanValue());
 
137
      assertTrue(callbackHandler.receivedCallback);
 
138
      Thread.sleep(5000);
 
139
      
 
140
      // Callback should be handled.
 
141
      assertTrue(callbackHandler.done);
 
142
      
 
143
      String threadCount = (String) client.invoke(AsynchCallbackTestServerRoot.GET_THREAD_COUNT);
 
144
      assertEquals(AsynchCallbackTestServerRoot.THREAD_COUNT, threadCount);
 
145
      String queueSize = (String) client.invoke(AsynchCallbackTestServerRoot.GET_QUEUE_SIZE);
 
146
      assertEquals(AsynchCallbackTestServerRoot.QUEUE_SIZE, queueSize);
 
147
      client.invoke(AsynchCallbackTestServerRoot.RESET);
 
148
      client.removeListener(callbackHandler);
 
149
      client.disconnect();
 
150
   }
 
151
   
 
152
   
 
153
   protected abstract String getTransport();
 
154
   
 
155
   
 
156
   protected void addTransportSpecificConfig(Map config)
 
157
   {
 
158
   }
 
159
   
 
160
   static class SampleCallbackHandler implements InvokerCallbackHandler
 
161
   {
 
162
      boolean receivedCallback;
 
163
      boolean done;
 
164
      
 
165
      public void handleCallback(Callback callback) throws HandleCallbackException
 
166
      {
 
167
         log.info("received callback");
 
168
         receivedCallback = true;
 
169
         try
 
170
         {
 
171
            Thread.sleep(5000);
 
172
         }
 
173
         catch (InterruptedException e)
 
174
         {
 
175
         }
 
176
         done = true;
 
177
      }
 
178
   }
 
179
}