~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/ConnectionPoolSingleRetryTestCase.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.lang.reflect.Field;
 
25
import java.net.InetAddress;
 
26
import java.util.HashMap;
 
27
import java.util.LinkedList;
 
28
import java.util.Map;
 
29
 
 
30
import javax.management.MBeanServer;
 
31
 
 
32
import junit.framework.TestCase;
 
33
 
 
34
import org.apache.log4j.ConsoleAppender;
 
35
import org.apache.log4j.Level;
 
36
import org.apache.log4j.Logger;
 
37
import org.apache.log4j.PatternLayout;
 
38
import org.jboss.logging.XLevel;
 
39
import org.jboss.remoting.Client;
 
40
import org.jboss.remoting.InvocationRequest;
 
41
import org.jboss.remoting.InvokerLocator;
 
42
import org.jboss.remoting.ServerInvocationHandler;
 
43
import org.jboss.remoting.ServerInvoker;
 
44
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
45
import org.jboss.remoting.transport.Connector;
 
46
import org.jboss.remoting.transport.PortUtil;
 
47
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
 
48
import org.jboss.remoting.transport.socket.SocketWrapper;
 
49
 
 
50
 
 
51
/**
 
52
 * 
 
53
 * Unit test for JBREM-911.
 
54
 * 
 
55
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
56
 * @version $Revision: 1.1 $
 
57
 * <p>
 
58
 * Copyright Feb 27, 2008
 
59
 * </p>
 
60
 */
 
61
public class ConnectionPoolSingleRetryTestCase extends TestCase
 
62
{
 
63
   protected static Logger log = Logger.getLogger(ConnectionPoolSingleRetryTestCase.class);
 
64
   protected static String DELAY = "delay";
 
65
   private static boolean firstTime = true;
 
66
   
 
67
   protected String host;
 
68
   protected int port;
 
69
   protected String locatorURI;
 
70
   protected InvokerLocator serverLocator;
 
71
   protected Connector connector;
 
72
   protected TestInvocationHandler invocationHandler;
 
73
 
 
74
   
 
75
   public void setUp() throws Exception
 
76
   {
 
77
      if (firstTime)
 
78
      {
 
79
         firstTime = false;
 
80
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
81
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
82
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
83
         PatternLayout layout = new PatternLayout(pattern);
 
84
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
85
         Logger.getRootLogger().addAppender(consoleAppender);  
 
86
      }
 
87
   }
 
88
 
 
89
   
 
90
   public void tearDown()
 
91
   {
 
92
   }
 
93
   
 
94
   
 
95
   /**
 
96
    * Verifies that the "create new socket on last retry" feature does not apply when
 
97
    * maxPoolSize == 1.  Verifies that the same socket is reused multiple times.
 
98
    */
 
99
   public void testMaxPoolSizeOne() throws Throwable
 
100
   {
 
101
      log.info("entering " + getName());
 
102
      
 
103
      // Start server - no connection checking.
 
104
      setupServer();
 
105
      
 
106
      // Create first client.
 
107
      HashMap config = new HashMap();
 
108
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
109
      config.put("numberOfCallRetries", "1");
 
110
      addExtraClientConfig(config);
 
111
      Client client = new Client(serverLocator, config);
 
112
      client.connect();
 
113
      
 
114
      assertEquals("abc", client.invoke("abc"));
 
115
      log.info("connection is good");
 
116
      
 
117
      // Verify numberOfCallRetries == 1.
 
118
      assertTrue(client.getInvoker() instanceof MicroSocketClientInvoker);
 
119
      MicroSocketClientInvoker invoker = (MicroSocketClientInvoker) client.getInvoker();
 
120
      assertEquals(1, invoker.getNumberOfCallRetries());
 
121
      
 
122
      // Get connection pool.
 
123
      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
 
124
      field.setAccessible(true);
 
125
      LinkedList pool = (LinkedList) field.get(invoker);
 
126
      assertEquals(1, pool.size());
 
127
      SocketWrapper socketWrapper = (SocketWrapper) pool.get(0);
 
128
      
 
129
      for (int i = 0; i < 10000; i++)
 
130
      {
 
131
         client.invoke("xyz");
 
132
      }
 
133
      
 
134
      log.info("made 10000 invocations");
 
135
      assertEquals(1, pool.size());
 
136
      assertEquals(socketWrapper, pool.get(0));
 
137
      
 
138
      client.disconnect();
 
139
      shutdownServer();
 
140
      log.info(getName() + " PASSES");
 
141
   }
 
142
   
 
143
   
 
144
   protected String getTransport()
 
145
   {
 
146
      return "socket";
 
147
   }
 
148
   
 
149
   
 
150
   protected void addExtraClientConfig(Map config) {}
 
151
   protected void addExtraServerConfig(Map config) {}
 
152
   
 
153
 
 
154
   protected void setupServer() throws Exception
 
155
   {
 
156
      host = InetAddress.getLocalHost().getHostAddress();
 
157
      port = PortUtil.findFreePort(host);
 
158
      locatorURI = getTransport() + "://" + host + ":" + port; 
 
159
      serverLocator = new InvokerLocator(locatorURI);
 
160
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
161
      HashMap config = new HashMap();
 
162
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
163
      addExtraServerConfig(config);
 
164
      connector = new Connector(serverLocator, config);
 
165
      connector.create();
 
166
      invocationHandler = new TestInvocationHandler();
 
167
      connector.addInvocationHandler("test", invocationHandler);
 
168
      connector.start();
 
169
   }
 
170
   
 
171
   
 
172
   protected void shutdownServer() throws Exception
 
173
   {
 
174
      if (connector != null)
 
175
         connector.stop();
 
176
   }
 
177
   
 
178
   
 
179
   static class TestInvocationHandler implements ServerInvocationHandler
 
180
   {
 
181
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
182
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
183
      {
 
184
         return invocation.getParameter();
 
185
      }
 
186
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
187
      public void setMBeanServer(MBeanServer server) {}
 
188
      public void setInvoker(ServerInvoker invoker) {}
 
189
   }
 
190
}
 
 
b'\\ No newline at end of file'