~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/socketpool/SocketClientPoolWithSemaphoreTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
package org.jboss.test.remoting.transport.socket.socketpool;
 
23
 
 
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;
 
32
import java.util.Map;
 
33
 
 
34
import javax.management.MBeanServer;
 
35
 
 
36
import junit.framework.TestCase;
 
37
 
 
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;
 
55
 
 
56
 
 
57
/**
 
58
 * Unit test for JBREM-845.
 
59
 * 
 
60
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
61
 * @version $Revision: 1.1 $
 
62
 * <p>
 
63
 * Copyright Nov 30, 2007
 
64
 * </p>
 
65
 */
 
66
public class SocketClientPoolWithSemaphoreTestCase extends TestCase
 
67
{
 
68
   protected static String WAIT = "wait";
 
69
   protected static String DURATION = "duration";
 
70
   
 
71
   private static Logger log = Logger.getLogger(SocketClientPoolWithSemaphoreTestCase.class);
 
72
   private static boolean firstTime = true;
 
73
   
 
74
   protected String host;
 
75
   protected int port;
 
76
   protected String locatorURI;
 
77
   protected InvokerLocator serverLocator;
 
78
   protected Connector connector;
 
79
   protected TestInvocationHandler invocationHandler;
 
80
 
 
81
   
 
82
   public void setUp() throws Exception
 
83
   {
 
84
      if (firstTime)
 
85
      {
 
86
         firstTime = false;
 
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);  
 
93
      }
 
94
   }
 
95
 
 
96
   
 
97
   public void tearDown()
 
98
   {
 
99
   }
 
100
   
 
101
   
 
102
   public void testStressClientPoolTen() throws Throwable
 
103
   {
 
104
      log.info("entering " + getName());
 
105
      
 
106
      // Start server.
 
107
      setupServer();
 
108
      
 
109
      // Create client.
 
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);
 
116
      client.connect();
 
117
      log.info("client is connected");
 
118
      
 
119
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
120
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
121
      
 
122
      for (int i = 0; i < 5000; i++)
 
123
      {
 
124
         assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
 
125
      }
 
126
      
 
127
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
128
      
 
129
      client.disconnect();
 
130
      shutdownServer();
 
131
      log.info(getName() + " PASSES");
 
132
   }
 
133
   
 
134
   
 
135
   public void testStressClientPoolOne() throws Throwable
 
136
   {
 
137
      log.info("entering " + getName());
 
138
      
 
139
      // Start server.
 
140
      setupServer();
 
141
      
 
142
      // Create client.
 
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);
 
149
      client.connect();
 
150
      log.info("client is connected");
 
151
      
 
152
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
153
      assertEquals(1, clientInvoker.getNumberOfAvailableConnections());
 
154
      
 
155
      for (int i = 0; i < 5000; i++)
 
156
      {
 
157
         assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
 
158
      }
 
159
      
 
160
      assertEquals(1, clientInvoker.getNumberOfAvailableConnections());
 
161
      
 
162
      client.disconnect();
 
163
      shutdownServer();
 
164
      log.info(getName() + " PASSES");
 
165
   }
 
166
   
 
167
   
 
168
   public void testMaxPoolSize() throws Throwable
 
169
   {
 
170
      log.info("entering " + getName());
 
171
      
 
172
      // Start server.
 
173
      setupServer();
 
174
      
 
175
      // Create client.
 
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);
 
182
      client.connect();
 
183
      log.info("client is connected");
 
184
      
 
185
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
186
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
187
      
 
188
      final HashMap metadata = new HashMap();
 
189
      metadata.put(DURATION, "20000");
 
190
      
 
191
      for (int i = 0; i < 20; i++)
 
192
      {
 
193
         new Thread()
 
194
         {
 
195
            public void run()
 
196
            {
 
197
               try
 
198
               {
 
199
                  client.invoke(WAIT, metadata);
 
200
               }
 
201
               catch (Throwable e)
 
202
               {
 
203
                  log.error("Error", e);
 
204
               }
 
205
            }
 
206
         }.start();
 
207
      }
 
208
      
 
209
      Thread.sleep(10000);
 
210
      assertEquals(10, invocationHandler.counter);
 
211
      assertEquals(0, clientInvoker.getNumberOfAvailableConnections());
 
212
      
 
213
      Thread.sleep(20000);
 
214
      assertEquals(20, invocationHandler.counter);
 
215
      assertEquals(0, clientInvoker.getNumberOfAvailableConnections());
 
216
      
 
217
      Thread.sleep(20000);
 
218
      assertEquals(20, invocationHandler.counter);
 
219
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
220
      
 
221
      client.disconnect();
 
222
      shutdownServer();
 
223
      log.info(getName() + " PASSES");
 
224
   }
 
225
   
 
226
   
 
227
   public void testRestartServer() throws Throwable
 
228
   {
 
229
      log.info("entering " + getName());
 
230
      
 
231
      // Start server.
 
232
      setupServer();
 
233
      
 
234
      // Create client.
 
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);
 
241
      client.connect();
 
242
      log.info("client is connected");
 
243
      
 
244
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
245
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
246
      
 
247
      for (int i = 0; i < 50; i++)
 
248
      {
 
249
         assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
 
250
      }
 
251
      
 
252
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
253
     
 
254
      disableServer(connector);
 
255
      shutdownServer();
 
256
      setupServer(port);
 
257
      
 
258
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
259
      
 
260
      for (int i = 0; i < 50; i++)
 
261
      {
 
262
         assertEquals(Integer.toString(i), (String) client.invoke(Integer.toString(i)));
 
263
      }
 
264
      
 
265
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
266
      
 
267
      client.disconnect();
 
268
      shutdownServer();
 
