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

« back to all changes in this revision

Viewing changes to .pc/0001-convert-to-official-Java-concurrent-packages.patch/src/tests/org/jboss/test/remoting/transport/socket/load/PooledConnectionTestCase.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
package org.jboss.test.remoting.transport.socket.load;
 
2
 
 
3
import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
 
4
import junit.framework.TestCase;
 
5
import org.apache.log4j.Logger;
 
6
import org.jboss.remoting.Client;
 
7
import org.jboss.remoting.InvokerLocator;
 
8
import org.jboss.remoting.transport.Connector;
 
9
 
 
10
public class PooledConnectionTestCase extends TestCase
 
11
{
 
12
   private static int numOfRunnerThreads = 10;
 
13
//   private static int numOfRunnerThreads = 3;
 
14
   private static SynchronizedInt responseCount = new SynchronizedInt(0);
 
15
   private Connector connector;
 
16
 
 
17
//   static
 
18
//   {
 
19
//      BasicConfigurator.configure();
 
20
//      Logger.getRootLogger().setLevel(Level.INFO);
 
21
//      Logger.getInstance("org.jboss.remoting.transport.socket").setLevel(Level.ALL);
 
22
//   }
 
23
 
 
24
   private Logger logger = Logger.getRootLogger();
 
25
 
 
26
   protected String getTransport()
 
27
   {
 
28
      return "socket";
 
29
   }
 
30
   
 
31
   public static void main(String[] args) throws Throwable
 
32
   {
 
33
      PooledConnectionTestCase rt = new PooledConnectionTestCase();
 
34
      rt.startServer();
 
35
//      rt.runMultipleClients(Integer.parseInt(args[1]));
 
36
      rt.runMultipleClients(PooledConnectionTestCase.numOfRunnerThreads);
 
37
   }
 
38
 
 
39
   public void setUp() throws Exception
 
40
   {
 
41
      startServer();
 
42
   }
 
43
 
 
44
   public void tearDown() throws Exception
 
45
   {
 
46
      if(connector != null)
 
47
      {
 
48
         connector.stop();
 
49
         connector.destroy();
 
50
      }
 
51
   }
 
52
 
 
53
   public void startServer() throws Exception
 
54
   {
 
55
      String locatorURI = getTransport() + "://localhost:54000/?maxPoolSize=10&timeout=10000";
 
56
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
57
 
 
58
      connector = new Connector();
 
59
 
 
60
      connector.setInvokerLocator(locator.getLocatorURI());
 
61
      connector.create();
 
62
 
 
63
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
64
      connector.addInvocationHandler("sample", invocationHandler);
 
65
      connector.start();
 
66
   }
 
67
 
 
68
   public void testRunClients() throws Throwable
 
69
   {
 
70
      runMultipleClients(PooledConnectionTestCase.numOfRunnerThreads);
 
71
      // waiting 8 seconds as the server handler will have waited client calls
 
72
      // 5 seconds, but should only be able to make client invocations 2 at a time.
 
73
      Thread.currentThread().sleep(8000);
 
74
      // should not have all invocations as should be blocking within client invoker (as only
 
75
      // allows 2 concurrent client connections).
 
76
      assertFalse(10 == PooledConnectionTestCase.responseCount.get());
 
77
 
 
78
      Thread.currentThread().sleep(120000);
 
79
      System.out.println("Response count = " + PooledConnectionTestCase.responseCount + ".  Expected 10.");
 
80
      assertEquals(10, PooledConnectionTestCase.responseCount.get());
 
81
   }
 
82
 
 
83
   public void runClient(String clientId) throws Throwable
 
84
   {
 
85
      String locatorURI = getTransport() + "://localhost:54000/?clientMaxPoolSize=2";
 
86
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
87
      Client client = new Client(locator);
 
88
      client.connect();
 
89
      String req = clientId;
 
90
      Object resp = client.invoke(req);
 
91
      PooledConnectionTestCase.responseCount.increment();
 
92
      System.out.println("Received response of: " + resp + ".  Response count = " + PooledConnectionTestCase.responseCount);
 
93
      System.in.read();
 
94
   }
 
95
 
 
96
      public void runMultipleClients(int cnt) throws Throwable {
 
97
      for (int i = 0; i < cnt; i++) {
 
98
         Thread t = new Thread(new Runnable() {
 
99
            public void run() {
 
100
               try {
 
101
                  Thread.sleep(1000);
 
102
                  runClient(Thread.currentThread().getName());
 
103
               } catch (Throwable e) {
 
104
                  logger.error(e);
 
105
                  e.printStackTrace();
 
106
               }
 
107
            }
 
108
         }, Integer.toString(i));
 
109
         t.start();
 
110
      }
 
111
   }
 
112
}