~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/connectionpool/ConnectionPoolRetryTestCase.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.connectionpool;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
 
 
30
import junit.framework.TestCase;
 
31
 
 
32
import org.apache.log4j.ConsoleAppender;
 
33
import org.apache.log4j.Level;
 
34
import org.apache.log4j.Logger;
 
35
import org.apache.log4j.PatternLayout;
 
36
import org.jboss.logging.XLevel;
 
37
import org.jboss.remoting.Client;
 
38
import org.jboss.remoting.InvocationRequest;
 
39
import org.jboss.remoting.InvokerLocator;
 
40
import org.jboss.remoting.ServerInvocationHandler;
 
41
import org.jboss.remoting.ServerInvoker;
 
42
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
43
import org.jboss.remoting.transport.Connector;
 
44
import org.jboss.remoting.transport.PortUtil;
 
45
 
 
46
 
 
47
/**
 
48
 * Unit test for JBREM-786, JBREM-890.
 
49
 * 
 
50
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
51
 * @version $Revision: 1.1 $
 
52
 * <p>
 
53
 * Copyright Dec 21, 2007
 
54
 * </p>
 
55
 */
 
56
public class ConnectionPoolRetryTestCase extends TestCase
 
57
{
 
58
   protected static Logger log = Logger.getLogger(ConnectionPoolRetryTestCase.class);
 
59
   protected static String DELAY = "delay";
 
60
   private static boolean firstTime = true;
 
61
   
 
62
   protected String host;
 
63
   protected int port;
 
64
   protected String locatorURI;
 
65
   protected InvokerLocator serverLocator;
 
66
   protected Connector connector;
 
67
   protected TestInvocationHandler invocationHandler;
 
68
 
 
69
   
 
70
   public void setUp() throws Exception
 
71
   {
 
72
      if (firstTime)
 
73
      {
 
74
         firstTime = false;
 
75
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
76
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
77
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
78
         PatternLayout layout = new PatternLayout(pattern);
 
79
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
80
         Logger.getRootLogger().addAppender(consoleAppender);  
 
81
      }
 
82
   }
 
83
 
 
84
   
 
85
   public void tearDown()
 
86
   {
 
87
   }
 
88
   
 
89
   
 
90
   /**
 
91
    * Verifies a new connection is created on the last attempt to make
 
92
    * an invocation.  Tries to create a situation in which server threads are
 
93
    * frequently evicted and reused with a new socket, leaving the matching
 
94
    * connection in the client side connection pool unusable.  If the probability of
 
95
    * getting an unusable connection is high, it is likely that the number
 
96
    * of retries would be exhausted and an exception thrown.  The new facility
 
97
    * for always creating a new connection on the last attempt should make it
 
98
    * possible to avoid running out of retries.
 
99
    */
 
100
   public void testNewConnectionOnLastRetry() throws Throwable
 
101
   {
 
102
      log.info("entering " + getName());
 
103
      
 
104
      // Start server - no connection checking.
 
105
      setupServer();
 
106
      
 
107
      // Create first client.
 
108
      InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
 
109
      HashMap clientConfig1 = new HashMap();
 
110
      clientConfig1.put(InvokerLocator.FORCE_REMOTE, "true");
 
111
      clientConfig1.put("numberOfCallRetries", "2");
 
112
      addExtraClientConfig(clientConfig1);
 
113
      Client client1 = new Client(clientLocator1, clientConfig1);
 
114
      client1.connect();
 
115
      
 
116
      int BANGERS = 50;
 
117
      int INVOCATIONS = 5000;
 
118
      BangerThread[] bangers = new BangerThread[BANGERS];
 
119
      for (int i = 0; i < BANGERS; i++)
 
120
      {
 
121
         bangers[i] = new BangerThread(client1, INVOCATIONS, "banger:" + i);
 
122
      }
 
123
      
 
124
      String newLocatorURI = locatorURI + "/?timeout=100000";
 
125
      InvokerLocator clientLocator2 = new InvokerLocator(newLocatorURI);
 
126
      HashMap clientConfig2 = new HashMap();
 
127
      clientConfig2.put(InvokerLocator.FORCE_REMOTE, "true");
 
128
      clientConfig1.put("numberOfCallRetries", "4");
 
129
      addExtraClientConfig(clientConfig2);
 
130
      Client client2 = new Client(clientLocator2, clientConfig2);
 
131
      client2.connect();
 
132
 
 
133
      for (int i = 0; i < BANGERS; i++)
 
134
      {
 
135
         bangers[i].start();
 
136
      }
 
137
      
 
138
      for (int i = 0; i < BANGERS; i++)
 
139
      {
 
140
         bangers[i].join();
 
141
      }
 
142
      
 
143
      for (int i = 0; i < BANGERS; i++)
 
144
      {
 
145
         log.info("banger " + i + " done: " + bangers[i].done);
 
146
         log.info("banger " + i + " ok:   " + bangers[i].ok);
 
147
      }
 
148
         
 
149
      for (int i = 0; i < BANGERS; i++)
 
150
      {
 
151
         assertTrue("banger " + i + " not done", bangers[i].done);
 
152
         assertTrue("banger " + i + " experienced error", bangers[i].ok);
 
153
      }
 
154
 
 
155
      client1.disconnect();
 
156
      client2.disconnect();
 
157
      shutdownServer();
 
158
      log.info(getName() + " PASSES");
 
159
   }
 
160
   
 
161
   
 
162
   protected String getTransport()
 
163
   {
 
164
      return "socket";
 
165
   }
 
166
   
 
167
   
 
168
   protected void addExtraClientConfig(Map config) {}
 
169
   protected void addExtraServerConfig(Map config) {}
 
170
   
 
171
 
 
172
   protected void setupServer() throws Exception
 
173
   {
 
174
      host = InetAddress.getLocalHost().getHostAddress();
 
175
      port = PortUtil.findFreePort(host);
 
176
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
177
      serverLocator = new InvokerLocator(locatorURI);
 
178
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
179
      HashMap config = new HashMap();
 
180
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
181
      config.put("maxPoolSize", "2");
 
182
      addExtraServerConfig(config);
 
183
      connector = new Connector(serverLocator, config);
 
184
      connector.create();
 
185
      invocationHandler = new TestInvocationHandler();
 
186
      connector.addInvocationHandler("test", invocationHandler);
 
187
      connector.start();
 
188
   }
 
189
   
 
190
   
 
191
   protected void shutdownServer() throws Exception
 
192
   {
 
193
      if (connector != null)
 
194
         connector.stop();
 
195
   }
 
196
   
 
197
   
 
198
   static class TestInvocationHandler implements ServerInvocationHandler
 
199
   {
 
200
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
201
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
202
      {
 
203
         return invocation.getParameter();
 
204
      }
 
205
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
206
      public void setMBeanServer(MBeanServer server) {}
 
207
      public void setInvoker(ServerInvoker invoker) {}
 
208
   }
 
209
 
 
210
   
 
211
   static class BangerThread extends Thread
 
212
   {
 
213
      public boolean done;
 
214
      public boolean ok;
 
215
      private Client client;
 
216
      private int count;
 
217
      private String name;
 
218
      private boolean error;
 
219
      
 
220
      public BangerThread(Client client, int count, String name)
 
221
      {
 
222
         this.client = client;
 
223
         this.count = count;
 
224
         this.name = name;
 
225
         setName(name);
 
226
      }
 
227
      public void run()
 
228
      {
 
229
         log.info(name + ": started");
 
230
         for (int i = 0; i < count; i++)
 
231
         {
 
232
            try
 
233
            {
 
234
               long start = System.currentTimeMillis();
 
235
               client.invoke(name + ":" + i);
 
236
               long duration = System.currentTimeMillis() - start;
 
237
               if (duration > 2000)
 
238
               {
 
239
                  log.info(this + " invocation(" + i + ") took " + duration + " ms");
 
240
               }
 
241
               
 
242
               if ((i + 1) % 1000 == 0)
 
243
                  log.info(name + " got response: " + (i+1));
 
244
            }
 
245
            catch (Throwable e)
 
246
            {
 
247
               log.error("error in thread: " + name + ", invocation: " + i, e);
 
248
               error = true;
 
249
            }
 
250
         }
 
251
         
 
252
         ok = !error;
 
253
         done = true;
 
254
      }
 
255
   }
 
256
}
 
 
b'\\ No newline at end of file'