269
      log.info(getName() + " PASSES");
 
270
   }
 
271
   
 
272
   
 
273
   public void testSemaphoreReleaseAfterGetConnection() throws Throwable
 
274
   {
 
275
      log.info("entering " + getName());
 
276
      
 
277
      // Start server.
 
278
      setupServer();
 
279
      
 
280
      // Create client.
 
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);
 
290
      client.connect();
 
291
      log.info("client is connected");
 
292
      
 
293
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
294
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
295
      
 
296
      try
 
297
      {
 
298
         log.info(client.invoke("abc"));
 
299
         fail("expected exception");
 
300
      }
 
301
      catch (Exception e)
 
302
      {
 
303
         log.info("got expected exception");
 
304
      }
 
305
      
 
306
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
307
      
 
308
      client.disconnect();
 
309
      shutdownServer();
 
310
      log.info(getName() + " PASSES");
 
311
   }
 
312
   
 
313
   
 
314
   public void testSemaphoreReleaseAfterSocketException() throws Throwable
 
315
   {
 
316
      String exceptionClass = SocketWrapperWithSocketException.class.getName();
 
317
      doSemaphoreReleaseAfterSocketException(exceptionClass);
 
318
   }
 
319
   
 
320
   
 
321
   
 
322
   public void testSemaphoreReleaseAfterIOException() throws Throwable
 
323
   {
 
324
      String exceptionClass = SocketWrapperWithIOException.class.getName();
 
325
      doSemaphoreReleaseAfterSocketException(exceptionClass);
 
326
   }
 
327
   
 
328
   
 
329
   protected void doSemaphoreReleaseAfterSocketException(String exceptionClass) throws Throwable
 
330
   {
 
331
      log.info("entering " + getName());
 
332
      
 
333
      // Start server.
 
334
      setupServer();
 
335
      
 
336
      // Create client.
 
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);
 
346
      client.connect();
 
347
      log.info("client is connected");
 
348
      
 
349
      MicroSocketClientInvoker clientInvoker = (MicroSocketClientInvoker) client.getInvoker();
 
350
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
351
      
 
352
      try
 
353
      {
 
354
         log.info(client.invoke("abc"));
 
355
         fail("expected exception");
 
356
      }
 
357
      catch (Exception e)
 
358
      {
 
359
         log.info("got expected exception: " + e, e);
 
360
      }
 
361
      
 
362
      assertEquals(10, clientInvoker.getNumberOfAvailableConnections());
 
363
      
 
364
      client.disconnect();
 
365
      shutdownServer();
 
366
      log.info(getName() + " PASSES");
 
367
   }
 
368
   
 
369
   
 
370
   protected String getTransport()
 
371
   {
 
372
      return "socket";
 
373
   }
 
374
   
 
375
   
 
376
   protected void addExtraClientConfig(Map config) {}
 
377
   protected void addExtraServerConfig(Map config) {}
 
378
   
 
379
 
 
380
   protected void setupServer() throws Exception
 
381
   {
 
382
      setupServer(-1);
 
383
   }
 
384
   
 
385
   
 
386
   protected void setupServer(int localPort) throws Exception
 
387
   {
 
388
      host = InetAddress.getLocalHost().getHostAddress();
 
389
      
 
390
      if (localPort == -1)
 
391
         port = PortUtil.findFreePort(host);
 
392
      else
 
393
         port = localPort;
 
394
         
 
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);
 
402
      connector.create();
 
403
      invocationHandler = new TestInvocationHandler();
 
404
      connector.addInvocationHandler("test", invocationHandler);
 
405
 
 
406
      for (int i = 0; i < 5; i++)
 
407
      {
 
408
          try
 
409
          {
 
410
                  connector.start();
 
411
          }
 
412
          catch (Exception e)
 
413
          {
 
414
                  log.error("unable to start Connector");
 
415
                  Thread.sleep(60000);
 
416
          }
 
417
      }
 
418
   }
 
419
   
 
420
   
 
421
   protected void disableServer(Connector connector) throws Exception
 
422
   {
 
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);
 
429
      ss.close();
 
430
      
 
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);
 
437
      
 
438
      while (it.hasNext())
 
439
      {
 
440
         Socket socket = (Socket) field.get(it.next());
 
441
         socket.close();
 
442
      }
 
443
      
 
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);
 
450
      
 
451
      while (it.hasNext())
 
452
      {
 
453
         Socket socket = (Socket) field.get(it.next());
 
454
         socket.close();
 
455
      }
 
456
      
 
457
      log.info("disabled " + connector);
 
458
   }
 
459
   
 
460
   
 
461
   protected void shutdownServer() throws Exception
 
462
   {
 
463
      if (connector != null)
 
464
         connector.stop();
 
465
   }
 
466
   
 
467
   
 
468
   static class TestInvocationHandler implements ServerInvocationHandler
 
469
   {
 
470
      public int counter;
 
471
      private Object lock = new Object();
 
472
      
 
473
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
474
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
475
      {
 
476
         synchronized (lock)
 
477
         {
 
478
            counter++;
 
479
         }
 
480
         
 
481
         String command =  (String) invocation.getParameter();
 
482
         
 
483
         if (WAIT.equals(command))
 
484
         {
 
485
            Map metadata = invocation.getRequestPayload();
 
486
            int duration = Integer.parseInt((String) metadata.get(DURATION));
 
487
            Thread.sleep(duration);
 
488
         }
 
489
         
 
490
         return command;
 
491
      }
 
492
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
493
      public void setMBeanServer(MBeanServer server) {}
 
494
      public void setInvoker(ServerInvoker invoker) {}
 
495
   }
 
496
}
 
 
b'\\ No newline at end of file'