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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/bisocket/threadpool/ServerThreadReuseTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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.bisocket.threadpool;
23
 
 
24
 
import java.lang.reflect.Field;
25
 
import java.net.InetAddress;
26
 
import java.net.Socket;
27
 
import java.util.HashMap;
28
 
import java.util.HashSet;
29
 
import java.util.Iterator;
30
 
import java.util.List;
31
 
import java.util.Map;
32
 
import java.util.Set;
33
 
 
34
 
import javax.management.MBeanServer;
35
 
 
36
 
import junit.framework.TestCase;
37
 
 
38
 
import org.apache.log4j.ConsoleAppender;
39
 
import org.apache.log4j.Level;
40
 
import org.apache.log4j.Logger;
41
 
import org.apache.log4j.PatternLayout;
42
 
import org.jboss.logging.XLevel;
43
 
import org.jboss.remoting.Client;
44
 
import org.jboss.remoting.InvocationRequest;
45
 
import org.jboss.remoting.InvokerLocator;
46
 
import org.jboss.remoting.ServerInvocationHandler;
47
 
import org.jboss.remoting.ServerInvoker;
48
 
import org.jboss.remoting.callback.Callback;
49
 
import org.jboss.remoting.callback.HandleCallbackException;
50
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
51
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
52
 
import org.jboss.remoting.transport.Connector;
53
 
import org.jboss.remoting.transport.PortUtil;
54
 
import org.jboss.remoting.transport.bisocket.Bisocket;
55
 
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
56
 
import org.jboss.remoting.transport.socket.LRUPool;
57
 
import org.jboss.remoting.transport.socket.ServerThread;
58
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
59
 
 
60
 
 
61
 
/**
62
 
 * Unit test for JBREM-938.
63
 
 * 
64
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
65
 
 * @version $Revision: 1.1 $
66
 
 * <p>
67
 
 * Copyright Mar 22, 2008
68
 
 * </p>
69
 
 */
70
 
public class ServerThreadReuseTestCase extends TestCase
71
 
