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.socket.socketpool;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.net.ServerSocket;
27
import java.net.Socket;
28
import java.util.ArrayList;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
34
import javax.management.MBeanServer;
36
import junit.framework.TestCase;
38
import org.apache.log4j.ConsoleAppender;
39
import org.apache.log4j.Level;
40
import org.apache.log4j.Logger;
41
import org.apache.log4j.PatternLayout;
42
import org.jboss.logging.XLevel;
43
import org.jboss.remoting.Client;
44
import org.jboss.remoting.InvocationRequest;
45
import org.jboss.remoting.InvokerLocator;
46
import org.jboss.remoting.ServerInvocationHandler;
47
import org.jboss.remoting.ServerInvoker;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.transport.Connector;
50
import org.jboss.remoting.transport.PortUtil;
51
import org.jboss.remoting.transport.socket.LRUPool;
52
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
53
import org.jboss.remoting.transport.socket.ServerThread;
54
import org.jboss.remoting.transport.socket.SocketServerInvoker;
58
* Unit test for JBREM-845.
60
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
61
* @version $Revision: 1.1 $
63
* Copyright Nov 30, 2007
66
public class SocketClientPoolWithSemaphoreTestCase extends TestCase
68
protected static String WAIT = "wait";
69
protected static String DURATION = "duration";
71
private static Logger log = Logger.getLogger(SocketClientPoolWithSemaphoreTestCase.class);
72
private static boolean firstTime = true;
74
protected String host;
76
protected String locatorURI;
77
protected InvokerLocator serverLocator;
78
protected Connector connector;
79
protected TestInvocationHandler invocationHandler;
82
public void setUp() throws Exception
87
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
88
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
89
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
90
PatternLayout layout = new PatternLayout(pattern);
91
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
92
Logger.getRootLogger().addAppender(consoleAppender);
97
public void tearDown()
102
public void testStressClientPoolTen() throws Throwable
104
log.info("entering " + getName());
110
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
111
HashMap clientConfig = new HashMap();
112
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
113
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "10");
114
addExtraClientConfig(clientConfig);
115
Client client = new Client(clientLocator, clientConfig);
117
log.info("client is connected");
119
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
120
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
122
for (int i = 0; i < 5000; i++)
124
assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
127
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
131
log.info(getName() + " PASSES");
135
public void testStressClientPoolOne() throws Throwable
137
log.info("entering " + getName());
143
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
144
HashMap clientConfig = new HashMap();
145
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
146
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "1");
147
addExtraClientConfig(clientConfig);
148
Client client = new Client(clientLocator, clientConfig);
150
log.info("client is connected");
152
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
153
assertEquals(1, clientInvoker.getNumberOfAvailableConnections());
155
for (int i = 0; i < 5000; i++)
157
assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
160
assertEquals(1, clientInvoker.getNumberOfAvailableConnections());
164
log.info(getName() + " PASSES");
168
public void testMaxPoolSize() throws Throwable
170
log.info("entering " + getName());
176
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
177
HashMap clientConfig = new HashMap();
178
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
179
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "10");
180
addExtraClientConfig(clientConfig);
181
final Client client = new Client(clientLocator, clientConfig);
183
log.info("client is connected");
185
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
186
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
188
final HashMap metadata = new HashMap();
189
metadata.put(DURATION, "20000");
191
for (int i = 0; i < 20; i++)
199
client.invoke(WAIT, metadata);
203
log.error("Error", e);
210
assertEquals(10, invocationHandler.counter);
211
assertEquals(0, clientInvoker.getNumberOfAvailableConnections());
214
assertEquals(20, invocationHandler.counter);
215
assertEquals(0, clientInvoker.getNumberOfAvailableConnections());
218
assertEquals(20, invocationHandler.counter);
219
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
223
log.info(getName() + " PASSES");
227
public void testRestartServer() throws Throwable
229
log.info("entering " + getName());
235
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
236
HashMap clientConfig = new HashMap();
237
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
238
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "10");
239
addExtraClientConfig(clientConfig);
240
Client client = new Client(clientLocator, clientConfig);
242
log.info("client is connected");
244
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
245
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
247
for (int i = 0; i < 50; i++)
249
assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
252
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
254
disableServer(connector);
258
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
260
for (int i = 0; i < 50; i++)
262
assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
265
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
269
log.info(getName() + " PASSES");
273
public void testSemaphoreReleaseAfterGetConnection() throws Throwable
275
log.info("entering " + getName());
281
String clientLocatorURI = locatorURI;
282
clientLocatorURI += "/?" + MicroSocketClientInvoker.CLIENT_SOCKET_CLASS_FLAG + "=bogus";
283
InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
284
log.info("clientLocator: " + clientLocator);
285
HashMap clientConfig = new HashMap();
286
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
287
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "10");
288
addExtraClientConfig(clientConfig);
289
Client client = new Client(clientLocator, clientConfig);
291
log.info("client is connected");
293
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
294
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
298
log.info(client.invoke("abc"));
299
fail("expected exception");
303
log.info("got expected exception");
306
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
310
log.info(getName() + " PASSES");
314
public void testSemaphoreReleaseAfterSocketException() throws Throwable
316
String exceptionClass = SocketWrapperWithSocketException.class.getName();
317
doSemaphoreReleaseAfterSocketException(exceptionClass);
322
public void testSemaphoreReleaseAfterIOException() throws Throwable
324
String exceptionClass = SocketWrapperWithIOException.class.getName();
325
doSemaphoreReleaseAfterSocketException(exceptionClass);
329
protected void doSemaphoreReleaseAfterSocketException(String exceptionClass) throws Throwable
331
log.info("entering " + getName());
337
String clientLocatorURI = locatorURI;
338
clientLocatorURI += "/?" + MicroSocketClientInvoker.CLIENT_SOCKET_CLASS_FLAG + "=" + exceptionClass;
339
InvokerLocator clientLocator = new InvokerLocator(clientLocatorURI);
340
log.info("clientLocator: " + clientLocator);
341
HashMap clientConfig = new HashMap();
342
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
343
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "10");
344
addExtraClientConfig(clientConfig);
345
Client client = new Client(clientLocator, clientConfig);
347
log.info("client is connected");
349
MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
350
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
354
log.info(client.invoke("abc"));
355
fail("expected exception");
359
log.info("got expected exception: " + e, e);
362
assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
366
log.info(getName() + " PASSES");
370
protected String getTransport()
376
protected void addExtraClientConfig(Map config) {}
377
protected void addExtraServerConfig(Map config) {}
380
protected void setupServer() throws Exception
386
protected void setupServer(int localPort) throws Exception
388
host = InetAddress.getLocalHost().getHostAddress();
391
port = PortUtil.findFreePort(host);
395
locatorURI = getTransport() + "://" + host + ":" + port;
396
serverLocator = new InvokerLocator(locatorURI);
397
log.info("Starting remoting server with locator uri of: " + locatorURI);
398
HashMap config = new HashMap();
399
config.put(InvokerLocator.FORCE_REMOTE, "true");
400
addExtraServerConfig(config);
401
connector = new Connector(serverLocator, config);
403
invocationHandler = new TestInvocationHandler();
404
connector.addInvocationHandler("test", invocationHandler);
406
for (int i = 0; i < 5; i++)
414
log.error("unable to start Connector");
421
protected void disableServer(Connector connector) throws Exception
423
log.info("disabling " + connector);
424
SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
425
Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
426
field.setAccessible(true);
427
List serverSockets = (List) field.get(serverInvoker);
428
ServerSocket ss = (ServerSocket) serverSockets.get(0);
431
field = SocketServerInvoker.class.getDeclaredField("clientpool");
432
field.setAccessible(true);
433
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
434
Iterator it = clientpool.getContents().iterator();
435
field = ServerThread.class.getDeclaredField("socket");
436
field.setAccessible(true);
440
Socket socket = (Socket) field.get(it.next());
444
field = SocketServerInvoker.class.getDeclaredField("threadpool");
445
field.setAccessible(true);
446
List threadpool = new ArrayList((List) field.get(serverInvoker));
447
it = threadpool.iterator();
448
field = ServerThread.class.getDeclaredField("socket");
449
field.setAccessible(true);
453
Socket socket = (Socket) field.get(it.next());
457
log.info("disabled " + connector);
461
protected void shutdownServer() throws Exception
463
if (connector != null)
468
static class TestInvocationHandler implements ServerInvocationHandler
471
private Object lock = new Object();
473
public void addListener(InvokerCallbackHandler callbackHandler) {}
474
public Object invoke(final InvocationRequest invocation) throws Throwable
481
String command = (String) invocation.getParameter();
483
if (WAIT.equals(command))
485
Map metadata = invocation.getRequestPayload();
486
int duration = Integer.parseInt((String) metadata.get(DURATION));
487
Thread.sleep(duration);
492
public void removeListener(InvokerCallbackHandler callbackHandler) {}
493
public void setMBeanServer(MBeanServer server) {}
494
public void setInvoker(ServerInvoker invoker) {}
b'\\ No newline at end of file'