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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/classloader/race/ClassloaderRaceTestParent.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.classloader.race;
 
23
 
 
24
import java.lang.ref.WeakReference;
 
25
import java.net.InetAddress;
 
26
import java.net.URL;
 
27
import java.net.URLClassLoader;
 
28
import java.util.HashMap;
 
29
import java.util.Map;
 
30
import java.util.WeakHashMap;
 
31
 
 
32
import javax.management.MBeanServer;
 
33
 
 
34
import junit.framework.TestCase;
 
35
 
 
36
import org.apache.log4j.ConsoleAppender;
 
37
import org.apache.log4j.Level;
 
38
import org.apache.log4j.Logger;
 
39
import org.apache.log4j.PatternLayout;
 
40
import org.jboss.logging.XLevel;
 
41
import org.jboss.remoting.Client;
 
42
import org.jboss.remoting.InvocationRequest;
 
43
import org.jboss.remoting.InvokerLocator;
 
44
import org.jboss.remoting.ServerInvocationHandler;
 
45
import org.jboss.remoting.ServerInvoker;
 
46
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
47
import org.jboss.remoting.transport.Connector;
 
48
import org.jboss.remoting.transport.PortUtil;
 
49
 
 
50
 
 
51
/**
 
52
 * 
 
53
 * Unit tests for JBREM-900.
 
54
 * 
 
55
 * Note: The class org.jboss.test.remoting.classloader.race.TestObject mentioned
 
56
 * in the tests below is found in 
 
57
 * 
 
58
 *  <remoting home>/src/etc/org/jboss/test/remoting/classloader/race.
 
59
 *    
 
60
 * It is loaded from
 
61
 * 
 
62
 *  <remoting home>/output/tests/classes/org/jboss/test/remoting/classloader/race/test.jar.
 
63
 * 
 
64
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
65
 * @version $Revision: 1.1 $
 
66
 * <p>
 
67
 * Copyright Feb 19, 2008
 
68
 * </p>
 
69
 */
 
70
public abstract class ClassloaderRaceTestParent extends TestCase
 
