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.timeout;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
27
import java.util.HashSet;
28
import java.util.Iterator;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.remoting.Client;
41
import org.jboss.remoting.InvocationRequest;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.ServerInvocationHandler;
44
import org.jboss.remoting.ServerInvoker;
45
import org.jboss.remoting.callback.Callback;
46
import org.jboss.remoting.callback.HandleCallbackException;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
49
import org.jboss.remoting.transport.Connector;
50
import org.jboss.remoting.transport.PortUtil;
55
* Unit test for JBREM-799.
57
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
* @version $Revision: 1.1.2.2 $
60
* Copyright Jan 25, 2008
63
abstract public class CallbackTimeoutTestParent extends TestCase
65
public static int port;
66
private static Logger log = Logger.getLogger(CallbackTimeoutTestParent.class);
67
private static final String CALLBACK_TEST = "callbackTest";
69
private static boolean firstTime = true;
71
// remoting server connector
72
private Connector connector;
73
private InvokerLocator serverLocator;
74
private TestInvocationHandler invocationHandler;
78
* Sets up target remoting server.
80
public void setUp() throws Exception
85
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
86
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
87
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
88
PatternLayout layout = new PatternLayout(pattern);
89
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
90
Logger.getRootLogger().addAppender(consoleAppender);
95
public void tearDown()
101
* Verifies that "timeout" value is used in absence of "callbackTimeout" value.
103
public void testNoCallbackTimeout() throws Throwable
105
log.info("entering " + getName());
108
String host = InetAddress.getLocalHost().getHostAddress();
109
port = PortUtil.findFreePort(host);
110
String locatorURI = getTransport() + "://" + host + ":" + port;
111
serverLocator = new InvokerLocator(locatorURI);
112
log.info("Starting remoting server with locator uri of: " + locatorURI);
113
HashMap config = new HashMap();
114
config.put(InvokerLocator.FORCE_REMOTE, "true");
115
config.put(ServerInvoker.TIMEOUT, "3000");
116
addExtraServerConfig(config);
117
connector = new Connector(serverLocator, config);
119
invocationHandler = new TestInvocationHandler();
120
connector.addInvocationHandler("sample", invocationHandler);
124
HashMap clientConfig = new HashMap();
125
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
126
addExtraClientConfig(clientConfig);
127
final Client client = new Client(serverLocator, clientConfig);
129
assertEquals("abc", client.invoke("abc"));
130
log.info("client is connected");
132
// Add callback handler.
133
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
134
client.addListener(testCallbackHandler, new HashMap(), null, true);
135
client.invoke(CALLBACK_TEST);
136
assertEquals(1, testCallbackHandler.counter);
137
log.info("callback handler is installed");
139
// Verify that "timeout" is used correctly.
140
ServerInvoker serverInvoker = connector.getServerInvoker();
141
Map configuration = serverInvoker.getConfiguration();
142
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
144
// Verify that "timeout" is used in the absence of "callbackTimeout".
145
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
146
field.setAccessible(true);
147
Map callbackHandlers = (Map) field.get(serverInvoker);
148
assertEquals(1, callbackHandlers.size());
149
Object o = callbackHandlers.values().iterator().next();
150
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
151
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
152
field.setAccessible(true);
153
Client callBackClient = (Client) field.get(callbackHandler);
154
configuration = callBackClient.getConfiguration();
155
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
157
client.removeListener(testCallbackHandler);
164
* Verifies that "callbackTimeout" value overrides "timeout" value.
165
* Values are specified in configuration map.
167
public void testDistinctCallbackTimeoutConfigMap() throws Throwable
169
log.info("entering " + getName());
172
String host = InetAddress.getLocalHost().getHostAddress();
173
port = PortUtil.findFreePort(host);
174
String locatorURI = getTransport() + "://" + host + ":" + port;
175
serverLocator = new InvokerLocator(locatorURI);
176
log.info("Starting remoting server with locator uri of: " + locatorURI);
177
HashMap config = new HashMap();
178
config.put(InvokerLocator.FORCE_REMOTE, "true");
179
config.put(ServerInvoker.TIMEOUT, "3000");
180
config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
181
addExtraServerConfig(config);
182
connector = new Connector(serverLocator, config);
184
invocationHandler = new TestInvocationHandler();
185
connector.addInvocationHandler("sample", invocationHandler);
189
HashMap clientConfig = new HashMap();
190
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
191
addExtraClientConfig(clientConfig);
192
final Client client = new Client(serverLocator, clientConfig);
194
assertEquals("abc", client.invoke("abc"));
195
log.info("client is connected");
197
// Add callback handler.
198
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
199
client.addListener(testCallbackHandler, new HashMap(), null, true);
200
client.invoke(CALLBACK_TEST);
201
assertEquals(1, testCallbackHandler.counter);
202
log.info("callback handler is installed");
204
// Verify that "timeout" is used correctly.
205
ServerInvoker serverInvoker = connector.getServerInvoker();
206
Map configuration = serverInvoker.getConfiguration();
207
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
209
// Verify that "callbackTimeout" is used correctly.
210
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
211
field.setAccessible(true);
212
Map callbackHandlers = (Map) field.get(serverInvoker);
213
assertEquals(1, callbackHandlers.size());
214
Object o = callbackHandlers.values().iterator().next();
215
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
216
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
217
field.setAccessible(true);
218
Client callBackClient = (Client) field.get(callbackHandler);
219
configuration = callBackClient.getConfiguration();
220
assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
222
client.removeListener(testCallbackHandler);
229
* Verifies that "callbackTimeout" value overrides "timeout" value.
230
* Values are specified in InvokerLocator.
232
public void testDistinctCallbackTimeoutLocator() throws Throwable
234
log.info("entering " + getName());
237
String host = InetAddress.getLocalHost().getHostAddress();
238
port = PortUtil.findFreePort(host);
239
String locatorURI = getTransport() + "://" + host + ":" + port;
240
locatorURI += "/?timeout=3000&callbackTimeout=7000";
241
serverLocator = new InvokerLocator(locatorURI);
242
log.info("Starting remoting server with locator uri of: " + locatorURI);
243
HashMap config = new HashMap();
244
config.put(InvokerLocator.FORCE_REMOTE, "true");
245
addExtraServerConfig(config);
246
connector = new Connector(serverLocator, config);
248
invocationHandler = new TestInvocationHandler();
249
connector.addInvocationHandler("sample", invocationHandler);
253
HashMap clientConfig = new HashMap();
254
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
255
addExtraClientConfig(clientConfig);
256
final Client client = new Client(serverLocator, clientConfig);
258
assertEquals("abc", client.invoke("abc"));
259
log.info("client is connected");
261
// Add callback handler.
262
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
263
client.addListener(testCallbackHandler, new HashMap(), null, true);
264
client.invoke(CALLBACK_TEST);
265
assertEquals(1, testCallbackHandler.counter);
266
log.info("callback handler is installed");
268
// Verify that "timeout" is used correctly.
269
ServerInvoker serverInvoker = connector.getServerInvoker();
270
Map configuration = serverInvoker.getConfiguration();
271
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
273
// Verify that "callbackTimeout" is used correctly.
274
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
275
field.setAccessible(true);
276
Map callbackHandlers = (Map) field.get(serverInvoker);
277
assertEquals(1, callbackHandlers.size());
278
Object o = callbackHandlers.values().iterator().next();
279
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
280
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
281
field.setAccessible(true);
282
Client callBackClient = (Client) field.get(callbackHandler);
283
configuration = callBackClient.getConfiguration();
284
assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
286
client.removeListener(testCallbackHandler);
293
* Verifies that "timeout" value is used in absense of "callbackTimeout".
294
* "timeout" value is specified in Client.addListener() metadata map.
296
public void testNoCallbackTimeoutMetadata() throws Throwable
298
log.info("entering " + getName());
301
String host = InetAddress.getLocalHost().getHostAddress();
302
port = PortUtil.findFreePort(host);
303
String locatorURI = getTransport() + "://" + host + ":" + port;
304
serverLocator = new InvokerLocator(locatorURI);
305
log.info("Starting remoting server with locator uri of: " + locatorURI);
306
HashMap config = new HashMap();
307
config.put(InvokerLocator.FORCE_REMOTE, "true");
308
addExtraServerConfig(config);
309
connector = new Connector(serverLocator, config);
311
invocationHandler = new TestInvocationHandler();
312
connector.addInvocationHandler("sample", invocationHandler);
316
HashMap clientConfig = new HashMap();
317
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
318
addExtraClientConfig(clientConfig);
319
final Client client = new Client(serverLocator, clientConfig);
321
assertEquals("abc", client.invoke("abc"));
322
log.info("client is connected");
324
// Add callback handler.
325
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
326
Map metadata = new HashMap();
327
metadata.put(ServerInvoker.TIMEOUT, "3000");
328
client.addListener(testCallbackHandler, metadata, null, true);
329
client.invoke(CALLBACK_TEST);
330
assertEquals(1, testCallbackHandler.counter);
331
log.info("callback handler is installed");
333
// Verify that "timeout" is used in the absence of "callbackTimeout".
334
ServerInvoker serverInvoker = connector.getServerInvoker();
335
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
336
field.setAccessible(true);
337
Map callbackHandlers = (Map) field.get(serverInvoker);
338
assertEquals(1, callbackHandlers.size());
339
Object o = callbackHandlers.values().iterator().next();
340
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
341
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
342
field.setAccessible(true);
343
Client callBackClient = (Client) field.get(callbackHandler);
344
Map configuration = callBackClient.getConfiguration();
345
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
347
client.removeListener(testCallbackHandler);
354
* Verifies that "callbackTimeout" value overrides "timeout" value.
355
* "callbackTimeout" is specified in Client.addListener() metadata map.
357
public void testDistinctCallbackTimeoutMetadata() throws Throwable
359
log.info("entering " + getName());
362
String host = InetAddress.getLocalHost().getHostAddress();
363
port = PortUtil.findFreePort(host);
364
String locatorURI = getTransport() + "://" + host + ":" + port;
365
serverLocator = new InvokerLocator(locatorURI);
366
log.info("Starting remoting server with locator uri of: " + locatorURI);
367
HashMap config = new HashMap();
368
config.put(InvokerLocator.FORCE_REMOTE, "true");
369
addExtraServerConfig(config);
370
connector = new Connector(serverLocator, config);
372
invocationHandler = new TestInvocationHandler();
373
connector.addInvocationHandler("sample", invocationHandler);
377
HashMap clientConfig = new HashMap();
378
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
379
addExtraClientConfig(clientConfig);
380
final Client client = new Client(serverLocator, clientConfig);
382
assertEquals("abc", client.invoke("abc"));
383
log.info("client is connected");
385
// Add callback handler.
386
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
387
Map metadata = new HashMap();
388
metadata.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
389
client.addListener(testCallbackHandler, metadata, null, true);
390
client.invoke(CALLBACK_TEST);
391
assertEquals(1, testCallbackHandler.counter);
392
log.info("callback handler is installed");
394
// Verify that "callbackTimeout" is used correctly.
395
ServerInvoker serverInvoker = connector.getServerInvoker();
396
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
397
field.setAccessible(true);
398
Map callbackHandlers = (Map) field.get(serverInvoker);
399
assertEquals(1, callbackHandlers.size());
400
Object o = callbackHandlers.values().iterator().next();
401
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
402
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
403
field.setAccessible(true);
404
Client callBackClient = (Client) field.get(callbackHandler);
405
Map configuration = callBackClient.getConfiguration();
406
assertEquals("7000", configuration.get(ServerInvoker.TIMEOUT));
408
client.removeListener(testCallbackHandler);
415
* Verifies that "callbackTimeout" specified in Client.addListener() metadata
416
* map overrides values in server InvokerLocator and server configuration map.
418
public void testMetadataOverridesLocatorAndConfigMap() throws Throwable
420
log.info("entering " + getName());
423
String host = InetAddress.getLocalHost().getHostAddress();
424
port = PortUtil.findFreePort(host);
425
String locatorURI = getTransport() + "://" + host + ":" + port;
426
locatorURI += "/?callbackTimeout=3000";
427
serverLocator = new InvokerLocator(locatorURI);
428
log.info("Starting remoting server with locator uri of: " + locatorURI);
429
HashMap config = new HashMap();
430
config.put(InvokerLocator.FORCE_REMOTE, "true");
431
config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
432
addExtraServerConfig(config);
433
connector = new Connector(serverLocator, config);
435
invocationHandler = new TestInvocationHandler();
436
connector.addInvocationHandler("sample", invocationHandler);
440
HashMap clientConfig = new HashMap();
441
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
442
addExtraClientConfig(clientConfig);
443
final Client client = new Client(serverLocator, clientConfig);
445
assertEquals("abc", client.invoke("abc"));
446
log.info("client is connected");
448
// Add callback handler.
449
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
450
Map metadata = new HashMap();
451
metadata.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "11000");
452
client.addListener(testCallbackHandler, metadata, null, true);
453
client.invoke(CALLBACK_TEST);
454
assertEquals(1, testCallbackHandler.counter);
455
log.info("callback handler is installed");
457
// Verify that "callbackTimeout" from Client.addListener() metadata
459
ServerInvoker serverInvoker = connector.getServerInvoker();
460
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
461
field.setAccessible(true);
462
Map callbackHandlers = (Map) field.get(serverInvoker);
463
assertEquals(1, callbackHandlers.size());
464
Object o = callbackHandlers.values().iterator().next();
465
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
466
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
467
field.setAccessible(true);
468
Client callBackClient = (Client) field.get(callbackHandler);
469
Map configuration = callBackClient.getConfiguration();
470
assertEquals("11000", configuration.get(ServerInvoker.TIMEOUT));
472
client.removeListener(testCallbackHandler);
479
* Verifies that "callbackTimeout" specified in server InvokerLocator
480
* overrides value specified in server configuration map.
482
public void testLocatorOverridesConfigMap() throws Throwable
484
log.info("entering " + getName());
487
String host = InetAddress.getLocalHost().getHostAddress();
488
port = PortUtil.findFreePort(host);
489
String locatorURI = getTransport() + "://" + host + ":" + port;
490
locatorURI += "/?callbackTimeout=3000";
491
serverLocator = new InvokerLocator(locatorURI);
492
log.info("Starting remoting server with locator uri of: " + locatorURI);
493
HashMap config = new HashMap();
494
config.put(InvokerLocator.FORCE_REMOTE, "true");
495
config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
496
addExtraServerConfig(config);
497
connector = new Connector(serverLocator, config);
499
invocationHandler = new TestInvocationHandler();
500
connector.addInvocationHandler("sample", invocationHandler);
504
HashMap clientConfig = new HashMap();
505
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
506
addExtraClientConfig(clientConfig);
507
final Client client = new Client(serverLocator, clientConfig);
509
assertEquals("abc", client.invoke("abc"));
510
log.info("client is connected");
512
// Add callback handler.
513
TestInvokerCallbackHandler testCallbackHandler = new TestInvokerCallbackHandler();
514
client.addListener(testCallbackHandler, new HashMap(), null, true);
515
client.invoke(CALLBACK_TEST);
516
assertEquals(1, testCallbackHandler.counter);
517
log.info("callback handler is installed");
519
// Verify that "callbackTimeout" from InvokerLocator overrides value
520
// in configuration map.
521
ServerInvoker serverInvoker = connector.getServerInvoker();
522
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
523
field.setAccessible(true);
524
Map callbackHandlers = (Map) field.get(serverInvoker);
525
assertEquals(1, callbackHandlers.size());
526
Object o = callbackHandlers.values().iterator().next();
527
ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) o;
528
field = ServerInvokerCallbackHandler.class.getDeclaredField("callBackClient");
529
field.setAccessible(true);
530
Client callBackClient = (Client) field.get(callbackHandler);
531
Map configuration = callBackClient.getConfiguration();
532
assertEquals("3000", configuration.get(ServerInvoker.TIMEOUT));
534
client.removeListener(testCallbackHandler);
540
abstract protected String getTransport();
543
protected void addExtraClientConfig(Map config) {}
544
protected void addExtraServerConfig(Map config) {}
547
static class TestInvocationHandler implements ServerInvocationHandler
549
public Set listeners = new HashSet();
551
public void addListener(InvokerCallbackHandler callbackHandler)
553
listeners.add(callbackHandler);
555
public Object invoke(final InvocationRequest invocation) throws Throwable
557
if (CALLBACK_TEST.equals(invocation.getParameter()))
559
Iterator it = listeners.iterator();
562
InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
563
handler.handleCallback(new Callback("test"));
566
return invocation.getParameter();
568
public void removeListener(InvokerCallbackHandler callbackHandler) {}
569
public void setMBeanServer(MBeanServer server) {}
570
public void setInvoker(ServerInvoker invoker) {}
574
static class TestInvokerCallbackHandler implements InvokerCallbackHandler
578
public void handleCallback(Callback callback) throws HandleCallbackException
b'\\ No newline at end of file'