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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/soak/ProducerConsumerSoakTestParent.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.soak;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.HashSet;
 
27
import java.util.Iterator;
 
28
import java.util.Map;
 
29
import java.util.Timer;
 
30
import java.util.TimerTask;
 
31
 
 
32
import javax.management.MBeanServer;
 
33
 
 
34
import junit.framework.TestCase;
 
35
 
 
36
import org.apache.log4j.ConsoleAppender;
 
37
import org.apache.log4j.Level;
 
38
import org.apache.log4j.Logger;
 
39
import org.apache.log4j.PatternLayout;
 
40
import org.jboss.logging.XLevel;
 
41
import org.jboss.remoting.Client;
 
42
import org.jboss.remoting.InvocationRequest;
 
43
import org.jboss.remoting.InvokerLocator;
 
44
import org.jboss.remoting.ServerInvocationHandler;
 
45
import org.jboss.remoting.ServerInvoker;
 
46
import org.jboss.remoting.callback.Callback;
 
47
import org.jboss.remoting.callback.HandleCallbackException;
 
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
49
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
 
50
import org.jboss.remoting.transport.Connector;
 
51
import org.jboss.remoting.transport.PortUtil;
 
52
import org.jboss.remoting.transport.bisocket.Bisocket;
 
53
 
 
54
 
 
55
public abstract class ProducerConsumerSoakTestParent extends TestCase
 
56
{
 
57
   private static Logger log = Logger.getLogger(ProducerConsumerSoakTestParent.class);
 
58
   
 
59
   private static boolean firstTime = true;
 
60
   
 
61
   protected String host;
 
62
   protected int port;
 
63
   protected String locatorURI;
 
64
   protected InvokerLocator serverLocator;
 
65
   protected Connector connector;
 
66
   protected TestInvocationHandler invocationHandler;
 
67
 
 
68
   
 
69
   public void setUp() throws Exception
 
70
   {
 
71
      if (firstTime)
 
72
      {
 
73
         firstTime = false;
 
74
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
75
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
76
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
77
         PatternLayout layout = new PatternLayout(pattern);
 
78
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
79
         Logger.getRootLogger().addAppender(consoleAppender);  
 
80
      }
 
81
   }
 
82
 
 
83
   
 
84
   public void tearDown()
 
85
   {
 
86
   }
 
87
   
 
88
   
 
89
   public void testSoak() throws Throwable
 
90
   {
 
91
      log.info("entering " + getName());
 
92
      System.gc();
 
93
      log.info("free space: " + Runtime.getRuntime().freeMemory());
 
94
      
 
95
      // Start server.
 
96
      setupServer();
 
97
      
 
98
      // Create clients.
 
99
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
100
      HashMap clientConfig = new HashMap();
 
101
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
102
      addExtraClientConfig(clientConfig);
 
103
      Client clients[] = new Client[SoakConstants.SENDERS];
 
104
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
105
      Sender senders[] = new Sender[SoakConstants.SENDERS];
 
106
      
 
107
      for (int i = 0; i < SoakConstants.SENDERS; i++)
 
108
      {
 
109
         clients[i] = new Client(clientLocator, clientConfig);
 
110
         clients[i].connect();
 
111
         log.info("client " + i + " is connected");
 
112
         HashMap callbackMetadata = new HashMap();
 
113
         
 
114
         for (int j = 0; j < SoakConstants.CALLBACK_LISTENERS; j++)
 
115
         {
 
116
            callbackMetadata.clear();
 
117
            addExtraCallbackConfig(callbackMetadata);
 
118
            clients[i].addListener(callbackHandler, callbackMetadata, null, true);
 
119
         }
 
120
         log.info("callback handlers installed for client " + i);
 
121
         
 
122
         senders[i] = new Sender(i, clients[i]);
 
123
      }
 
124
 
 
125
      Timer timer = new Timer(true);
 
126
      timer.schedule(new IntervalTimerTask(), 60000, 60000);
 
127
      
 
128
      for (int i = 0; i < SoakConstants.SENDERS; i++)
 
129
      {
 
130
         senders[i].start();
 
131
      }
 
132
      log.info("senders atarted");
 
133
      
 
134
      int invocations = 0;
 
135
      for (int i = 0; i < SoakConstants.SENDERS; i++)
 
136
      {
 
137
         senders[i].join();
 
138
         invocations += senders[i].counter;
 
139
      }
 
140
      
 
141
      log.info("senders done");
 
142
      log.info("invocations made: " + invocations);
 
143
      log.info("invocations received: " + invocationHandler.counter);
 
144
      log.info("callbacks received: " + callbackHandler.counter);
 
145
      
 
146
      assertEquals(invocations, invocationHandler.counter);
 
147
      assertEquals(invocations * SoakConstants.SENDERS * SoakConstants.CALLBACK_LISTENERS,
 
148
                   callbackHandler.counter);
 
149
      
 
150
      for (int i = 0; i < SoakConstants.SENDERS; i++)
 
151
      {
 
152
         clients[i].removeListener(callbackHandler);
 
153
         clients[i].disconnect();
 
154
      }         
 
155
 
 
156
      shutdownServer();
 
157
      System.gc();
 
158
      log.info("free space: " + Runtime.getRuntime().freeMemory());
 
159
      log.info(getName() + " PASSES");
 
160
   }
 
161
   
 
162
   
 
163
   protected abstract String getTransport();
 
164
   
 
165
   
 
166
   protected void addExtraClientConfig(Map config) {}
 
167
   protected void addExtraServerConfig(Map config) {}
 
168
   protected void addExtraCallbackConfig(Map config) {}
 
169
   
 
170
 
 
171
   protected void setupServer() throws Exception
 
172
   {
 
173
      host = InetAddress.getLocalHost().getHostAddress();
 
174
      port = PortUtil.findFreePort(host);
 
175
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
176
      serverLocator = new InvokerLocator(locatorURI);
 
177
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
178
      HashMap config = new HashMap();
 
179
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
180
      addExtraServerConfig(config);
 
181
      connector = new Connector(serverLocator, config);
 
182
      connector.create();
 
183
      invocationHandler = new TestInvocationHandler();
 
184
      connector.addInvocationHandler("test", invocationHandler);
 
185
      connector.start();
 
186
   }
 
187
   
 
188
   
 
189
   protected void shutdownServer() throws Exception
 
190
   {
 
191
      if (connector != null)
 
192
         connector.stop();
 
193
   }
 
194
   
 
195
   
 
196
   static class TestInvocationHandler implements ServerInvocationHandler
 
197
   {
 
198
      HashSet listeners = new HashSet();
 
199
      int counter;
 
200
      Object lock = new Object();
 
201
      
 
202
      public void addListener(InvokerCallbackHandler callbackHandler)
 
203
      {
 
204
         listeners.add(callbackHandler);
 
205
      }
 
206
      
 
207
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
208
      {
 
209
         synchronized (lock)
 
210
         {
 
211
            counter++;
 
212
            if ((counter + 1) % SoakConstants.INTERVAL == 0)
 
213
               log.info("invocations received: " + (counter + 1));
 
214
         }
 
215
         
 
216
         Object o = invocation.getParameter();
 
217
         Callback c = new Callback(o);
 
218
         Iterator it = listeners.iterator();
 
219
         while(it.hasNext())
 
220
         {
 
221
            ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) it.next();
 
222
            handler.handleCallbackOneway(c);
 
223
            handler.handleCallback(c);
 
224
         }
 
225
         
 
226
         return o;
 
227
      }
 
228
      
 
229
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
230
      public void setMBeanServer(MBeanServer server) {}
 
231
      public void setInvoker(ServerInvoker invoker) {}
 
232
   }
 
