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.synchronization;
24
import java.net.InetAddress;
25
import java.util.Date;
26
import java.util.HashMap;
28
import javax.management.MBeanServer;
30
import junit.framework.TestCase;
32
import org.apache.log4j.ConsoleAppender;
33
import org.apache.log4j.Level;
34
import org.apache.log4j.Logger;
35
import org.apache.log4j.PatternLayout;
36
import org.jboss.logging.XLevel;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.ConnectionListener;
39
import org.jboss.remoting.InvocationRequest;
40
import org.jboss.remoting.InvokerLocator;
41
import org.jboss.remoting.ServerInvocationHandler;
42
import org.jboss.remoting.ServerInvoker;
43
import org.jboss.remoting.callback.InvokerCallbackHandler;
44
import org.jboss.remoting.transport.Connector;
45
import org.jboss.remoting.transport.PortUtil;
50
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
* @version $Revision: 2913 $
53
* Copyright Jul 31, 2007
56
public class MultipleClientSynchronizationTestCase extends TestCase
58
protected static Logger log = Logger.getLogger(MultipleClientSynchronizationTestCase.class);
59
protected static boolean firstTime = true;
60
protected static BooleanHolder go1 = new BooleanHolder();
61
protected static BooleanHolder go2 = new BooleanHolder();
62
protected static BooleanHolder stop1 = new BooleanHolder();
63
protected static BooleanHolder stop2 = new BooleanHolder();
64
protected static InvokerLocator locator;
65
protected static int counter;
66
protected static Object lock = new Object();
68
public void setUp() throws Exception
73
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
74
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
75
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
76
PatternLayout layout = new PatternLayout(pattern);
77
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
78
Logger.getRootLogger().addAppender(consoleAppender);
85
public void testMultipleClientsStoppingAndStartingSimultaneously() throws Throwable
87
log.info("entering " + getName());
88
String host = InetAddress.getLocalHost().getHostAddress();
89
int port = PortUtil.findFreePort(host);
90
String locatorURI = "socket://" + host + ":" + port;
91
locator = new InvokerLocator(locatorURI);
92
Connector connector = new Connector(locator);
94
connector.setLeasePeriod(1000);
95
connector.addConnectionListener(new TestListener());
96
TestHandler handler = new TestHandler();
97
connector.addInvocationHandler("test", handler);
100
int INVOCATIONS = 1000;
101
ClientThread[] threads = new ClientThread[INVOCATIONS];
103
// Start clients 0..499.
104
for (int i = 0; i < INVOCATIONS / 2; i++)
106
threads[i] = new ClientThread(i, go1, stop1);
117
// Stop clients 0..499 and start clients 500..999.
118
for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
120
threads[i] = new ClientThread(i, go2, stop2);
137
// Stop clients 500..999.
144
// Wait for clients to disconnect.
145
for (int i = 0; i < 60; i++)
149
log.info("counter: " + counter);
150
if (counter == INVOCATIONS)
163
// Verify that all clients started and stopped successfully.
164
for (int i = 0; i < INVOCATIONS; i++)
166
assertTrue(new Date() + ": failure in thread: " + i, threads[i].ok);
170
log.info(getName() + " PASSES");
173
public void testMultipleClientsStartingStoppingStarting() throws Throwable
175
log.info("entering " + getName());
176
String host = InetAddress.getLocalHost().getHostAddress();
177
int port = PortUtil.findFreePort(host);
178
String locatorURI = "socket://" + host + ":" + port;
179
locator = new InvokerLocator(locatorURI);
180
Connector connector = new Connector(locator);
182
connector.setLeasePeriod(1000);
183
connector.addConnectionListener(new TestListener());
184
TestHandler handler = new TestHandler();
185
connector.addInvocationHandler("test", handler);
188
int INVOCATIONS = 1000;
189
ClientThread[] threads = new ClientThread[INVOCATIONS];
191
// Start clients 0..499.
192
for (int i = 0; i < INVOCATIONS / 2; i++)
194
threads[i] = new ClientThread(i, go1, stop1);
205
// Stop clients 0..499.
212
// Start clients 500..999.
213
for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
215
threads[i] = new ClientThread(i, go2, stop2);
227
// Stop clients 500..999.
234
// Wait for clients to disconnect.
235
for (int i = 0; i < 60; i++)
239
log.info("counter: " + counter);
240
if (counter == INVOCATIONS)
253
// Verify that all clients started and stopped successfully.
254
for (int i = 0; i < INVOCATIONS; i++)
256
assertTrue(new Date() + ": failure in thread: " + threads[i], threads[i].ok);
260
log.info(getName() + " PASSES");
264
public class ClientThread extends Thread
268
BooleanHolder startFlag;
269
BooleanHolder stopFlag;
272
public ClientThread(int id, BooleanHolder startFlag, BooleanHolder stopFlag) throws Exception
275
this.startFlag = startFlag;
276
this.stopFlag = stopFlag;
278
HashMap config = new HashMap();
279
config.put(InvokerLocator.FORCE_REMOTE, "true");
280
config.put(Client.ENABLE_LEASE, "true");
281
config.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
282
client = new Client(locator, config);
283
setName("ClientThread-" + id);
284
log.debug("client created (" + id + "): " + client.getSessionId());
291
synchronized (startFlag)
293
while (!startFlag.value)
295
try {startFlag.wait();} catch (InterruptedException e) {}
298
log.debug("client got start flag (" + id + "): " + client.getSessionId());
300
log.debug("client connected (" + id + "): " + client.getSessionId());
309
log.debug("client waiting for stop flag (" + id + "): " + client.getSessionId());
311
synchronized (stopFlag)
313
while (!stopFlag.value)
315
try {stopFlag.wait();} catch (InterruptedException e) {}
319
log.debug("client got stop flag (" + id + "): " + client.getSessionId());
321
// log.info("client disconnected (" + id + "): " + client.getSessionId());
323
log.debug("client ok (" + id + "):" + client.getSessionId());
338
static class TestHandler implements ServerInvocationHandler
340
public void setMBeanServer(MBeanServer server) {}
341
public void setInvoker(ServerInvoker invoker) {}
342
public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
343
public void addListener(InvokerCallbackHandler callbackHandler) {}
344
public void removeListener(InvokerCallbackHandler callbackHandler) {}
348
public static class TestListener implements ConnectionListener
350
public void handleConnectionException(Throwable throwable, Client client)
352
log.debug("got connection exception: " + throwable);
357
static class BooleanHolder
359
public boolean value;
b'\\ No newline at end of file'