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.lease;
24
import java.lang.reflect.Field;
25
import java.lang.reflect.Method;
26
import java.net.InetAddress;
27
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.remoting.Client;
39
import org.jboss.remoting.ConnectionListener;
40
import org.jboss.remoting.InvocationRequest;
41
import org.jboss.remoting.InvokerLocator;
42
import org.jboss.remoting.LeasePinger;
43
import org.jboss.remoting.ServerInvocationHandler;
44
import org.jboss.remoting.ServerInvoker;
45
import org.jboss.remoting.callback.InvokerCallbackHandler;
46
import org.jboss.remoting.transport.Connector;
47
import org.jboss.remoting.transport.PortUtil;
48
import org.jboss.remoting.transport.socket.SocketClientInvoker;
52
* Unit test for JBREM-794.
54
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
* @version $Revision: 5467 $
57
* Copyright Aug 22, 2007
60
public class UniquePingerTaskTestCase extends TestCase
62
private static Logger log = Logger.getLogger(UniquePingerTaskTestCase.class);
63
private static boolean firstTime = true;
65
protected String host;
67
protected String locatorURI;
68
protected InvokerLocator serverLocator;
69
protected Connector connector;
70
protected 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()
97
* Verifies that only one LeasePinger.LeaseTimerTask gets created.
99
public void testUniqueLeaseTimerTask() throws Throwable
101
log.info("entering " + getName());
104
SocketClientInvoker clientInvoker = new SocketClientInvoker(serverLocator);
105
clientInvoker.connect();
106
LeasePinger leasePinger = new LeasePinger(clientInvoker, "abc", 2000);
107
log.info("expect WARN \"failed to ping to server\"");
108
Method method = LeasePinger.class.getDeclaredMethod("setLeasePingerId", new Class[]{String.class});
109
method.setAccessible(true);
110
method.invoke(leasePinger, new Object[]{"dummyID"});
111
leasePinger.addClient("def", null, 1000);
113
// Verify LeasePingerTimerTask has not been created.
114
Field field = LeasePinger.class.getDeclaredField("timerTask");
115
field.setAccessible(true);
116
TimerTask timerTask = (TimerTask) field.get(leasePinger);
117
assertNull(timerTask);
118
leasePinger.startPing();
119
timerTask = (TimerTask) field.get(leasePinger);
120
assertNotNull(timerTask);
122
// Verify new LeasePingerTimerTask gets created when adding new Client
123
// with shorter lease period.
124
leasePinger.addClient("ghi", null, 500);
125
TimerTask newTimerTask = (TimerTask) field.get(leasePinger);
126
assertNotSame(newTimerTask, timerTask);
128
log.info(getName() + " PASSES");
131
protected void setupServer() throws Exception
133
host = InetAddress.getLocalHost().getHostAddress();
134
port = PortUtil.findFreePort(host);
135
locatorURI = "socket://" + host + ":" + port;
136
String metadata = System.getProperty("remoting.metadata");
137
if (metadata != null)
139
locatorURI += "/?" + metadata;
141
serverLocator = new InvokerLocator(locatorURI);
142
log.info("Starting remoting server with locator uri of: " + locatorURI);
143
HashMap config = new HashMap();
144
config.put(InvokerLocator.FORCE_REMOTE, "true");
145
config.put("leasePeriod", "1000");
146
connector = new Connector(serverLocator, config);
148
invocationHandler = new TestInvocationHandler();
149
connector.addInvocationHandler("test", invocationHandler);
150
connector.addConnectionListener(new TestConnectionListener());
155
protected void shutdownServer() throws Exception
157
if (connector != null)
162
static class TestInvocationHandler implements ServerInvocationHandler
164
public void addListener(InvokerCallbackHandler callbackHandler) {}
165
public Object invoke(final InvocationRequest invocation) throws Throwable
167
return invocation.getParameter();
169
public void removeListener(InvokerCallbackHandler callbackHandler) {}
170
public void setMBeanServer(MBeanServer server) {}
171
public void setInvoker(ServerInvoker invoker) {}
175
static class TestConnectionListener implements ConnectionListener
177
public void handleConnectionException(Throwable throwable, Client client)
b'\\ No newline at end of file'