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.transport.http.timeout;
24
import java.net.InetAddress;
25
import java.net.SocketTimeoutException;
26
import java.util.HashMap;
29
import javax.management.MBeanServer;
31
import junit.framework.TestCase;
33
import org.apache.log4j.ConsoleAppender;
34
import org.apache.log4j.Level;
35
import org.apache.log4j.Logger;
36
import org.apache.log4j.PatternLayout;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.ConnectionListener;
39
import org.jboss.remoting.InvocationRequest;
40
import org.jboss.remoting.InvokerLocator;
41
import org.jboss.remoting.ServerInvocationHandler;
42
import org.jboss.remoting.ServerInvoker;
43
import org.jboss.remoting.callback.Callback;
44
import org.jboss.remoting.callback.HandleCallbackException;
45
import org.jboss.remoting.callback.InvokerCallbackHandler;
46
import org.jboss.remoting.transport.Connector;
47
import org.jboss.remoting.transport.PortUtil;
52
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
53
* @version $Revision: 2495 $
55
* Copyright May 31, 2007
58
public class ReusedHttpURLConnectionsTestCase extends TestCase
60
public static int port;
62
private static Logger log = Logger.getLogger(ReusedHttpURLConnectionsTestCase.class);
64
private static final String CALLBACK_DELAY_KEY = "callbackDelay";
65
private static boolean firstTime = true;
67
// remoting server connector
68
private Connector connector;
69
private InvokerLocator serverLocator;
70
private TestInvocationHandler invocationHandler;
74
* Sets up target remoting server.
76
public void setUp() throws Exception
81
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
82
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
83
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
84
PatternLayout layout = new PatternLayout(pattern);
85
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
86
Logger.getRootLogger().addAppender(consoleAppender);
91
public void tearDown()
96
public void testLongThenShortTimeouts() throws Throwable
98
log.info("entering " + getName());
101
String host = InetAddress.getLocalHost().getHostAddress();
102
port = PortUtil.findFreePort(host);
103
String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=4000";
104
serverLocator = new InvokerLocator(locatorURI);
105
log.info("Starting remoting server with locator uri of: " + locatorURI);
106
HashMap config = new HashMap();
107
config.put(InvokerLocator.FORCE_REMOTE, "true");
108
addExtraServerConfig(config);
109
connector = new Connector(serverLocator, config);
111
invocationHandler = new TestInvocationHandler();
112
connector.addInvocationHandler("sample", invocationHandler);
116
HashMap clientConfig = new HashMap();
117
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
118
addExtraClientConfig(clientConfig);
119
Client client = new Client(serverLocator, clientConfig);
121
log.info("client is connected");
124
assertEquals("test", client.invoke("test"));
126
// Test connection timeout (4000).
127
HashMap metadata = new HashMap();
130
metadata.put(CALLBACK_DELAY_KEY, "6000");
131
client.invoke("test", metadata);
136
if (e.getCause() instanceof SocketTimeoutException)
138
log.info(getName() + ": got first expected timeout");
142
log.error(getName() + ": got unexpected exception", e);
147
// Test per invocation timeout (1000).
150
metadata.put("timeout", "1000");
151
metadata.put(CALLBACK_DELAY_KEY, "3000");
152
client.invoke("test", metadata);
157
if (e.getCause() instanceof SocketTimeoutException)
159
log.info(getName() + ": got second expected timeout");
163
log.error(getName() + ": got unexpected exception", e);
173
public void testShortThenLongTimeouts() throws Throwable
175
log.info("entering " + getName());
178
String host = InetAddress.getLocalHost().getHostAddress();
179
port = PortUtil.findFreePort(host);
180
String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=1000";
181
serverLocator = new InvokerLocator(locatorURI);
182
log.info("Starting remoting server with locator uri of: " + locatorURI);
183
HashMap config = new HashMap();
184
config.put(InvokerLocator.FORCE_REMOTE, "true");
185
addExtraServerConfig(config);
186
connector = new Connector(serverLocator, config);
188
invocationHandler = new TestInvocationHandler();
189
connector.addInvocationHandler("sample", invocationHandler);
193
HashMap clientConfig = new HashMap();
194
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
195
addExtraClientConfig(clientConfig);
196
Client client = new Client(serverLocator, clientConfig);
198
log.info("client is connected");
201
assertEquals("test", client.invoke("test"));
203
// Test connection timeout (1000).
204
HashMap metadata = new HashMap();
207
metadata.put(CALLBACK_DELAY_KEY, "3000");
208
client.invoke("test", metadata);
213
if (e.getCause() instanceof SocketTimeoutException)
215
log.info(getName() + ": got first expected timeout");
219
log.error(getName() + ": got unexpected exception", e);
224
// Try per invocation timeout (4000).
227
metadata.put("timeout", "4000");
228
metadata.put(CALLBACK_DELAY_KEY, "6000");
229
client.invoke("test", metadata);
234
if (e.getCause() instanceof SocketTimeoutException)
236
log.info(getName() + ": got second expected timeout");
240
log.error(getName() + ": got unexpected exception", e);
250
public void testShortThenZeroTimeouts() throws Throwable
252
log.info("entering " + getName());
255
String host = InetAddress.getLocalHost().getHostAddress();
256
port = PortUtil.findFreePort(host);
257
String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=1000";
258
serverLocator = new InvokerLocator(locatorURI);
259
log.info("Starting remoting server with locator uri of: " + locatorURI);
260
HashMap config = new HashMap();
261
config.put(InvokerLocator.FORCE_REMOTE, "true");
262
addExtraServerConfig(config);
263
connector = new Connector(serverLocator, config);
265
invocationHandler = new TestInvocationHandler();
266
connector.addInvocationHandler("sample", invocationHandler);
270
HashMap clientConfig = new HashMap();
271
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
272
addExtraClientConfig(clientConfig);
273
Client client = new Client(serverLocator, clientConfig);
275
log.info("client is connected");
278
assertEquals("test", client.invoke("test"));
280
// Test connection timeout (1000).
281
HashMap metadata = new HashMap();
284
metadata.put(CALLBACK_DELAY_KEY, "3000");
285
client.invoke("test", metadata);
290
if (e.getCause() instanceof SocketTimeoutException)
292
log.info(getName() + ": got first expected timeout");
296
log.error(getName() + ": got unexpected exception", e);
301
// Try per invocation timeout (0).
304
metadata.put("timeout", "0");
305
metadata.put(CALLBACK_DELAY_KEY, "5000");
306
assertEquals("test", client.invoke("test", metadata));
310
log.error(getName() + ": got unexpected exception", e);
319
public void testTimeoutsWithConnectionValidator() throws Throwable
321
log.info("entering " + getName());
324
String host = InetAddress.getLocalHost().getHostAddress();
325
port = PortUtil.findFreePort(host);
326
String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=4000";
327
serverLocator = new InvokerLocator(locatorURI);
328
log.info("Starting remoting server with locator uri of: " + locatorURI);
329
HashMap config = new HashMap();
330
config.put(InvokerLocator.FORCE_REMOTE, "true");
331
addExtraServerConfig(config);
332
connector = new Connector(serverLocator, config);
334
invocationHandler = new TestInvocationHandler();
335
connector.addInvocationHandler("sample", invocationHandler);
339
HashMap clientConfig = new HashMap();
340
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
341
addExtraClientConfig(clientConfig);
342
Client client = new Client(serverLocator, clientConfig);
344
ConnectionListener listener = new TestConnectionListener();
345
client.addConnectionListener(listener);
346
log.info("client is connected");
349
assertEquals("test", client.invoke("test"));
351
// Wait for a ping to occur.
354
// Try per invocation timeout (0).
355
HashMap metadata = new HashMap();
358
metadata.put("timeout", "0");
359
metadata.put(CALLBACK_DELAY_KEY, "6000");
360
assertEquals("test", client.invoke("test", metadata));
364
log.error(getName() + ": got unexpected exception", e);
373
protected String getTransport()
379
protected void addExtraClientConfig(Map config) {}
380
protected void addExtraServerConfig(Map config) {}
383
static class TestInvocationHandler implements ServerInvocationHandler
385
public void addListener(InvokerCallbackHandler callbackHandler) {}
387
public Object invoke(final InvocationRequest invocation) throws Throwable
389
Map requestMap = invocation.getRequestPayload();
391
String delayString = (String) requestMap.get(CALLBACK_DELAY_KEY);
392
if (delayString != null)
393
delay = Integer.parseInt(delayString);
394
log.info("starting delay: " + delay);
396
log.info("ending delay");
397
return invocation.getParameter();
400
public void removeListener(InvokerCallbackHandler callbackHandler) {}
401
public void setMBeanServer(MBeanServer server) {}
402
public void setInvoker(ServerInvoker invoker) {}
405
static class TestInvokerCallbackHandler implements InvokerCallbackHandler
407
public void handleCallback(Callback callback) throws HandleCallbackException {}
410
static class TestConnectionListener implements ConnectionListener
412
public void handleConnectionException(Throwable throwable, Client client)
b'\\ No newline at end of file'