{
72
 
   private static Logger log = Logger.getLogger(ServerThreadReuseTestCase.class);
73
 
   
74
 
   private static boolean firstTime = true;
75
 
   
76
 
   protected String host;
77
 
   protected int port;
78
 
   protected String locatorURI;
79
 
   protected InvokerLocator serverLocator;
80
 
   protected Connector connector;
81
 
   protected TestInvocationHandler invocationHandler;
82
 
 
83
 
   
84
 
   public void setUp() throws Exception
85
 
   {
86
 
      if (firstTime)
87
 
      {
88
 
         firstTime = false;
89
 
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
90
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
91
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
92
 
         PatternLayout layout = new PatternLayout(pattern);
93
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
94
 
         Logger.getRootLogger().addAppender(consoleAppender);  
95
 
      }
96
 
   }
97
 
 
98
 
   
99
 
   public void tearDown()
100
 
   {
101
 
   }
102
 
   
103
 
   
104
 
   public void testServerThreadReuse() throws Throwable
105
 
   {
106
 
      log.info("entering " + getName());
107
 
      
108
 
      // Start server.
109
 
      setupServer();
110
 
      
111
 
      // Create client.
112
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
113
 
      HashMap clientConfig = new HashMap();
114
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
115
 
      addExtraClientConfig(clientConfig);
116
 
      Client client = new Client(clientLocator, clientConfig);
117
 
      client.connect();
118
 
      log.info("client is connected");
119
 
      
120
 
      // Test connections.
121
 
      assertEquals("abc", client.invoke("abc"));
122
 
      log.info("connection is good");
123
 
      
124
 
      // Add callback handler.
125
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
126
 
      HashMap metadata = new HashMap();
127
 
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
128
 
      client.addListener(callbackHandler, metadata, null, true);
129
 
      client.invoke("callback");
130
 
      assertEquals(1, callbackHandler.counter);
131
 
      log.info("first callback successful");
132
 
      
133
 
      // Get callback server thread pools.
134
 
      Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
135
 
      assertEquals(1, callbackConnectors.size());
136
 
      Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
137
 
      BisocketServerInvoker invoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
138
 
      Field field = SocketServerInvoker.class.getDeclaredField("threadpool");
139
 
      field.setAccessible(true);
140
 
      List threadpool = (List) field.get(invoker);
141
 
      field = SocketServerInvoker.class.getDeclaredField("clientpool");
142
 
      field.setAccessible(true);
143
 
      LRUPool clientpool = (LRUPool) field.get(invoker);
144
 
      assertEquals(0, threadpool.size());
145
 
      assertEquals(1, clientpool.size());
146
 
      
147
 
      // Kill worker thread's socket.
148
 
      Set clientpoolContents = clientpool.getContents();
149
 
      ServerThread serverThread1 = (ServerThread) clientpoolContents.iterator().next();
150
 
      field = ServerThread.class.getDeclaredField("socket");
151
 
      field.setAccessible(true);
152
 
      Socket socket = (Socket) field.get(serverThread1);
153
 
      socket.close();
154
 
      Thread.sleep(4000);
155
 
      assertEquals(1, threadpool.size());
156
 
      assertEquals(0, clientpool.size());
157
 
      
158
 
      // Make second callback and verify that worker thread gets reused.
159
 
      client.invoke("callback");
160
 
      assertEquals(2, callbackHandler.counter);
161
 
      log.info("second callback successful");
162
 
      assertEquals(0, threadpool.size());
163
 
      assertEquals(1, clientpool.size());
164
 
      clientpoolContents = clientpool.getContents();
165
 
      ServerThread serverThread2 = (ServerThread) clientpoolContents.iterator().next();
166
 
      assertEquals(serverThread2, serverThread1);
167
 
      log.info("ServerThread was reused");
168
 
      
169
 
      client.removeListener(callbackHandler);
170
 
      client.disconnect();
171
 
      shutdownServer();
172
 
      log.info(getName() + " PASSES");
173
 
   }
174
 
   
175
 
   
176
 
   protected String getTransport()
177
 
   {
178
 
      return "bisocket";
179
 
   }
180
 
   
181
 
   
182
 
   protected void addExtraClientConfig(Map config) {}
183
 
   protected void addExtraServerConfig(Map config) {}
184
 
   
185
 
 
186
 
   protected void setupServer() throws Exception
187
 
   {
188
 
      host = InetAddress.getLocalHost().getHostAddress();
189
 
      port = PortUtil.findFreePort(host);
190
 
      locatorURI = getTransport() + "://" + host + ":" + port; 
191
 
      serverLocator = new InvokerLocator(locatorURI);
192
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
193
 
      HashMap config = new HashMap();
194
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
195
 
      addExtraServerConfig(config);
196
 
      connector = new Connector(serverLocator, config);
197
 
      connector.create();
198
 
      invocationHandler = new TestInvocationHandler();
199
 
      connector.addInvocationHandler("test", invocationHandler);
200
 
      connector.start();
201
 
   }
202
 
   
203
 
   
204
 
   protected void shutdownServer() throws Exception
205
 
   {
206
 
      if (connector != null)
207
 
         connector.stop();
208
 
   }
209
 
   
210
 
   
211
 
   static class TestInvocationHandler implements ServerInvocationHandler
212
 
   {
213
 
      HashSet callbackHandlers = new HashSet();
214
 
      
215
 
      public void addListener(InvokerCallbackHandler callbackHandler)
216
 
      {
217
 
         callbackHandlers.add(callbackHandler);
218
 
      }
219
 
      
220
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
221
 
      {
222
 
         String command = (String) invocation.getParameter();
223
 
         
224
 
         if ("callback".equals(command))
225
 
         {
226
 
            Callback callback = new Callback("callback");
227
 
            Iterator it = callbackHandlers.iterator();
228
 
            while (it.hasNext())
229
 
            {
230
 
               ServerInvokerCallbackHandler handler;
231
 
               handler = (ServerInvokerCallbackHandler) it.next();
232
 
               handler.handleCallback(callback);
233
 
            }
234
 
         }
235
 
         
236
 
         return command;
237
 
      }
238
 
      
239
 
      public void removeListener(InvokerCallbackHandler callbackHandler)
240
 
      {
241
 
         callbackHandlers.remove(callbackHandler);
242
 
      }
243
 
      
244
 
      
245
 
      public void setMBeanServer(MBeanServer server) {}
246
 
      public void setInvoker(ServerInvoker invoker) {}
247
 
   }
248
 
   
249
 
   
250
 
   static class TestCallbackHandler implements InvokerCallbackHandler
251
 
   {
252
 
      public int counter;
253
 
      
254
 
      public void handleCallback(Callback callback) throws HandleCallbackException
255
 
      {
256
 
         counter++;
257
 
         log.info("received callback");
258
 
      }  
259
 
   }
260
 
}
 
 
b'\\ No newline at end of file'