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.transport.bisocket.timertask;
24
import java.lang.reflect.Field;
25
import java.lang.reflect.Method;
26
import java.net.InetAddress;
27
import java.security.AccessController;
28
import java.security.PrivilegedActionException;
29
import java.security.PrivilegedExceptionAction;
30
import java.util.HashMap;
33
import java.util.Timer;
34
import java.util.TimerTask;
36
import javax.management.MBeanServer;
38
import junit.framework.TestCase;
40
import org.apache.log4j.ConsoleAppender;
41
import org.apache.log4j.Level;
42
import org.apache.log4j.Logger;
43
import org.apache.log4j.PatternLayout;
44
import org.jboss.logging.XLevel;
45
import org.jboss.remoting.Client;
46
import org.jboss.remoting.InvocationRequest;
47
import org.jboss.remoting.InvokerLocator;
48
import org.jboss.remoting.ServerInvocationHandler;
49
import org.jboss.remoting.ServerInvoker;
50
import org.jboss.remoting.callback.Callback;
51
import org.jboss.remoting.callback.HandleCallbackException;
52
import org.jboss.remoting.callback.InvokerCallbackHandler;
53
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
54
import org.jboss.remoting.transport.Connector;
55
import org.jboss.remoting.transport.PortUtil;
56
import org.jboss.remoting.transport.bisocket.Bisocket;
57
import org.jboss.remoting.transport.bisocket.BisocketClientInvoker;
58
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
59
import org.jboss.remoting.util.SecurityUtility;
63
* Unit test for JBREM-1005.
65
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
66
* @version $Revision: 1.1 $
68
* Copyright Jun 26, 2008
71
public class TimerTaskTestCase extends TestCase
73
private static Logger log = Logger.getLogger(TimerTaskTestCase.class);
75
private static boolean firstTime = true;
76
private static boolean purgeMethodAvailable;
78
protected String host;
80
protected String locatorURI;
81
protected InvokerLocator serverLocator;
82
protected Connector connector;
83
protected TestInvocationHandler invocationHandler;
86
public void setUp() throws Exception
91
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
92
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
93
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
94
PatternLayout layout = new PatternLayout(pattern);
95
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
96
Logger.getRootLogger().addAppender(consoleAppender);
100
getDeclaredMethod(Timer.class, "purge", new Class[]{});
101
purgeMethodAvailable = true;
105
log.info("Timer.purge() is not available: must be running with jdk 1.4");
111
public void tearDown()
116
public void testZeroPingFrequency() throws Throwable
118
log.info("entering " + getName());
120
if (!purgeMethodAvailable)
122
log.info(getName() + " PASSES (trivially)");
130
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
131
HashMap clientConfig = new HashMap();
132
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
133
addExtraClientConfig(clientConfig);
134
Client client = new Client(clientLocator, clientConfig);
138
assertEquals("abc", client.invoke("abc"));
139
log.info("connection is good");
141
// Set up callback handler.
142
TestCallbackHandler callbackHandler = new TestCallbackHandler();
143
HashMap metadata = new HashMap();
144
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
145
client.addListener(callbackHandler, metadata);
146
assertEquals(1, callbackHandler.counter);
148
// Verify ControlMonitorTimerTask is not created.
149
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
150
assertEquals(1, callbackConnectors.size());
151
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
152
Field field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
153
field.setAccessible(true);
154
BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
155
TimerTask timerTask = (TimerTask) field.get(serverInvoker);
156
assertNull(timerTask);
157
log.info("ControlMonitorTimerTask was not created");
159
// Verify PingTimerTask is not created.
160
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
161
field.setAccessible(true);
162
serverInvoker = (BisocketServerInvoker) connector.getServerInvoker();
163
Map callbackHandlers = (Map) field.get(serverInvoker);
164
assertEquals(1, callbackHandlers.size());
165
Object o = callbackHandlers.values().iterator().next();
166
Client callbackClient = ((ServerInvokerCallbackHandler) o).getCallbackClient();
167
BisocketClientInvoker clientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
168
field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
169
field.setAccessible(true);
170
timerTask = (TimerTask) field.get(clientInvoker);
171
assertNull(timerTask);
172
log.info("PingTimerTask was not created");
176
log.info(getName() + " PASSES");
180
public void testNonZeroPingFrequency() throws Throwable
182
log.info("entering " + getName());
184
if (!purgeMethodAvailable)
186
log.info(getName() + " PASSES (trivially)");
194
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
195
HashMap clientConfig = new HashMap();
196
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
197
addExtraClientConfig(clientConfig);
198
Client client = new Client(clientLocator, clientConfig);
202
assertEquals("abc", client.invoke("abc"));
203
log.info("connection is good");
205
// Set up callback handler.
206
TestCallbackHandler callbackHandler = new TestCallbackHandler();
207
HashMap metadata = new HashMap();
208
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
209
client.addListener(callbackHandler, metadata);
210
assertEquals(1, callbackHandler.counter);
212
// Verify ControlMonitorTimerTask is created.
213
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
214
assertEquals(1, callbackConnectors.size());
215
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
216
Field field = BisocketServerInvoker.class.getDeclaredField("controlMonitorTimerTask");
217
field.setAccessible(true);
218
BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
219
TimerTask timerTask = (TimerTask) field.get(serverInvoker);
220
assertNotNull(timerTask);
221
log.info("ControlMonitorTimerTask was created");
223
// Verify PingTimerTask is created.
224
field = ServerInvoker.class.getDeclaredField("callbackHandlers");
225
field.setAccessible(true);
226
BisocketServerInvoker callbackServerInvoker = (BisocketServerInvoker) connector.getServerInvoker();
227
Map callbackHandlers = (Map) field.get(callbackServerInvoker);
228
assertEquals(1, callbackHandlers.size());
229
Object o = callbackHandlers.values().iterator().next();
230
Client callbackClient = ((ServerInvokerCallbackHandler) o).getCallbackClient();
231
BisocketClientInvoker clientInvoker = (BisocketClientInvoker) callbackClient.getInvoker();
232
field = BisocketClientInvoker.class.getDeclaredField("pingTimerTask");
233
field.setAccessible(true);
234
timerTask = (TimerTask) field.get(clientInvoker);
235
assertNotNull(timerTask);
236
log.info("PingTimerTask was created");
238
// Get count of ControlMonitorTimerTasks.
239
field = BisocketServerInvoker.class.getDeclaredField("timer");
240
field.setAccessible(true);
241
Timer timer = (Timer) field.get(null);
242
field = Timer.class.getDeclaredField("queue");
243
field.setAccessible(true);
244
Object taskQueue = field.get(timer);
245
Field serverTimerfield = taskQueue.getClass().getDeclaredField("size");
246
serverTimerfield.setAccessible(true);
247
int serverSize = ((Integer) serverTimerfield.get(taskQueue)).intValue();
248
log.info("ControlMonitorTimerTasks: " + serverSize);
250
// Get count of PingTimerTasks.
251
field = BisocketClientInvoker.class.getDeclaredField("timer");
252
field.setAccessible(true);
253
timer = (Timer) field.get(null);
254
field = Timer.class.getDeclaredField("queue");
255
field.setAccessible(true);
256
taskQueue = field.get(timer);
257
Field clientTimerfield = taskQueue.getClass().getDeclaredField("size");
258
clientTimerfield.setAccessible(true);
259
int clientSize = ((Integer) clientTimerfield.get(taskQueue)).intValue();
260
log.info("PingTimerTasks: " + clientSize);
262
// Shut down callback connection and verify TimerTasks are removed from
264
client.removeListener(callbackHandler);
265
serverSize = ((Integer) serverTimerfield.get(taskQueue)).intValue();
266
log.info("ControlMonitorTimerTasks: " + serverSize);
267
assertEquals(0, serverSize);
268
clientSize = ((Integer) clientTimerfield.get(taskQueue)).intValue();
269
log.info("PingTimerTasks: " + clientSize);
270
assertEquals(0, clientSize);
274
log.info(getName() + " PASSES");
278
protected String getTransport()
284
protected void addExtraClientConfig(Map config) {}
285
protected void addExtraServerConfig(Map config) {}
288
protected void setupServer(String pingFrequency) throws Exception
290
host = InetAddress.getLocalHost().getHostAddress();
291
port = PortUtil.findFreePort(host);
292
locatorURI = getTransport() + "://" + host + ":" + port;
293
locatorURI += "/?" + Bisocket.PING_FREQUENCY + "=" + pingFrequency;
294
serverLocator = new InvokerLocator(locatorURI);
295
log.info("Starting remoting server with locator uri of: " + locatorURI);
296
HashMap config = new HashMap();
297
config.put(InvokerLocator.FORCE_REMOTE, "true");
298
addExtraServerConfig(config);
299
connector = new Connector(serverLocator, config);
301
invocationHandler = new TestInvocationHandler();
302
connector.addInvocationHandler("test", invocationHandler);
307
protected void shutdownServer() throws Exception
309
if (connector != null)
314
static class TestInvocationHandler implements ServerInvocationHandler
316
public void addListener(InvokerCallbackHandler callbackHandler)
320
callbackHandler.handleCallback(new Callback("callback"));
322
catch (HandleCallbackException e)
324
log.error("Unable to send callback", e);
327
public Object invoke(final InvocationRequest invocation) throws Throwable
329
return invocation.getParameter();
331
public void removeListener(InvokerCallbackHandler callbackHandler) {}
332
public void setMBeanServer(MBeanServer server) {}
333
public void setInvoker(ServerInvoker invoker) {}
337
static class TestCallbackHandler implements InvokerCallbackHandler
340
public void handleCallback(Callback callback) throws HandleCallbackException
343
log.info("received callback");
347
static private Method getDeclaredMethod(final Class c, final String name, final Class[] parameterTypes)
348
throws NoSuchMethodException
350
if (SecurityUtility.skipAccessControl())
352
Method m = c.getDeclaredMethod(name, parameterTypes);
353
m.setAccessible(true);
359
return (Method) AccessController.doPrivileged( new PrivilegedExceptionAction()
361
public Object run() throws NoSuchMethodException
363
Method m = c.getDeclaredMethod(name, parameterTypes);
364
m.setAccessible(true);
369
catch (PrivilegedActionException e)
371
throw (NoSuchMethodException) e.getCause();
b'\\ No newline at end of file'