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.connection;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
28
import java.util.TimerTask;
30
import javax.management.MBeanServer;
32
import junit.framework.TestCase;
34
import org.apache.log4j.ConsoleAppender;
35
import org.apache.log4j.Level;
36
import org.apache.log4j.Logger;
37
import org.apache.log4j.PatternLayout;
38
import org.jboss.logging.XLevel;
39
import org.jboss.remoting.Client;
40
import org.jboss.remoting.ConnectionListener;
41
import org.jboss.remoting.ConnectionValidator;
42
import org.jboss.remoting.InvocationRequest;
43
import org.jboss.remoting.InvokerLocator;
44
import org.jboss.remoting.LeasePinger;
45
import org.jboss.remoting.MicroRemoteClientInvoker;
46
import org.jboss.remoting.ServerInvocationHandler;
47
import org.jboss.remoting.ServerInvoker;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.transport.ClientInvoker;
50
import org.jboss.remoting.transport.Connector;
51
import org.jboss.remoting.transport.PortUtil;
55
* Unit tests for JBREM-891.
57
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
* @version $Revision: 1.1 $
60
* Copyright Jan 19, 2008
63
public class ConnectionValidatorTiedToLeaseTestCase extends TestCase
65
private static Logger log = Logger.getLogger(ConnectionValidatorTiedToLeaseTestCase.class);
67
private static boolean firstTime = true;
69
protected String host;
71
protected String locatorURI;
72
protected InvokerLocator serverLocator;
73
protected Connector connector;
74
protected TestInvocationHandler invocationHandler;
75
protected TestConnectionListener serverListener;
78
public void setUp() throws Exception
83
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
84
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
85
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
86
PatternLayout layout = new PatternLayout(pattern);
87
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
88
Logger.getRootLogger().addAppender(consoleAppender);
93
public void tearDown()
98
public void testTiedToLeaseDefaultConfigurationLocalInvoker() throws Throwable
100
log.info("entering " + getName());
106
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
107
HashMap clientConfig = new HashMap();
108
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
109
addExtraClientConfig(clientConfig);
110
Client client = new Client(clientLocator, clientConfig);
112
log.info("client is connected");
114
// Register client side listener.
115
Object lock = new Object();
116
TestConnectionListener clientListener = new TestConnectionListener(lock);
117
client.addConnectionListener(clientListener);
118
log.info("connection listener added on client side");
120
// Verify ConnectionValidator is running.
121
assertTrue(client.getPingPeriod() > -1);
124
assertEquals("abc", client.invoke("abc"));
125
log.info("connection is good");
127
// Verify LeasePinger is running.
128
assertTrue(client.getInvoker() instanceof MicroRemoteClientInvoker);
129
ClientInvoker clientInvoker = client.getInvoker();
130
Field field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
131
field.setAccessible(true);
132
LeasePinger pinger = (LeasePinger) field.get(clientInvoker);
133
assertNotNull(pinger);
134
field = LeasePinger.class.getDeclaredField("timerTask");
135
field.setAccessible(true);
136
TimerTask timerTask = (TimerTask) field.get(pinger);
137
assertNotNull(timerTask);
139
// Shut down lease on server side.
140
connector.removeConnectionListener(serverListener);
142
// Wait for client side listener to be notified.
148
// Verify ConnectionValidator has stopped.
150
assertTrue(clientListener.notified);
151
assertEquals(-1, client.getPingPeriod());
153
// Verfiy LeasePinger has stopped.
154
timerTask = (TimerTask) field.get(pinger);
155
assertNull(timerTask);
157
client.removeConnectionListener(clientListener);
160
log.info(getName() + " PASSES");
164
public void testTiedToLeaseDefaultConfigurationRemoteInvoker() throws Throwable
166
log.info("entering " + getName());
172
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
173
HashMap clientConfig = new HashMap();
174
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
175
addExtraClientConfig(clientConfig);
176
Client client = new Client(clientLocator, clientConfig);
178
log.info("client is connected");
180
// Register client side listener.
181
Object lock = new Object();
182
TestConnectionListener clientListener = new TestConnectionListener(lock);
183
Map metadata = new HashMap();
184
metadata.put(InvokerLocator.FORCE_REMOTE, "true");
185
client.addConnectionListener(clientListener, metadata);
186
log.info("connection listener added on client side");
188
// Verify ConnectionValidator is running.
189
assertTrue(client.getPingPeriod() > -1);
192
assertEquals("abc", client.invoke("abc"));
193
log.info("connection is good");
195
// Verify LeasePinger is running.
196
assertTrue(client.getInvoker() instanceof MicroRemoteClientInvoker);
197
ClientInvoker clientInvoker = client.getInvoker();
198
Field field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
199
field.setAccessible(true);
200
LeasePinger pinger = (LeasePinger) field.get(clientInvoker);
201
assertNotNull(pinger);
202
field = LeasePinger.class.getDeclaredField("timerTask");
203
field.setAccessible(true);
204
TimerTask timerTask = (TimerTask) field.get(pinger);
205
assertNotNull(timerTask);
207
// Shut down lease on server side.
208
connector.removeConnectionListener(serverListener);
210
// Wait for client side listener to be notified.
216
// Verify ConnectionValidator has stopped.
218
assertTrue(clientListener.notified);
219
assertEquals(-1, client.getPingPeriod());
221
// Verfiy LeasePinger has stopped.
222
timerTask = (TimerTask) field.get(pinger);
223
assertNull(timerTask);
225
client.removeConnectionListener(clientListener);
228
log.info(getName() + " PASSES");
232
public void testTiedToLeaseDontStopLease() throws Throwable
234
log.info("entering " + getName());
240
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
241
HashMap clientConfig = new HashMap();
242
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
243
addExtraClientConfig(clientConfig);
244
Client client = new Client(clientLocator, clientConfig);
246
log.info("client is connected");
248
// Register client side listener.
249
Object lock = new Object();
250
TestConnectionListener clientListener = new TestConnectionListener(lock);
251
Map metadata = new HashMap();
252
metadata.put(InvokerLocator.FORCE_REMOTE, "true");
253
metadata.put(ConnectionValidator.TIE_TO_LEASE, "true");
254
metadata.put(ConnectionValidator.STOP_LEASE_ON_FAILURE, "false");
255
client.addConnectionListener(clientListener, metadata);
256
log.info("connection listener added on client side");
258
// Verify ConnectionValidator is running.
259
assertTrue(client.getPingPeriod() > -1);
262
assertEquals("abc", client.invoke("abc"));
263
log.info("connection is good");
265
// Verify LeasePinger is running.
266
assertTrue(client.getInvoker() instanceof MicroRemoteClientInvoker);
267
ClientInvoker clientInvoker = client.getInvoker();
268
Field field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
269
field.setAccessible(true);
270
LeasePinger pinger = (LeasePinger) field.get(clientInvoker);
271
assertNotNull(pinger);
272
field = LeasePinger.class.getDeclaredField("timerTask");
273
field.setAccessible(true);
274
TimerTask timerTask = (TimerTask) field.get(pinger);
275
assertNotNull(timerTask);
277
// Shut down lease on server side.
278
connector.removeConnectionListener(serverListener);
280
// Wait for client side listener to be notified.
286
// Verify ConnectionValidator has stopped.
288
assertTrue(clientListener.notified);
289
assertEquals(-1, client.getPingPeriod());
291
// Verfiy LeasePinger has not stopped.
292
timerTask = (TimerTask) field.get(pinger);
293
assertNotNull(timerTask);
295
client.removeConnectionListener(clientListener);
298
log.info(getName() + " PASSES");
302
public void testNotTiedToLeaseDontStopLease() throws Throwable
304
log.info("entering " + getName());
310
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
311
HashMap clientConfig = new HashMap();
312
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
313
addExtraClientConfig(clientConfig);
314
Client client = new Client(clientLocator, clientConfig);
316
log.info("client is connected");
318
// Register client side listener.
319
Object lock = new Object();
320
TestConnectionListener clientListener = new TestConnectionListener(lock);
321
Map metadata = new HashMap();
322
metadata.put(InvokerLocator.FORCE_REMOTE, "true");
323
metadata.put(ConnectionValidator.TIE_TO_LEASE, "false");
324
metadata.put(ConnectionValidator.STOP_LEASE_ON_FAILURE, "false");
325
client.addConnectionListener(clientListener, metadata);
326
log.info("connection listener added on client side");
328
// Verify ConnectionValidator is running.
329
assertTrue(client.getPingPeriod() > -1);
332
assertEquals("abc", client.invoke("abc"));
333
log.info("connection is good");
335
// Verify LeasePinger is running.
336
assertTrue(client.getInvoker() instanceof MicroRemoteClientInvoker);
337
ClientInvoker clientInvoker = client.getInvoker();
338
Field field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
339
field.setAccessible(true);
340
LeasePinger pinger = (LeasePinger) field.get(clientInvoker);
341
assertNotNull(pinger);
342
field = LeasePinger.class.getDeclaredField("timerTask");
343
field.setAccessible(true);
344
TimerTask timerTask = (TimerTask) field.get(pinger);
345
assertNotNull(timerTask);
347
// Shut down lease on server side.
348
connector.removeConnectionListener(serverListener);
350
// Wait for client side listener to be notified.
356
// Verify ConnectionValidator has not stopped.
358
assertFalse(clientListener.notified);
359
assertTrue(client.getPingPeriod() > -1);
361
// Verfiy LeasePinger has not stopped.
362
timerTask = (TimerTask) field.get(pinger);
363
assertNotNull(timerTask);
365
client.removeConnectionListener(clientListener);
368
log.info(getName() + " PASSES");
372
public void testNotTiedToLeaseStopLease() throws Throwable
374
log.info("entering " + getName());
380
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
381
HashMap clientConfig = new HashMap();
382
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
383
addExtraClientConfig(clientConfig);
384
Client client = new Client(clientLocator, clientConfig);
386
log.info("client is connected");
388
// Register client side listener.
389
Object lock = new Object();
390
TestConnectionListener clientListener = new TestConnectionListener(lock);
391
Map metadata = new HashMap();
392
metadata.put(InvokerLocator.FORCE_REMOTE, "true");
393
metadata.put(ConnectionValidator.TIE_TO_LEASE, "false");
394
metadata.put(ConnectionValidator.STOP_LEASE_ON_FAILURE, "true");
395
client.addConnectionListener(clientListener, metadata);
396
log.info("connection listener added on client side");
398
// Verify ConnectionValidator is running.
399
assertTrue(client.getPingPeriod() > -1);
402
assertEquals("abc", client.invoke("abc"));
403
log.info("connection is good");
405
// Verify LeasePinger is running.
406
assertTrue(client.getInvoker() instanceof MicroRemoteClientInvoker);
407
ClientInvoker clientInvoker = client.getInvoker();
408
Field field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
409
field.setAccessible(true);
410
LeasePinger pinger = (LeasePinger) field.get(clientInvoker);
411
assertNotNull(pinger);
412
field = LeasePinger.class.getDeclaredField("timerTask");
413
field.setAccessible(true);
414
TimerTask timerTask = (TimerTask) field.get(pinger);
415
assertNotNull(timerTask);
417
// Prevent server from responding to ConnectionValidator pings.
418
ServerInvoker serverInvoker = connector.getServerInvoker();
419
field = ServerInvoker.class.getDeclaredField("started");
420
field.setAccessible(true);
421
field.set(serverInvoker, new Boolean(false));
422
assertFalse(field.getBoolean(serverInvoker));
423
log.info("Stopped server invoker");
425
// Wait for client side listener to be notified.
431
// Verify ConnectionValidator has stopped.
433
assertTrue(clientListener.notified);
434
assertEquals(-1, client.getPingPeriod());
436
// Verfiy LeasePinger has stopped.
437
field = LeasePinger.class.getDeclaredField("timerTask");
438
field.setAccessible(true);
439
timerTask = (TimerTask) field.get(pinger);
440
assertNull(timerTask);
442
client.removeConnectionListener(clientListener);
445
log.info(getName() + " PASSES");
449
public void testTiedToLeaseServerLeasingTurnedOff() throws Throwable
451
log.info("entering " + getName());
457
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
458
HashMap clientConfig = new HashMap();
459
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
460
addExtraClientConfig(clientConfig);
461
Client client = new Client(clientLocator, clientConfig);
463
log.info("client is connected");
465
// Register client side listener.
466
Object lock = new Object();
467
TestConnectionListener clientListener = new TestConnectionListener(lock);
468
Map metadata = new HashMap();
469
metadata.put(InvokerLocator.FORCE_REMOTE, "true");
470
client.addConnectionListener(clientListener, metadata);
471
log.info("connection listener added on client side");
473
// Verify ConnectionValidator is running.
474
assertTrue(client.getPingPeriod() > -1);
477
assertEquals("abc", client.invoke("abc"));
478
log.info("connection is good");
480
// Wait for client side listener to be notified.
486
// Verify ConnectionValidator has not stopped.
488
assertFalse(clientListener.notified);
489
assertTrue(client.getPingPeriod() > -1);
491
client.removeConnectionListener(clientListener);
494
log.info(getName() + " PASSES");
498
protected String getTransport()
504
protected void addExtraClientConfig(Map config) {}
505
protected void addExtraServerConfig(Map config) {}
508
protected void setupServer(boolean startLeasing) throws Exception
510
host = InetAddress.getLocalHost().getHostAddress();
511
port = PortUtil.findFreePort(host);
512
locatorURI = getTransport() + "://" + host + ":" + port;
513
locatorURI += "/?" + InvokerLocator.CLIENT_LEASE + "=true";
514
locatorURI += "&" + InvokerLocator.CLIENT_LEASE_PERIOD + "=4000";
515
serverLocator = new InvokerLocator(locatorURI);
516
log.info("Starting remoting server with locator uri of: " + locatorURI);
517
HashMap config = new HashMap();
518
config.put(InvokerLocator.FORCE_REMOTE, "true");
519
config.put(ServerInvoker.CLIENT_LEASE_PERIOD, "4000");
520
addExtraServerConfig(config);
521
connector = new Connector(serverLocator, config);
523
invocationHandler = new TestInvocationHandler();
524
connector.addInvocationHandler("test", invocationHandler);
527
serverListener = new TestConnectionListener();
528
connector.addConnectionListener(serverListener);
534
protected void shutdownServer() throws Exception
536
if (connector != null)
538
connector.removeConnectionListener(serverListener);
544
static class TestInvocationHandler implements ServerInvocationHandler
546
public void addListener(InvokerCallbackHandler callbackHandler) {}
547
public Object invoke(final InvocationRequest invocation) throws Throwable
549
return invocation.getParameter();
551
public void removeListener(InvokerCallbackHandler callbackHandler) {}
552
public void setMBeanServer(MBeanServer server) {}
553
public void setInvoker(ServerInvoker invoker) {}
557
static class TestConnectionListener implements ConnectionListener
559
public boolean notified;
562
TestConnectionListener()
566
TestConnectionListener(Object lock)
570
public void handleConnectionException(Throwable throwable, Client client)
577
// Give listener a chance to wait on lock.
580
catch (InterruptedException e)
582
log.info("Unexpected interrupt in TestConnectionListener");
b'\\ No newline at end of file'