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.shutdown;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import junit.framework.TestCase;
30
import org.apache.log4j.Logger;
31
import org.jboss.remoting.Client;
32
import org.jboss.remoting.ConnectionListener;
33
import org.jboss.remoting.InvokerLocator;
34
import org.jboss.remoting.ServerInvoker;
35
import org.jboss.remoting.callback.Callback;
36
import org.jboss.remoting.callback.CallbackPoller;
37
import org.jboss.remoting.callback.HandleCallbackException;
38
import org.jboss.remoting.callback.InvokerCallbackHandler;
41
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
42
* @version $Revision: 3011 $
44
* Copyright Jan 19, 2007
47
public abstract class AbstractClient extends TestCase
49
private static Logger log = Logger.getLogger(AbstractClient.class);
50
private String transport;
51
private Map extraConfig;
54
public AbstractClient(String transport, Map config)
56
this.transport = transport;
57
this.extraConfig = new HashMap(config);
58
log.info("client transport: " + transport);
59
log.info("log4j.configuration: " + System.getProperty("log4j.configuration"));
60
Runtime.getRuntime().traceMethodCalls(true);
65
* This test is used to verify that a JVM with a client connected to a server will shut
66
* down. To exercise as many threads as possible, it enables leasing, registers a
67
* connection listener, and registers a callback handler for blocking polled callbacks
68
* and another callback handler for nonblocking polled callbacks.
70
* At the end of the method, it creates a Thread which runs longer that this test is
71
* supposed to last. According to the value returned by the overridden abstract
72
* method daemon(), it the Thread will be a daemon or non-daemon thread.
74
public void testShutdown() throws Throwable
78
String host = InetAddress.getLocalHost().getHostAddress();
79
String portString = System.getProperty("port");
80
int port = Integer.parseInt(portString);
81
String locatorURI = transport + "://" + host + ":" + port;
82
InvokerLocator locator = new InvokerLocator(locatorURI);
83
HashMap clientConfig = new HashMap(extraConfig);
84
clientConfig.put(Client.ENABLE_LEASE, "true");
85
clientConfig.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
86
Client client = new Client(locator, clientConfig);
88
log.info("client connected");
90
ConnectionListener listener = new ShutdownTestServer.TestListener();
91
client.addConnectionListener(listener, 1000);
92
Integer i = (Integer) client.invoke(new Integer(17));
93
if (18 != i.intValue())
94
throw new Exception("invocation failed");
95
log.info("invocation successful");
96
TestCallbackHandler callbackHandler1 = new TestCallbackHandler();
97
HashMap metadata = new HashMap();
98
metadata.put(ServerInvoker.BLOCKING_TIMEOUT, "2000");
99
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
100
addCallbackArgs(metadata);
101
log.info("metadata: " + metadata);
102
client.addListener(callbackHandler1, metadata, null, false);
103
log.info("added blocking listener 1");
104
TestCallbackHandler callbackHandler2 = new TestCallbackHandler();
106
metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "500");
107
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.NONBLOCKING);
108
addCallbackArgs(metadata);
109
log.info("metadata: " + metadata);
110
client.addListener(callbackHandler2, metadata, null, false);
111
log.info("added nonblocking listener 2");
113
if (!callbackHandler1.receivedCallback)
115
log.info("callback 1 failed");
116
throw new Exception("callback 1 failed");
118
if (!callbackHandler2.receivedCallback)
120
log.info("callback 2 failed");
121
throw new Exception("callback 2 failed");
123
log.info("callback successful");
124
client.removeConnectionListener(listener);
125
log.info("calling removeListener(): 1");
126
client.removeListener(callbackHandler1);
127
log.info("calling removeListener(): 2");
128
client.removeListener(callbackHandler2);
129
log.info("calling disconnect()");
131
Thread t = new Thread()
139
catch (InterruptedException e)
141
log.info("interrupted");
145
t.setDaemon(daemon());
147
log.info("client disconnected");
151
log.info("exception in client: " + e);
157
abstract protected boolean daemon();
160
protected void addCallbackArgs(Map map)
166
protected static void getConfig(Map config, String configs)
169
int ampersand = configs.indexOf('&');
170
while (ampersand > 0)
172
String s = configs.substring(start, ampersand);
173
int equals = s.indexOf('=');
174
String param = s.substring(0, equals);
175
String value = s.substring(equals + 1);
176
config.put(param, value);
177
start = ampersand + 1;
178
ampersand = configs.indexOf('&', start);
183
public class TestCallbackHandler implements InvokerCallbackHandler
185
public boolean receivedCallback;
187
public void handleCallback(Callback callback) throws HandleCallbackException
189
receivedCallback = true;
190
log.info("received callback: " + callback);