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;
29
import java.util.LinkedList;
33
import javax.management.MBeanServer;
35
import junit.framework.TestCase;
37
import org.apache.log4j.ConsoleAppender;
38
import org.apache.log4j.Level;
39
import org.apache.log4j.Logger;
40
import org.apache.log4j.PatternLayout;
41
import org.jboss.remoting.Client;
42
import org.jboss.remoting.InvocationRequest;
43
import org.jboss.remoting.InvokerLocator;
44
import org.jboss.remoting.ServerInvocationHandler;
45
import org.jboss.remoting.ServerInvoker;
46
import org.jboss.remoting.callback.Callback;
47
import org.jboss.remoting.callback.HandleCallbackException;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
50
import org.jboss.remoting.transport.Connector;
51
import org.jboss.remoting.transport.PortUtil;
52
import org.jboss.remoting.transport.socket.ClientSocketWrapper;
53
import org.jboss.remoting.transport.socket.LRUPool;
54
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
55
import org.jboss.remoting.transport.socket.ServerSocketWrapper;
56
import org.jboss.remoting.transport.socket.ServerThread;
57
import org.jboss.remoting.transport.socket.SocketServerInvoker;
62
* Unit test for JBREM-765.
64
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
65
* @version $Revision: 2751 $
67
* Copyright Aug 5, 2007
70
public class BisocketCallbackTimeoutTestCase extends TestCase
72
public static int port;
73
private static Logger log = Logger.getLogger(BisocketCallbackTimeoutTestCase.class);
74
private static final String CALLBACK_TEST = "callbackTest";
76
private static boolean firstTime = true;
78
// remoting server connector
79
private Connector connector;
80
private InvokerLocator serverLocator;
81
private TestInvocationHandler invocationHandler;
85
* Sets up target remoting server.
87
public void setUp() throws Exception
92
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
93
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
94
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
95
PatternLayout layout = new PatternLayout(pattern);
96
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
97
Logger.getRootLogger().addAppender(consoleAppender);
102
public void tearDown()
108
* Verifies that "timeout" value is used in absence of "callbackTimeout" value.
110
public void testNoCallbackTimeout() throws Throwable
112
log.info("entering " + getName());
115
String host = InetAddress.getLocalHost().getHostAddress();
116
port = PortUtil.findFreePort(host);
117
String locatorURI = getTransport() + "://" + host + ":" + port;
118
serverLocator = new InvokerLocator(locatorURI);
119
log.info("Starting remoting server with locator uri of: " + locatorURI);
120
HashMap config = new HashMap();
121
config.put(InvokerLocator.FORCE_REMOTE, "true");
122
config.put(ServerInvoker.TIMEOUT, "3000");
123
addExtraServerConfig(config);
124
connector = new Connector(serverLocator, config);
126
invocationHandler = new TestInvocationHandler();
127
connector.addInvocationHandler("sample", invocationHandler);
131
HashMap clientConfig = new HashMap();
132
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
133
addExtraClientConfig(clientConfig);
134
final Client client = new Client(serverLocator, clientConfig);
136
assertEquals("abc", client.invoke("abc"));
137
log.info("client is connected");
139
// Add callback handler.
140
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
141
client.addListener(callbackHandler, new HashMap());
142
client.invoke(CALLBACK_TEST);
143
assertEquals(1, callbackHandler.counter);
144
log.info("callback handler is installed");
146
// Verify that "timeout" is used correctly.
147
assertTrue(connector.getServerInvoker() instanceof SocketServerInvoker);
148
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
149
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
150
field.setAccessible(true);
151
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
152
Set threads = clientpool.getContents();
153
assertEquals(1, threads.size());
154
ServerThread serverThread = (ServerThread) threads.iterator().next();
155
field = ServerThread.class.getDeclaredField("socketWrapper");
156
field.setAccessible(true);
157
assertTrue(field.get(serverThread) instanceof ServerSocketWrapper);
158
ServerSocketWrapper serverWrapper = (ServerSocketWrapper) field.get(serverThread);
159
assertEquals(3000, serverWrapper.getTimeout());
161
// Verify that "timeout" is used in the absence of "callbackTimeout".
162
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
163
field.setAccessible(true);
164
Map callbackHandlers = (Map) field.get(serverInvoker);
165
assertEquals(1, callbackHandlers.size());
166
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
167
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
168
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
169
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
170
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
171
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
172
field.setAccessible(true);
173
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
174
assertEquals(1, pool.size());
175
Object o = pool.iterator().next();
176
assertTrue(o instanceof ClientSocketWrapper);
177
ClientSocketWrapper clientWrapper = (ClientSocketWrapper) o;
178
assertEquals(3000, clientWrapper.getTimeout());
180
client.removeListener(callbackHandler);
187
* Verifies that "callbackTimeout" value overrides "timeout" value.
188
* Values are specified in configuration map.
190
public void testDistinctCallbackTimeoutConfigMap() throws Throwable
192
log.info("entering " + getName());
195
String host = InetAddress.getLocalHost().getHostAddress();
196
port = PortUtil.findFreePort(host);
197
String locatorURI = getTransport() + "://" + host + ":" + port;
198
serverLocator = new InvokerLocator(locatorURI);
199
log.info("Starting remoting server with locator uri of: " + locatorURI);
200
HashMap config = new HashMap();
201
config.put(InvokerLocator.FORCE_REMOTE, "true");
202
config.put(ServerInvoker.TIMEOUT, "3000");
203
config.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
204
addExtraServerConfig(config);
205
connector = new Connector(serverLocator, config);
207
invocationHandler = new TestInvocationHandler();
208
connector.addInvocationHandler("sample", invocationHandler);
212
HashMap clientConfig = new HashMap();
213
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
214
addExtraClientConfig(clientConfig);
215
final Client client = new Client(serverLocator, clientConfig);
217
assertEquals("abc", client.invoke("abc"));
218
log.info("client is connected");
220
// Add callback handler.
221
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
222
client.addListener(callbackHandler, new HashMap());
223
client.invoke(CALLBACK_TEST);
224
assertEquals(1, callbackHandler.counter);
225
log.info("callback handler is installed");
227
// Verify that "timeout" is used correctly.
228
assertTrue(connector.getServerInvoker() instanceof SocketServerInvoker);
229
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
230
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
231
field.setAccessible(true);
232
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
233
Set threads = clientpool.getContents();
234
assertEquals(1, threads.size());
235
ServerThread serverThread = (ServerThread) threads.iterator().next();
236
field = ServerThread.class.getDeclaredField("socketWrapper");
237
field.setAccessible(true);
238
assertTrue(field.get(serverThread) instanceof ServerSocketWrapper);
239
ServerSocketWrapper serverWrapper = (ServerSocketWrapper) field.get(serverThread);
240
assertEquals(3000, serverWrapper.getTimeout());
242
// Verify that "callbackTimeout" is used correctly.
243
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
244
field.setAccessible(true);
245
Map callbackHandlers = (Map) field.get(serverInvoker);
246
assertEquals(1, callbackHandlers.size());
247
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
248
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
249
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
250
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
251
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
252
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
253
field.setAccessible(true);
254
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
255
assertEquals(1, pool.size());
256
Object o = pool.iterator().next();
257
assertTrue(o instanceof ClientSocketWrapper);
258
ClientSocketWrapper clientWrapper = (ClientSocketWrapper) o;
259
assertEquals(7000, clientWrapper.getTimeout());
261
client.removeListener(callbackHandler);
268
* Verifies that "callbackTimeout" value overrides "timeout" value.
269
* Values are specified in InvokerLocator.
271
public void testDistinctCallbackTimeoutLocator() throws Throwable
273
log.info("entering " + getName());
276
String host = InetAddress.getLocalHost().getHostAddress();
277
port = PortUtil.findFreePort(host);
278
String locatorURI = getTransport() + "://" + host + ":" + port;
279
locatorURI += "/?timeout=3000&callbackTimeout=7000";
280
serverLocator = new InvokerLocator(locatorURI);
281
log.info("Starting remoting server with locator uri of: " + locatorURI);
282
HashMap config = new HashMap();
283
config.put(InvokerLocator.FORCE_REMOTE, "true");
284
addExtraServerConfig(config);
285
connector = new Connector(serverLocator, config);
287
invocationHandler = new TestInvocationHandler();
288
connector.addInvocationHandler("sample", invocationHandler);
292
HashMap clientConfig = new HashMap();
293
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
294
addExtraClientConfig(clientConfig);
295
final Client client = new Client(serverLocator, clientConfig);
297
assertEquals("abc", client.invoke("abc"));
298
log.info("client is connected");
300
// Add callback handler.
301
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
302
client.addListener(callbackHandler, new HashMap());
303
client.invoke(CALLBACK_TEST);
304
assertEquals(1, callbackHandler.counter);
305
log.info("callback handler is installed");
307
// Verify that "timeout" is used correctly.
308
assertTrue(connector.getServerInvoker() instanceof SocketServerInvoker);
309
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
310
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
311
field.setAccessible(true);
312
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
313
Set threads = clientpool.getContents();
314
assertEquals(1, threads.size());
315
ServerThread serverThread = (ServerThread) threads.iterator().next();
316
field = ServerThread.class.getDeclaredField("socketWrapper");
317
field.setAccessible(true);
318
assertTrue(field.get(serverThread) instanceof ServerSocketWrapper);
319
ServerSocketWrapper serverWrapper = (ServerSocketWrapper) field.get(serverThread);
320
assertEquals(3000, serverWrapper.getTimeout());
322
// Verify that "callbackTimeout" is used correctly.
323
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
324
field.setAccessible(true);
325
Map callbackHandlers = (Map) field.get(serverInvoker);
326
assertEquals(1, callbackHandlers.size());
327
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
328
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
329
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
330
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
331
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
332
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
333
field.setAccessible(true);
334
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
335
assertEquals(1, pool.size());
336
Object o = pool.iterator().next();
337
assertTrue(o instanceof ClientSocketWrapper);
338
ClientSocketWrapper clientWrapper = (ClientSocketWrapper) o;
339
assertEquals(7000, clientWrapper.getTimeout());
341
client.removeListener(callbackHandler);
348
* Verifies that "timeout" value is used in absense of "callbackTimeout".
349
* "timeout" value is specified in Client.addListener() metadata map.
351
public void testNoCallbackTimeoutMetadata() throws Throwable
353
log.info("entering " + getName());
356
String host = InetAddress.getLocalHost().getHostAddress();
357
port = PortUtil.findFreePort(host);
358
String locatorURI = getTransport() + "://" + host + ":" + port;
359
serverLocator = new InvokerLocator(locatorURI);
360
log.info("Starting remoting server with locator uri of: " + locatorURI);
361
HashMap config = new HashMap();
362
config.put(InvokerLocator.FORCE_REMOTE, "true");
363
addExtraServerConfig(config);
364
connector = new Connector(serverLocator, config);
366
invocationHandler = new TestInvocationHandler();
367
connector.addInvocationHandler("sample", invocationHandler);
371
HashMap clientConfig = new HashMap();
372
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
373
addExtraClientConfig(clientConfig);
374
final Client client = new Client(serverLocator, clientConfig);
376
assertEquals("abc", client.invoke("abc"));
377
log.info("client is connected");
379
// Add callback handler.
380
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
381
Map metadata = new HashMap();
382
metadata.put(ServerInvoker.TIMEOUT, "3000");
383
client.addListener(callbackHandler, metadata);
384
client.invoke(CALLBACK_TEST);
385
assertEquals(1, callbackHandler.counter);
386
log.info("callback handler is installed");
388
// Verify that "timeout" is used correctly.
389
ServerInvoker serverInvoker = connector.getServerInvoker();
390
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
391
field.setAccessible(true);
392
Map callbackHandlers = (Map) field.get(serverInvoker);
393
assertEquals(1, callbackHandlers.size());
394
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
395
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
396
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
397
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
398
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
399
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
400
field.setAccessible(true);
401
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
402
assertEquals(1, pool.size());
403
Object o = pool.iterator().next();
404
assertTrue(o instanceof ClientSocketWrapper);
405
ClientSocketWrapper clientWrapper = (ClientSocketWrapper) o;
406
assertEquals(3000, clientWrapper.getTimeout());
408
client.removeListener(callbackHandler);
415
* Verifies that "callbackTimeout" value overrides "timeout" value.
416
* "callbackTimeout" is specified in Client.addListener() metadata map.
418
public void testDistinctCallbackTimeoutMetadata() 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
serverLocator = new InvokerLocator(locatorURI);
427
log.info("Starting remoting server with locator uri of: " + locatorURI);
428
HashMap config = new HashMap();
429
config.put(InvokerLocator.FORCE_REMOTE, "true");
430
addExtraServerConfig(config);
431
connector = new Connector(serverLocator, config);
433
invocationHandler = new TestInvocationHandler();
434
connector.addInvocationHandler("sample", invocationHandler);
438
HashMap clientConfig = new HashMap();
439
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
440
addExtraClientConfig(clientConfig);
441
final Client client = new Client(serverLocator, clientConfig);
443
assertEquals("abc", client.invoke("abc"));
444
log.info("client is connected");
446
// Add callback handler.
447
TestInvokerCallbackHandler callbackHandler = new TestInvokerCallbackHandler();
448
Map metadata = new HashMap();
449
metadata.put(ServerInvokerCallbackHandler.CALLBACK_TIMEOUT, "7000");
450
client.addListener(callbackHandler, metadata);
451
client.invoke(CALLBACK_TEST);
452
assertEquals(1, callbackHandler.counter);
453
log.info("callback handler is installed");
455
// Verify that "callbackTimeout" is used correctly.
456
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
457
Field field = ServerInvoker.class.getDeclaredField("callbackHandlers");
458
field.setAccessible(true);
459
Map callbackHandlers = (Map) field.get(serverInvoker);
460
assertEquals(1, callbackHandlers.size());
461
ServerInvokerCallbackHandler serverInvokerCallbackHandler;
462
serverInvokerCallbackHandler = (ServerInvokerCallbackHandler) callbackHandlers.values().iterator().next();
463
Client callbackClient = serverInvokerCallbackHandler.getCallbackClient();
464
assertTrue(callbackClient.getInvoker() instanceof MicroSocketClientInvoker);
465
MicroSocketClientInvoker callbackClientInvoker = (MicroSocketClientInvoker) callbackClient.getInvoker();
466
field = MicroSocketClientInvoker.class.getDeclaredField("pool");
467
field.setAccessible(true);
468
LinkedList pool = (LinkedList) field.get(callbackClientInvoker);
469
assertEquals(1, pool.size());
470
Object o = pool.iterator().next();
471
assertTrue(o instanceof ClientSocketWrapper);
472
ClientSocketWrapper clientWrapper = (ClientSocketWrapper) o;
473
assertEquals(7000, clientWrapper.getTimeout());
475
client.removeListener(callbackHandler);
481
protected String getTransport()
487
protected void addExtraClientConfig(Map config) {}
488
protected void addExtraServerConfig(Map config) {}
491
static class TestInvocationHandler implements ServerInvocationHandler
493
public Set listeners = new HashSet();
495
public void addListener(InvokerCallbackHandler callbackHandler)
497
listeners.add(callbackHandler);
499
public Object invoke(final InvocationRequest invocation) throws Throwable
501
if (CALLBACK_TEST.equals(invocation.getParameter()))
503
Iterator it = listeners.iterator();
506
InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
507
handler.handleCallback(new Callback("test"));
510
return invocation.getParameter();
512
public void removeListener(InvokerCallbackHandler callbackHandler) {}
513
public void setMBeanServer(MBeanServer server) {}
514
public void setInvoker(ServerInvoker invoker) {}
518
static class TestInvokerCallbackHandler implements InvokerCallbackHandler
522
public void handleCallback(Callback callback) throws HandleCallbackException
b'\\ No newline at end of file'