71
{
 
72
   private static Logger log = Logger.getLogger(ClassloaderRaceTestParent.class);
 
73
   
 
74
   private static boolean firstTime = true;
 
75
   protected static String metadata;
 
76
   
 
77
   protected String host;
 
78
   protected int port;
 
79
   protected String locatorURI;
 
80
   protected InvokerLocator serverLocator;
 
81
   protected Connector connector;
 
82
   protected TestInvocationHandler invocationHandler;
 
83
 
 
84
   
 
85
   public void setUp() throws Exception
 
86
   {
 
87
      if (firstTime)
 
88
      {
 
89
         firstTime = false;
 
90
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
91
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
92
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
93
         PatternLayout layout = new PatternLayout(pattern);
 
94
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
95
         Logger.getRootLogger().addAppender(consoleAppender);
 
96
         metadata = System.getProperty("remoting.metadata", "serializationtype=java");
 
97
      }
 
98
   }
 
99
 
 
100
   
 
101
   public void tearDown()
 
102
   {
 
103
   }
 
104
   
 
105
   public void testDirectClassloading() throws Throwable
 
106
   {
 
107
      log.info("entering " + getName());
 
108
 
 
109
      URL url = ClassloaderRaceTestParent.class.getResource("test.jar");
 
110
      ClassLoader cl1 = new TestClassLoader(new URL[]{url});
 
111
      Class c1 = cl1.loadClass("org.jboss.test.remoting.classloader.race.TestObject");
 
112
      Object testObject1 = c1.newInstance();
 
113
      log.info("classloader1: " + testObject1.getClass().getClassLoader());
 
114
 
 
115
      ClassLoader cl2 = new TestClassLoader(new URL[]{url});
 
116
      Class c2 = cl2.loadClass("org.jboss.test.remoting.classloader.race.TestObject");
 
117
      Object testObject2 = c2.newInstance();
 
118
      log.info("classloader2: " + testObject2.getClass().getClassLoader());
 
119
 
 
120
      assertFalse(testObject1.getClass().isAssignableFrom(testObject2.getClass()));            
 
121
      log.info(getName() + " PASSES");
 
122
   }
 
123
   
 
124
   
 
125
   public void testSequential() throws Throwable
 
126
   {
 
127
      log.info("entering " + getName());
 
128
      
 
129
      // Start server.
 
130
      setupServer();
 
131
      
 
132
      // Create client.
 
133
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
134
      HashMap clientConfig = new HashMap();
 
135
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
136
      addExtraClientConfig(clientConfig);
 
137
      Client client = new Client(clientLocator, clientConfig);
 
138
      client.connect();
 
139
      log.info("client is connected to: " + locatorURI);
 
140
      
 
141
      SimpleInvocationThread t1 = new SimpleInvocationThread(client, "InvocationThread:1");
 
142
      t1.start();
 
143
      t1.join();
 
144
      SimpleInvocationThread t2 = new SimpleInvocationThread(client, "InvocationThread:2");
 
145
      t2.start();
 
146
      t2.join();
 
147
      
 
148
      assertEquals(t1.getContextClassLoader(), t1.getResponseClassLoader());
 
149
      assertEquals(t2.getContextClassLoader(), t2.getResponseClassLoader());
 
150
      assertNotSame(t1.getResponseClassLoader(), t2.getResponseClassLoader());
 
151
      
 
152
      client.disconnect();
 
153
      shutdownServer();
 
154
      log.info(getName() + " PASSES");
 
155
   }
 
156
   
 
157
   
 
158
   public void testSimultaneous() throws Throwable
 
159
   {
 
160
      log.info("entering " + getName());
 
161
      
 
162
      // Start server.
 
163
      setupServer();
 
164
      
 
165
      // Create client.
 
166
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
167
      HashMap clientConfig = new HashMap();
 
168
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
169
      addExtraClientConfig(clientConfig);
 
170
      Client client = new Client(clientLocator, clientConfig);
 
171
      client.connect();
 
172
      log.info("client is connected to: " + locatorURI);
 
173
      
 
174
      int COUNT = 100;
 
175
      log.info("COUNT: " + COUNT);
 
176
      SynchronizedInvocationThread[] threads = new SynchronizedInvocationThread[COUNT];
 
177
      Object lock = new Object();
 
178
      URL url = ClassloaderRaceTestParent.class.getResource("test.jar");
 
179
      ClassLoader cl1 = new TestClassLoader(new URL[]{url});
 
180
      ClassLoader cl2 = new TestClassLoader(new URL[]{url});
 
181
      log.info("classloader1: " + cl1);
 
182
      log.info("classloader2: " + cl2);
 
183
      
 
184
      // Create threads that use cl1.
 
185
      for (int i = 0; i < COUNT / 2; i++)
 
186
      {
 
187
         threads[i] = new SynchronizedInvocationThread(client, cl1, i, lock);
 
188
         threads[i].start();
 
189
      }
 
190
      
 
191
      // Create threads that use cl2.
 
192
      for (int i = COUNT/2; i < COUNT; i++)
 
193
      {
 
194
         threads[i] = new SynchronizedInvocationThread(client, cl2, i, lock);
 
195
         threads[i].start();
 
196
      }
 
197
      
 
198
      // Start threads.
 
199
      Thread.sleep(2000);
 
200
      synchronized (lock)
 
201
      {
 
202
         lock.notifyAll();
 
203
      }
 
204
      
 
205
      // Wait for all threads to complete.
 
206
      for (int i = 0; i < COUNT; i++)
 
207
      {
 
208
         threads[i].join();
 
209
      }
 
210
      
 
211
      // Checks threads that use cl1.
 
212
      for (int i = 0; i < COUNT / 2; i++)
 
213
      {
 
214
         assertEquals("error in thread " + i, cl1, threads[i].responseClassLoader);     
 
215
      }
 
216
      
 
217
      // Check threads that use cl2.
 
218
      for (int i = COUNT / 2; i < COUNT; i++)
 
219
      {
 
220
         assertEquals("error in thread " + i, cl2, threads[i].responseClassLoader);     
 
221
      }
 
222
      
 
223
      client.disconnect();
 
224
      shutdownServer();
 
225
      log.info(getName() + " PASSES");
 
226
   }
 
227
 
 
228
   
 
229
   protected abstract String getTransport();
 
230
   
 
231
   
 
232
   protected void addExtraClientConfig(Map config) {}
 
233
   protected void addExtraServerConfig(Map config) {}
 
234
   
 
235
 
 
236
   protected void setupServer() throws Exception
 
237
   {
 
238
      host = InetAddress.getLocalHost().getHostAddress();
 
239
      port = PortUtil.findFreePort(host);
 
240
      locatorURI = getTransport() + "://" + host + ":" + port;
 
241
      locatorURI += "/?" + metadata;
 
242
      serverLocator = new InvokerLocator(locatorURI);
 
243
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
244
      HashMap config = new HashMap();
 
245
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
246
      addExtraServerConfig(config);
 
247
      connector = new Connector(serverLocator, config);
 
248
      connector.create();
 
249
      invocationHandler = new TestInvocationHandler();
 
250
      connector.addInvocationHandler("test", invocationHandler);
 
251
      connector.start();
 
252
   }
 
253
   
 
254
   
 
255
   protected void shutdownServer() throws Exception
 
256
   {
 
257
      if (connector != null)
 
258
         connector.stop();
 
259
   }
 
260
   
 
261
   
 
262
   static class TestInvocationHandler implements ServerInvocationHandler
 
263
   {
 
264
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
265
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
266
      {
 
267
         URL url = ClassloaderRaceTestParent.class.getResource("test.jar");
 
268
         ClassLoader cl1 = new TestClassLoader(new URL[]{url});
 
269
         Class c = cl1.loadClass("org.jboss.test.remoting.classloader.race.TestObject");
 
270
         Object o = c.newInstance(); 
 
271
         return o;
 
272
      }
 
273
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
274
      public void setMBeanServer(MBeanServer server) {}
 
275
      public void setInvoker(ServerInvoker invoker) {}
 
276
   }
 
277
   
 
278
   
 
279
   static class TestClassLoader extends URLClassLoader
 
280
   {
 
281
      public TestClassLoader(URL[] urls)
 
282
      {
 
283
         super(urls, null);
 
284
      }
 
285
 
 
286
      public Class findClass(String fqn) throws ClassNotFoundException
 
287
      {
 
288
         log.debug(this + " loading class: " + fqn);
 
289
         Class c = super.findClass(fqn);
 
290
         log.debug(this + " loaded class: " + fqn);
 
291
         return c;
 
292
      }
 
293
   }
 
294
   
 
295
   
 
296
   static class SimpleInvocationThread extends Thread
 
297
   {
 
298
      Client client;
 
299
      String name;
 
300
      ClassLoader contextClassLoader;
 
301
      ClassLoader responseClassLoader;
 
302
      
 
303
      public SimpleInvocationThread(Client client, String name)
 
304
      {
 
305
         this.client = client;
 
306
         this.name = name;
 
307
      }
 
308
 
 
309
      public void run()
 
310
      {
 
311
         try
 
312
         {
 
313
            URL url = getClass().getResource("test.jar");
 
314
            contextClassLoader = new TestClassLoader(new URL[]{url});
 
315
            Thread.currentThread().setContextClassLoader(contextClassLoader);
 
316
            log.info(this + " context classloader: " + contextClassLoader);
 
317
            Object response = client.invoke(name);
 
318
            responseClassLoader = response.getClass().getClassLoader();
 
319
            log.info(this + " response classloader: " + responseClassLoader);
 
320
         }
 
321
         catch (Throwable t)
 
322
         {
 
323
            log.error("unable to complete invocation", t);
 
324
         }
 
325
      }
 
326
      
 
327
      public ClassLoader getContextClassLoader()
 
328
      {
 
329
         return contextClassLoader;
 
330
      }
 
331
      
 
332
      public ClassLoader getResponseClassLoader()
 
333
      {
 
334
         return responseClassLoader;
 
335
      }
 
336
      
 
337
      public String toString()
 
338
      {
 
339
         return name;
 
340
      }
 
341
   }
 
342
   
 
343
   
 
344
   static class SynchronizedInvocationThread extends Thread
 
345
   {
 
346
      Client client;
 
347
      String name;
 
348
      ClassLoader contextClassLoader;
 
349
      ClassLoader responseClassLoader;
 
350
      Object lock;
 
351
      
 
352
      public SynchronizedInvocationThread(Client client, ClassLoader classLoader,
 
353
                                          int id, Object lock)
 
354
      {
 
355
         this.client = client;
 
356
         this.contextClassLoader = classLoader;
 
357
         this.name = "SynchronizedInvocationThread:" + id;
 
358
         this.lock = lock;
 
359
      }
 
360
 
 
361
      public void run()
 
362
      {
 
363
         try
 
364
         {
 
365
            Thread.currentThread().setContextClassLoader(contextClassLoader);
 
366
            
 
367
            synchronized (lock)
 
368
            {
 
369
               log.debug(this + " waiting");
 
370
               lock.wait();
 
371
            }
 
372
            
 
373
            log.debug(this + " making invocation");
 
374
            Object response = client.invoke(name);
 
375
            responseClassLoader = response.getClass().getClassLoader();
 
376
            log.debug(this + " done");
 
377
         }
 
378
         catch (Throwable t)
 
379
         {
 
380
            log.error(this + " unable to complete invocation", t);
 
381
         }
 
382
      }
 
383
      
 
384
      public String toString()
 
385
      {
 
386
         return name;
 
387
      }
 
388
   }
 
389
   
 
390
   
 
391
   static class Counter
 
392
   {
 
393
      public int count;
 
394
   }
 
395
}
 
 
b'\\ No newline at end of file'