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