2
* JBoss, Home of Professional Open Source.
3
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
4
* as indicated by the @author tags. See the copyright.txt file in the
5
* distribution for a 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.
23
package org.jboss.test.remoting.connection.identity;
25
import java.net.InetAddress;
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.logging.XLevel;
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.MicroRemoteClientInvoker;
44
import org.jboss.remoting.Remoting;
45
import org.jboss.remoting.ServerInvocationHandler;
46
import org.jboss.remoting.ServerInvoker;
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
import org.jboss.remoting.transport.ClientInvoker;
49
import org.jboss.remoting.transport.Connector;
50
import org.jboss.remoting.transport.PortUtil;
51
import org.jboss.util.id.GUID;
55
* Unit test for JBREM-1133.
57
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
58
* @version $Revision: 1.1 $
60
* Copyright May 08, 2009
63
public class LeaseIdentityTestCase extends TestCase
65
private static Logger log = Logger.getLogger(LeaseIdentityTestCase.class);
67
protected static long LEASE_PERIOD = 2000;
68
protected static String LEASE_PERIOD_STRING = "2000";
70
private static boolean firstTime = true;
72
protected String host;
74
protected String locatorURI;
75
protected InvokerLocator serverLocator;
76
protected Connector connector;
77
protected TestInvocationHandler invocationHandler;
78
protected TestConnectionListener listener;
81
public void setUp() throws Exception
86
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
87
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
88
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
89
PatternLayout layout = new PatternLayout(pattern);
90
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
91
Logger.getRootLogger().addAppender(consoleAppender);
96
public void tearDown()
101
public void testDefault() throws Throwable
103
log.info("entering " + getName());
106
setupServer(false, null);
109
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
110
HashMap clientConfig = new HashMap();
111
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
112
clientConfig.put(Client.ENABLE_LEASE, "true");
113
addExtraClientConfig(clientConfig);
114
Client client = new Client(clientLocator, clientConfig);
116
log.info("client is connected");
119
assertEquals("abc", client.invoke("abc"));
120
log.info("connection is good");
122
// Test lease behavior.
123
MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
124
clientInvoker.terminateLease(client.getSessionId(), 0);
125
TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
126
leasePinger.setLeasePingerId(new GUID().toString());
127
leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
128
leasePinger.startPing();
129
Thread.sleep(LEASE_PERIOD * 4);
130
assertFalse(listener.called);
132
leasePinger.stopPing();
135
log.info(getName() + " PASSES");
139
public void testClientConnectionIdentityFalse() throws Throwable
141
log.info("entering " + getName());
144
setupServer(true, "false");
147
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
148
HashMap clientConfig = new HashMap();
149
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
150
clientConfig.put(Client.ENABLE_LEASE, "true");
151
addExtraClientConfig(clientConfig);
152
Client client = new Client(clientLocator, clientConfig);
154
log.info("client is connected");
157
assertEquals("abc", client.invoke("abc"));
158
log.info("connection is good");
160
// Test lease behavior.
161
MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
162
clientInvoker.terminateLease(client.getSessionId(), 0);
163
TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
164
leasePinger.setLeasePingerId(new GUID().toString());
165
leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
166
leasePinger.startPing();
167
Thread.sleep(LEASE_PERIOD * 4);
168
assertFalse(listener.called);
170
leasePinger.stopPing();
173
log.info(getName() + " PASSES");
177
public void testClientConnectionIdentityTrue() throws Throwable
179
log.info("entering " + getName());
182
setupServer(true, "true");
185
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
186
HashMap clientConfig = new HashMap();
187
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
188
clientConfig.put(Client.ENABLE_LEASE, "true");
189
addExtraClientConfig(clientConfig);
190
Client client = new Client(clientLocator, clientConfig);
192
log.info("client is connected");
195
assertEquals("abc", client.invoke("abc"));
196
log.info("connection is good");
198
// Test lease behavior.
199
MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
200
clientInvoker.terminateLease(client.getSessionId(), 0);
201
TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
202
leasePinger.setLeasePingerId(new GUID().toString());
203
leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
204
leasePinger.startPing();
205
Thread.sleep(LEASE_PERIOD * 4);
206
assertTrue(listener.called);
207
assertNull(listener.throwable);
209
leasePinger.stopPing();
212
log.info(getName() + " PASSES");
216
protected String getTransport()
222
protected void addExtraClientConfig(Map config) {}
223
protected void addExtraServerConfig(Map config) {}
226
protected void setupServer(boolean setUseClientIdentity, String useClientIdentity) throws Exception
228
host = InetAddress.getLocalHost().getHostAddress();
229
port = PortUtil.findFreePort(host);
230
locatorURI = getTransport() + "://" + host + ":" + port;
231
String metadata = System.getProperty("remoting.metadata");
232
if (metadata != null)
234
locatorURI += "/?" + metadata;
236
serverLocator = new InvokerLocator(locatorURI);
237
log.info("Starting remoting server with locator uri of: " + locatorURI);
238
HashMap config = new HashMap();
239
config.put(InvokerLocator.FORCE_REMOTE, "true");
240
config.put("leasePeriod", LEASE_PERIOD_STRING);
241
if (setUseClientIdentity)
243
config.put(Remoting.USE_CLIENT_CONNECTION_IDENTITY, useClientIdentity);
245
addExtraServerConfig(config);
246
connector = new Connector(serverLocator, config);
248
invocationHandler = new TestInvocationHandler();
249
connector.addInvocationHandler("test", invocationHandler);
251
listener = new TestConnectionListener();
252
connector.addConnectionListener(listener);
256
protected void shutdownServer() throws Exception
258
if (connector != null)
263
static class TestInvocationHandler implements ServerInvocationHandler
265
public void addListener(InvokerCallbackHandler callbackHandler) {}
266
public Object invoke(final InvocationRequest invocation) throws Throwable
268
return invocation.getParameter();
270
public void removeListener(InvokerCallbackHandler callbackHandler) {}
271
public void setMBeanServer(MBeanServer server) {}
272
public void setInvoker(ServerInvoker invoker) {}
276
static class TestConnectionListener implements ConnectionListener
278
public boolean called;
279
public Throwable throwable;
281
public void handleConnectionException(Throwable throwable, Client client)
284
this.throwable = throwable;
285
log.info("called: throwable = " + throwable);
290
static class TestLeasePinger extends LeasePinger
292
public TestLeasePinger(ClientInvoker invoker, String invokerSessionID, long defaultLeasePeriod)
294
super(invoker, invokerSessionID, defaultLeasePeriod);
297
public void setLeasePingerId(String leasePingerId)
299
super.setLeasePingerId(leasePingerId);
b'\\ No newline at end of file'