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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/timeout/ServerThreadReuseAfterTimeoutTestCase.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.timeout;
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
 
import java.util.Set;
30
 
 
31
 
import javax.management.MBeanServer;
32
 
 
33
 
import junit.framework.TestCase;
34
 
 
35
 
import org.apache.log4j.ConsoleAppender;
36
 
import org.apache.log4j.Level;
37
 
import org.apache.log4j.Logger;
38
 
import org.apache.log4j.PatternLayout;
39
 
import org.jboss.logging.XLevel;
40
 
import org.jboss.remoting.Client;
41
 
import org.jboss.remoting.InvocationRequest;
42
 
import org.jboss.remoting.InvokerLocator;
43
 
import org.jboss.remoting.ServerInvocationHandler;
44
 
import org.jboss.remoting.ServerInvoker;
45
 
import org.jboss.remoting.callback.Callback;
46
 
import org.jboss.remoting.callback.HandleCallbackException;
47
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
48
 
import org.jboss.remoting.transport.Connector;
49
 
import org.jboss.remoting.transport.PortUtil;
50
 
import org.jboss.remoting.transport.socket.LRUPool;
51
 
import org.jboss.remoting.transport.socket.ServerThread;
52
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
53
 
 
54
 
 
55
 
public class ServerThreadReuseAfterTimeoutTestCase extends TestCase
56
 
