~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/bisocket/BisocketLeakTest.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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.bisocket;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
 
 
27
import javax.management.MBeanServer;
 
28
 
 
29
import junit.framework.TestCase;
 
30
 
 
31
import org.apache.log4j.ConsoleAppender;
 
32
import org.apache.log4j.Level;
 
33
import org.apache.log4j.Logger;
 
34
import org.apache.log4j.PatternLayout;
 
35
import org.jboss.remoting.Client;
 
36
import org.jboss.remoting.InvocationRequest;
 
37
import org.jboss.remoting.InvokerLocator;
 
38
import org.jboss.remoting.ServerInvocationHandler;
 
39
import org.jboss.remoting.ServerInvoker;
 
40
import org.jboss.remoting.callback.Callback;
 
41
import org.jboss.remoting.callback.HandleCallbackException;
 
42
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
43
import org.jboss.remoting.transport.Connector;
 
44
import org.jboss.remoting.transport.PortUtil;
 
45
import org.jboss.remoting.transport.bisocket.Bisocket;
 
46
 
 
47
/** 
 
48
 * This class can be profiled for memory leaks.  See JBREM-721.
 
49
 * 
 
50
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
51
 * @version $Revision: 2316 $
 
52
 * <p>
 
53
 * Copyright March 15, 2007
 
54
 * </p>
 
55
 */
 
56
public class BisocketLeakTest extends TestCase
 
57
{
 
58
   private static Logger log = Logger.getLogger(BisocketLeakTest.class);
 
59
   private static String transport = "bisocket";
 
60
   private static int LOOPS = 1000;
 
61
   
 
62
   
 
63
   public void testConnectionCreation() throws Throwable
 
64
   {
 
65
      Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
66
      Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
67
      String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
68
      PatternLayout layout = new PatternLayout(pattern);
 
69
      ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
70
      Logger.getRootLogger().addAppender(consoleAppender); 
 
71
      
 
72
      String host = InetAddress.getLocalHost().getHostName();
 
73
      int port = PortUtil.findFreePort(host);
 
74
      String locatorURI = transport + "://" + host + ":" + port; 
 
75
      InvokerLocator serverLocator = new InvokerLocator(locatorURI);
 
76
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
77
      HashMap serverConfig = new HashMap();
 
78
      serverConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
79
      serverConfig.put(Bisocket.IS_CALLBACK_SERVER, "false");
 
80
      Connector connector = new Connector(serverLocator, serverConfig);
 
81
      connector.create();
 
82
      SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
 
83
      connector.addInvocationHandler("sample", invocationHandler);
 
84
      connector.start();
 
85
      log.info("Started remoting server with locator uri of: " + locatorURI);
 
86
      
 
87
      HashMap clientConfig = new HashMap();
 
88
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
89
      clientConfig.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
90
      log.info("client onnecting to: " + serverLocator);
 
91
      Client client = new Client(serverLocator, clientConfig);
 
92
      client.connect();
 
93
      log.info("client is connected");
 
94
      SampleCallbackHandler callbackHandler = new SampleCallbackHandler();
 
95
      
 
96
      long start = System.currentTimeMillis();
 
97
      for (int i = 0; i < LOOPS; i++)
 
98
      {
 
99
         client.invoke("test");
 
100
         client.addListener(callbackHandler, new HashMap(), null, true);
 
101
         client.removeListener(callbackHandler);
 
102
         assertEquals(i+1, callbackHandler.counter);
 
103
         if ((i+1) % 100 == 0)
 
104
            log.info("connections: " + (i+1));
 
105
      }
 
106
      long finish = System.currentTimeMillis();
 
107
      log.info("time to create " + LOOPS + " connections: " + (finish - start));
 
108
      
 
109
      client.disconnect();
 
110
      connector.stop();
 
111
   }
 
112
   
 
113
   
 
114
   /**
 
115
    * Can pass transport and port to be used as parameters.
 
116
    *
 
117
    * @param args
 
118
    */
 
119
   public static void main(String[] args)
 
120
   {
 
121
      if(args != null && args.length > 0)
 
122
      {
 
123
         transport = args[0];
 
124
         if (args.length > 1)
 
125
            LOOPS = Integer.parseInt(args[1]);
 
126
      }
 
127
      BisocketLeakTest testCase = new BisocketLeakTest();
 
128
      try
 
129
      {
 
130
         testCase.setUp();
 
131
         testCase.testConnectionCreation();
 
132
         testCase.tearDown();
 
133
         log.info("done");
 
134
      }
 
135
      catch(Throwable e)
 
136
      {
 
137
         e.printStackTrace();
 
138
      }
 
139
   }
 
140
 
 
141
   /**
 
142
    * Simple invocation handler implementation.  When callback client's are registered, will
 
143
    * generate callbacks periodically.
 
144
    */
 
145
   static class SampleInvocationHandler implements ServerInvocationHandler
 
146
   {
 
147
      public void addListener(InvokerCallbackHandler callbackHandler)
 
148
      {
 
149
         log.debug("Adding callback listener.");
 
150
         try
 
151
         {
 
152
            callbackHandler.handleCallback(new Callback("callback"));
 
153
         }
 
154
         catch (HandleCallbackException e)
 
155
         {
 
156
            log.error("Unable to send callback");
 
157
         }
 
158
      }
 
159
 
 
160
      public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
 
161
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
162
      public void setMBeanServer(MBeanServer server) {}
 
163
      public void setInvoker(ServerInvoker invoker) {}
 
164
   }
 
165
   
 
166
   
 
167
   static class SampleCallbackHandler implements InvokerCallbackHandler
 
168
   {
 
169
      int counter;
 
170
      
 
171
      public void handleCallback(Callback callback) throws HandleCallbackException
 
172
      {
 
173
         counter++;
 
174
         if ((counter + 1) % 1000 == 0)
 
175
         {
 
176
            System.out.println("received callback " + (counter + 1));
 
177
         }
 
178
      }
 
179
   }
 
180
}