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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/leak/ServerInvokerCallbackHandlerLeakTestCase.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 2009, Red Hat Middleware LLC, and individual contributors
4
 
 * as indicated by the @author tags. See the copyright.txt file in the
5
 
 * distribution for a 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
 
 
23
 
package org.jboss.test.remoting.callback.leak;
24
 
 
25
 
import java.lang.reflect.Field;
26
 
import java.net.InetAddress;
27
 
import java.util.HashMap;
28
 
import java.util.HashSet;
29
 
import java.util.Iterator;
30
 
import java.util.Map;
31
 
import java.util.Set;
32
 
import java.util.TimerTask;
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.remoting.Client;
43
 
import org.jboss.remoting.ConnectionListener;
44
 
import org.jboss.remoting.ConnectionNotifier;
45
 
import org.jboss.remoting.InvocationRequest;
46
 
import org.jboss.remoting.InvokerLocator;
47
 
import org.jboss.remoting.LeasePinger;
48
 
import org.jboss.remoting.MicroRemoteClientInvoker;
49
 
import org.jboss.remoting.ServerInvocationHandler;
50
 
import org.jboss.remoting.ServerInvoker;
51
 
import org.jboss.remoting.callback.Callback;
52
 
import org.jboss.remoting.callback.HandleCallbackException;
53
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
54
 
import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
55
 
import org.jboss.remoting.transport.Connector;
56
 
import org.jboss.remoting.transport.PortUtil;
57
 
 
58
 
 
59
 
/**
60
 
 * Unit tests for JBREM-1113.
61
 
 * 
62
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
63
 
 * @version $Rev$
64
 
 * <p>
65
 
 * Copyright Apr 13, 2009
66
 
 * </p>
67
 
 */
68
 
public class ServerInvokerCallbackHandlerLeakTestCase extends TestCase
69
 