{
57
 
   private static Logger log = Logger.getLogger(ServerThreadReuseAfterTimeoutTestCase.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 testJavaSerializationDefault() throws Throwable
90
 
   {
91
 
      log.info("entering " + getName());
92
 
      Map config = new HashMap();
93
 
      doJavaSerializationTest(config);
94
 
      log.info(getName() + " PASSES");
95
 
   }
96
 
   
97
 
   
98
 
   public void testJavaSerializationConfigured() throws Throwable
99
 
   {
100
 
      log.info("entering " + getName());
101
 
      Map config = new HashMap();
102
 
      config.put(ServerThread.CONTINUE_AFTER_TIMEOUT, "false");
103
 
      doJavaSerializationTest(config);
104
 
      log.info(getName() + " PASSES");
105
 
   }
106
 
   
107
 
 
108
 
   public void testJBossSerializationDefault() throws Throwable
109
 
   {
110
 
      log.info("entering " + getName());
111
 
      Map config = new HashMap();
112
 
      doJBossSerializationTest(config);
113
 
      log.info(getName() + " PASSES");
114
 
   }
115
 
   
116
 
   
117
 
   public void testJBossSerializationConfigured() throws Throwable
118
 
   {
119
 
      log.info("entering " + getName());
120
 
      Map config = new HashMap();
121
 
      config.put(ServerThread.CONTINUE_AFTER_TIMEOUT, "true");
122
 
      doJBossSerializationTest(config);
123
 
      log.info(getName() + " PASSES");
124
 
   }
125
 
   
126
 
   
127
 
   public void doJavaSerializationTest(Map clientConfig) throws Throwable
128
 
   {
129
 
      log.info("entering " + getName());
130
 
      
131
 
      // Start server.
132
 
      setupServer("java");
133
 
      
134
 
      // Create client.
135
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
136
 
      clientConfig = new HashMap();
137
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
138
 
      addExtraClientConfig(clientConfig);
139
 
      Client client = new Client(clientLocator, clientConfig);
140
 
      client.connect();
141
 
      log.info("client is connected");
142
 
      
143
 
      // Test connections.
144
 
      assertEquals("abc", client.invoke("abc"));
145
 
      log.info("connection is good");
146
 
      
147
 
      // Get ServerThread.
148
 
      SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
149
 
      Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
150
 
      field.setAccessible(true);
151
 
      LRUPool clientpool = (LRUPool) field.get(invoker);
152
 
      assertEquals(1, clientpool.size());
153
 
      Set s =  clientpool.getContents();
154
 
      ServerThread serverThread1 = (ServerThread) s.iterator().next();
155
 
      
156
 
      // Get threadpool.
157
 
      field = SocketServerInvoker.class.getDeclaredField("threadpool");
158
 
      field.setAccessible(true);
159
 
      LinkedList threadpool = (LinkedList) field.get(invoker);
160
 
      assertEquals(0, threadpool.size());
161
 
      
162
 
      // Wait for ServerThread to time out.
163
 
      Thread.sleep(6000);
164
 
      for (int i = 0; i < 5; i++)
165
 
      {
166
 
         Thread.sleep(2000);
167
 
         if (clientpool.size() == 0) break;
168
 
      }
169
 
      
170
 
      if (clientpool.size() > 0)
171
 
      {
172
 
         fail("expect clientpool.size() == 0");
173
 
      }
174
 
      
175
 
      // Verify original ServerThread was returned to threadpool.
176
 
      assertEquals(1, threadpool.size());
177
 
      assertEquals(serverThread1, threadpool.iterator().next());
178
 
      
179
 
      // Make another invocation and verify ServerThread was reused.
180
 
      client.invoke("xyz");
181
 
      assertEquals(1, clientpool.size());
182
 
      s =  clientpool.getContents();
183
 
      ServerThread serverThread2 = (ServerThread) s.iterator().next();
184
 
      assertEquals(serverThread1, serverThread2);
185
 
      
186
 
      client.disconnect();
187
 
      shutdownServer();
188
 
      log.info(getName() + " PASSES");
189
 
   }
190
 
   
191
 
   
192
 
   public void doJBossSerializationTest(Map clientConfig) throws Throwable
193
 
   {
194
 
      log.info("entering " + getName());
195
 
      
196
 
      // Start server.
197
 
      setupServer("jboss");
198
 
      
199
 
      // Create client.
200
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
201
 
      clientConfig = new HashMap();
202
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
203
 
      addExtraClientConfig(clientConfig);
204
 
      Client client = new Client(clientLocator, clientConfig);
205
 
      client.connect();
206
 
      log.info("client is connected");
207
 
      
208
 
      // Test connections.
209
 
      assertEquals("abc", client.invoke("abc"));
210
 
      log.info("connection is good");
211
 
      
212
 
      // Get clientpool and ServerThread.
213
 
      SocketServerInvoker invoker = (SocketServerInvoker) connector.getServerInvoker();
214
 
      Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
215
 
      field.setAccessible(true);
216
 
      LRUPool clientpool = (LRUPool) field.get(invoker);
217
 
      assertEquals(1, clientpool.size());
218
 
      Set clientpoolContents =  clientpool.getContents();
219
 
      ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
220
 
      
221
 
      // Get threadpool.
222
 
      field = SocketServerInvoker.class.getDeclaredField("threadpool");
223
 
      field.setAccessible(true);
224
 
      LinkedList threadpool = (LinkedList) field.get(invoker);
225
 
      assertEquals(0, threadpool.size());
226
 
      
227
 
      // Wait for ServerThread to time out.
228
 
      Thread.sleep(8000);
229
 
      
230
 
      // Verify original ServerThread remains in clientpool.
231
 
      assertEquals(0, threadpool.size());
232
 
      assertEquals(1, clientpool.size());
233
 
      clientpoolContents =  clientpool.getContents();
234
 
      assertEquals(serverThread1, clientpoolContents.iterator().next());
235
 
      
236
 
      // Make another invocation and verify ServerThread was reused.
237
 
      client.invoke("xyz");
238
 
      assertEquals(1, clientpool.size());
239
 
      clientpoolContents =  clientpool.getContents();
240
 
      ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
241
 
      assertEquals(serverThread1, serverThread2);
242
 
      
243
 
      client.disconnect();
244
 
      shutdownServer();
245
 
      log.info(getName() + " PASSES");
246
 
   }
247
 
   
248
 
   
249
 
   protected String getTransport()
250
 
   {
251
 
      return "socket";
252
 
   }
253
 
   
254
 
   
255
 
   protected void addExtraClientConfig(Map config) {}
256
 
   protected void addExtraServerConfig(Map config) {}
257
 
   
258
 
 
259
 
   protected void setupServer(String serializationType) throws Exception
260
 
   {
261
 
      host = InetAddress.getLocalHost().getHostAddress();
262
 
      port = PortUtil.findFreePort(host);
263
 
      locatorURI = getTransport() + "://" + host + ":" + port + "/?timeout=4000";
264
 
      locatorURI += "&serializationtype=" + serializationType;
265
 
      serverLocator = new InvokerLocator(locatorURI);
266
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
267
 
      HashMap config = new HashMap();
268
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
269
 
      addExtraServerConfig(config);
270
 
      connector = new Connector(serverLocator, config);
271
 
      connector.create();
272
 
      invocationHandler = new TestInvocationHandler();
273
 
      connector.addInvocationHandler("test", invocationHandler);
274
 
      connector.start();
275
 
   }
276
 
   
277
 
   
278
 
   protected void shutdownServer() throws Exception
279
 
   {
280
 
      if (connector != null)
281
 
         connector.stop();
282
 
   }
283
 
   
284
 
   
285
 
   static class TestInvocationHandler implements ServerInvocationHandler
286
 
   {
287
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
288
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
289
 
      {
290
 
         return invocation.getParameter();
291
 
      }
292
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
293
 
      public void setMBeanServer(MBeanServer server) {}
294
 
      public void setInvoker(ServerInvoker invoker) {}
295
 
   }
296
 
   
297
 
   
298
 
   static class TestCallbackHandler implements InvokerCallbackHandler
299
 
   {
300
 
      public void handleCallback(Callback callback) throws HandleCallbackException
301
 
      {
302
 
         log.info("received callback");
303
 
      }  
304
 
   }
305
 
}
 
 
b'\\ No newline at end of file'