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.
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.
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.
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.
22
package org.jboss.test.remoting.callback.asynch;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import junit.framework.TestCase;
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;
38
* AsynchCallbackTestClientRoot and AsynchCallbackTestServerRoot are the parent classes
39
* for a set of transport specific tests of the asynchronous callback facility.
41
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
42
* @version $Revision: 2920 $
44
* Copyright Nov 25, 2006
47
public abstract class AsynchCallbackTestClientRoot extends TestCase
49
private static Logger log = Logger.getLogger(AsynchCallbackTestClientRoot.class);
52
public void testSynchronousCallback() throws Throwable
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);
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);
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());
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");
83
log.info("disconnected");
87
public void testASynchronousCallbackClientSide() throws Throwable
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);
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);
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);
115
public void testASynchronousCallbackServerSide() throws Throwable
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);
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);
134
// Should have returned.
135
Boolean done = (Boolean) client.invoke(AsynchCallbackTestServerRoot.GET_STATUS);
136
assertTrue(done.booleanValue());
137
assertTrue(callbackHandler.receivedCallback);
140
// Callback should be handled.
141
assertTrue(callbackHandler.done);
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);
153
protected abstract String getTransport();
156
protected void addTransportSpecificConfig(Map config)
160
static class SampleCallbackHandler implements InvokerCallbackHandler
162
boolean receivedCallback;
165
public void handleCallback(Callback callback) throws HandleCallbackException
167
log.info("received callback");
168
receivedCallback = true;
173
catch (InterruptedException e)