{
70
 
   private static Logger log = Logger.getLogger(ServerInvokerCallbackHandlerLeakTestCase.class);
71
 
   
72
 
   private static boolean firstTime = true;
73
 
   private static int COUNT = 10;
74
 
   private static Object lock = new Object();
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(Level.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
 
      TestConnectionListener.count = 0;
97
 
   }
98
 
 
99
 
   
100
 
   public void tearDown()
101
 
   {
102
 
   }
103
 
   
104
 
   
105
 
   public void testLeakWithCallbackHandlersListening() throws Throwable
106
 
   {
107
 
      doLeakTest(true);
108
 
   }
109
 
   
110
 
   
111
 
   public void testLeakWithoutCallbackHandlersListening() throws Throwable
112
 
   {
113
 
      doLeakTest(false);
114
 
   }
115
 
   
116
 
   
117
 
   public void doLeakTest(boolean registerCallbackListener) throws Throwable
118
 
   {
119
 
      log.info("entering " + getName());
120
 
      setupServer(registerCallbackListener);
121
 
      
122
 
      // Get fields.
123
 
      ServerInvoker serverInvoker = connector.getServerInvoker();
124
 
      Field field = ServerInvoker.class.getDeclaredField("connectionNotifier");
125
 
      field.setAccessible(true);
126
 
      ConnectionNotifier connectionNotifier = (ConnectionNotifier) field.get(serverInvoker);
127
 
      field = ServerInvoker.class.getDeclaredField("callbackHandlers");
128
 
      field.setAccessible(true);
129
 
      Map callbackHandlers = (Map) field.get(serverInvoker);
130
 
      
131
 
      // Create client.
132
 
      HashMap clientConfig = new HashMap();
133
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
134
 
      addExtraClientConfig(clientConfig);
135
 
      Client client = null;
136
 
      
137
 
      for (int i = 0; i < COUNT; i++)
138
 
      {
139
 
         client = new Client(serverLocator, 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
 
         TestCallbackHandler callbackHandler = new TestCallbackHandler();
148
 
         client.addListener(callbackHandler, null, null, true);
149
 
      }
150
 
      
151
 
      field = MicroRemoteClientInvoker.class.getDeclaredField("leasePinger");
152
 
      field.setAccessible(true);
153
 
      LeasePinger pinger = (LeasePinger) field.get(client.getInvoker());
154
 
      field = LeasePinger.class.getDeclaredField("timerTask");
155
 
      field.setAccessible(true);
156
 
      TimerTask timerTask = (TimerTask) field.get(pinger);
157
 
      timerTask.cancel();
158
 
      
159
 
      synchronized(lock)
160
 
      {
161
 
         lock.wait();
162
 
      }
163
 
      Thread.sleep(2000);
164
 
      
165
 
      assertEquals(COUNT, TestConnectionListener.count);
166
 
      assertEquals(1, connectionNotifier.size());
167
 
      assertTrue(callbackHandlers.isEmpty());
168
 
      
169
 
      log.info(getName() + " PASSES");
170
 
   }
171
 
   
172
 
   
173
 
   protected String getTransport()
174
 
   {
175
 
      return "socket";
176
 
   }
177
 
   
178
 
   
179
 
   protected void addExtraClientConfig(Map config) {}
180
 
   protected void addExtraServerConfig(Map config) {}
181
 
   
182
 
 
183
 
   protected void setupServer(boolean registerCallbackListener) throws Exception
184
 
   {
185
 
      host = InetAddress.getLocalHost().getHostAddress();
186
 
      port = PortUtil.findFreePort(host);
187
 
      locatorURI = getTransport() + "://" + host + ":" + port;
188
 
      locatorURI += "/?leasing=true";
189
 
      if (registerCallbackListener)
190
 
      {
191
 
         locatorURI += "&" + ServerInvoker.REGISTER_CALLBACK_LISTENER + "=true";
192
 
      }
193
 
      else
194
 
      {
195
 
         locatorURI += "&" + ServerInvoker.REGISTER_CALLBACK_LISTENER + "=false";
196
 
      }
197
 
      serverLocator = new InvokerLocator(locatorURI);
198
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
199
 
      HashMap config = new HashMap();
200
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
201
 
      addExtraServerConfig(config);
202
 
      connector = new Connector(serverLocator, config);
203
 
      connector.create();
204
 
      invocationHandler = new TestInvocationHandler();
205
 
      connector.addInvocationHandler("test", invocationHandler);
206
 
      connector.start();
207
 
      connector.setLeasePeriod(2000);
208
 
      TestConnectionListener listener = new TestConnectionListener(!registerCallbackListener);
209
 
      connector.addConnectionListener(listener);
210
 
   }
211
 
   
212
 
   
213
 
   protected void shutdownServer() throws Exception
214
 
   {
215
 
      if (connector != null)
216
 
         connector.stop();
217
 
   }
218
 
   
219
 
   
220
 
   static class TestInvocationHandler implements ServerInvocationHandler
221
 
   {
222
 
      static public Set callbackHandlers = new HashSet();
223
 
      public void addListener(InvokerCallbackHandler callbackHandler)
224
 
      {
225
 
         callbackHandlers.add(callbackHandler);
226
 
      }
227
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
228
 
      {
229
 
         return invocation.getParameter();
230
 
      }
231
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
232
 
      public void setMBeanServer(MBeanServer server) {}
233
 
      public void setInvoker(ServerInvoker invoker) {}
234
 
   }
235
 
   
236
 
   
237
 
   static class TestCallbackHandler implements InvokerCallbackHandler
238
 
   {
239
 
      public void handleCallback(Callback callback) throws HandleCallbackException
240
 
      {
241
 
         log.info("received callback");
242
 
      }  
243
 
   }
244
 
   
245
 
   
246
 
   static class TestConnectionListener implements ConnectionListener
247
 
   {
248
 
      static public int count;
249
 
      boolean shutdownCallbackHandlers;
250
 
      
251
 
      public TestConnectionListener(boolean shutdownCallbackHandlers)
252
 
      {
253
 
         this.shutdownCallbackHandlers = shutdownCallbackHandlers;
254
 
      }
255
 
 
256
 
      public synchronized void handleConnectionException(Throwable throwable, Client client)
257
 
      {
258
 
         log.info("got connection exception");
259
 
         if(++count == COUNT)
260
 
         {
261
 
            if (shutdownCallbackHandlers)
262
 
            {
263
 
               Iterator it = TestInvocationHandler.callbackHandlers.iterator();
264
 
               while (it.hasNext())
265
 
               {
266
 
                  ServerInvokerCallbackHandler callbackHandler = (ServerInvokerCallbackHandler) it.next();
267
 
                  callbackHandler.shutdown();
268
 
                  log.info("shut down: " + callbackHandler);
269
 
               }
270
 
               TestInvocationHandler.callbackHandlers.clear();
271
 
            }
272
 
            synchronized(lock)
273
 
            {
274
 
               lock.notify();
275
 
            }
276
 
         }
277
 
      }  
278
 
   }
279
 
}
 
 
b'\\ No newline at end of file'