233
   
 
234
   
 
235
   static class TestCallbackHandler implements InvokerCallbackHandler
 
236
   {
 
237
      int counter;
 
238
      Object lock = new Object();
 
239
      
 
240
      public void handleCallback(Callback callback) throws HandleCallbackException
 
241
      {
 
242
         synchronized (lock)
 
243
         {
 
244
            counter++;
 
245
            if ((counter + 1) % SoakConstants.INTERVAL == 0)
 
246
               log.info("callbacks received: " + (counter + 1));
 
247
         }
 
248
      }  
 
249
   }
 
250
   
 
251
   static class Sender extends Thread
 
252
   {
 
253
      String name;
 
254
      Client client;
 
255
      int counter;
 
256
      Object lock = new Object();
 
257
      long start;
 
258
      
 
259
      public Sender(int id, Client client)
 
260
      {
 
261
         name = "sender:" + id;
 
262
         this.client = client;
 
263
      }
 
264
      
 
265
      public void run()
 
266
      {
 
267
         try
 
268
         {
 
269
            start = System.currentTimeMillis();
 
270
            while (System.currentTimeMillis() - start <= SoakConstants.DURATION)
 
271
            {
 
272
               counter++;
 
273
               if ((counter + 1) % SoakConstants.INTERVAL == 0)
 
274
                  log.info(name + " invcations made: : " + (counter + 1));
 
275
               client.invoke(name + ":" + counter);
 
276
            }
 
277
         }
 
278
         catch (Throwable t)
 
279
         {
 
280
            log.error(this, t);
 
281
         }
 
282
      }
 
283
   }
 
284
   
 
285
   
 
286
   static class IntervalTimerTask extends TimerTask
 
287
   {
 
288
      int counter;
 
289
      
 
290
      public void run()
 
291
      {
 
292
         log.info("MINUTES ELAPSED: " + ++counter);
 
293
      }
 
294
   }
 
295
}
 
 
b'\\ No newline at end of file'