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.soak;
24
import java.net.InetAddress;
25
import java.util.HashMap;
26
import java.util.HashSet;
27
import java.util.Iterator;
29
import java.util.Timer;
30
import java.util.TimerTask;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.logging.XLevel;
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.bisocket.Bisocket;
55
public abstract class ProducerConsumerSoakTestParent extends TestCase
57
private static Logger log = Logger.getLogger(ProducerConsumerSoakTestParent.class);
59
private static boolean firstTime = true;
61
protected String host;
63
protected String locatorURI;
64
protected InvokerLocator serverLocator;
65
protected Connector connector;
66
protected TestInvocationHandler invocationHandler;
69
public void setUp() throws Exception
74
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
75
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
76
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
77
PatternLayout layout = new PatternLayout(pattern);
78
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
79
Logger.getRootLogger().addAppender(consoleAppender);
84
public void tearDown()
89
public void testSoak() throws Throwable
91
log.info("entering " + getName());
93
log.info("free space: " + Runtime.getRuntime().freeMemory());
99
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
100
HashMap clientConfig = new HashMap();
101
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
102
addExtraClientConfig(clientConfig);
103
Client clients[] = new Client[SoakConstants.SENDERS];
104
TestCallbackHandler callbackHandler = new TestCallbackHandler();
105
Sender senders[] = new Sender[SoakConstants.SENDERS];
107
for (int i = 0; i < SoakConstants.SENDERS; i++)
109
clients[i] = new Client(clientLocator, clientConfig);
110
clients[i].connect();
111
log.info("client " + i + " is connected");
112
HashMap callbackMetadata = new HashMap();
114
for (int j = 0; j < SoakConstants.CALLBACK_LISTENERS; j++)
116
callbackMetadata.clear();
117
addExtraCallbackConfig(callbackMetadata);
118
clients[i].addListener(callbackHandler, callbackMetadata, null, true);
120
log.info("callback handlers installed for client " + i);
122
senders[i] = new Sender(i, clients[i]);
125
Timer timer = new Timer(true);
126
timer.schedule(new IntervalTimerTask(), 60000, 60000);
128
for (int i = 0; i < SoakConstants.SENDERS; i++)
132
log.info("senders atarted");
135
for (int i = 0; i < SoakConstants.SENDERS; i++)
138
invocations += senders[i].counter;
141
log.info("senders done");
142
log.info("invocations made: " + invocations);
143
log.info("invocations received: " + invocationHandler.counter);
144
log.info("callbacks received: " + callbackHandler.counter);
146
assertEquals(invocations, invocationHandler.counter);
147
assertEquals(invocations * SoakConstants.SENDERS * SoakConstants.CALLBACK_LISTENERS,
148
callbackHandler.counter);
150
for (int i = 0; i < SoakConstants.SENDERS; i++)
152
clients[i].removeListener(callbackHandler);
153
clients[i].disconnect();
158
log.info("free space: " + Runtime.getRuntime().freeMemory());
159
log.info(getName() + " PASSES");
163
protected abstract String getTransport();
166
protected void addExtraClientConfig(Map config) {}
167
protected void addExtraServerConfig(Map config) {}
168
protected void addExtraCallbackConfig(Map config) {}
171
protected void setupServer() throws Exception
173
host = InetAddress.getLocalHost().getHostAddress();
174
port = PortUtil.findFreePort(host);
175
locatorURI = getTransport() + "://" + host + ":" + port;
176
serverLocator = new InvokerLocator(locatorURI);
177
log.info("Starting remoting server with locator uri of: " + locatorURI);
178
HashMap config = new HashMap();
179
config.put(InvokerLocator.FORCE_REMOTE, "true");
180
addExtraServerConfig(config);
181
connector = new Connector(serverLocator, config);
183
invocationHandler = new TestInvocationHandler();
184
connector.addInvocationHandler("test", invocationHandler);
189
protected void shutdownServer() throws Exception
191
if (connector != null)
196
static class TestInvocationHandler implements ServerInvocationHandler
198
HashSet listeners = new HashSet();
200
Object lock = new Object();
202
public void addListener(InvokerCallbackHandler callbackHandler)
204
listeners.add(callbackHandler);
207
public Object invoke(final InvocationRequest invocation) throws Throwable
212
if ((counter + 1) % SoakConstants.INTERVAL == 0)
213
log.info("invocations received: " + (counter + 1));
216
Object o = invocation.getParameter();
217
Callback c = new Callback(o);
218
Iterator it = listeners.iterator();
221
ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) it.next();
222
handler.handleCallbackOneway(c);
223
handler.handleCallback(c);
229
public void removeListener(InvokerCallbackHandler callbackHandler) {}
230
public void setMBeanServer(MBeanServer server) {}
231
public void setInvoker(ServerInvoker invoker) {}
235
static class TestCallbackHandler implements InvokerCallbackHandler
238
Object lock = new Object();
240
public void handleCallback(Callback callback) throws HandleCallbackException
245
if ((counter + 1) % SoakConstants.INTERVAL == 0)
246
log.info("callbacks received: " + (counter + 1));
251
static class Sender extends Thread
256
Object lock = new Object();
259
public Sender(int id, Client client)
261
name = "sender:" + id;
262
this.client = client;
269
start = System.currentTimeMillis();
270
while (System.currentTimeMillis() - start <= SoakConstants.DURATION)
273
if ((counter + 1) % SoakConstants.INTERVAL == 0)
274
log.info(name + " invcations made: : " + (counter + 1));
275
client.invoke(name + ":" + counter);
286
static class IntervalTimerTask extends TimerTask
292
log.info("MINUTES ELAPSED: " + ++counter);
b'\\ No newline at end of file'