garydgregory commented on code in PR #367:
URL: https://github.com/apache/commons-vfs/pull/367#discussion_r1070091466


##########
commons-vfs2/src/test/java/org/apache/commons/vfs2/impl/VfsClassLoaderTests.java:
##########
@@ -203,6 +214,84 @@ public void testSealing() throws Exception {
         verifyPackage(pack, true);
     }
 
+    @Test
+    public void testThreadSafety() throws Exception {
+        final int THREADS = 20;
+        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(THREADS * 
2);
+        List<Throwable> exceptions = new ArrayList<>();
+        Thread.UncaughtExceptionHandler handler = new 
Thread.UncaughtExceptionHandler() {
+            @Override
+            public void uncaughtException(Thread t, Throwable e) {
+                synchronized(exceptions) {
+                    exceptions.add(e);
+                }
+            }
+        };
+        ThreadFactory factory = new 
ThreadFactoryBuilder().setUncaughtExceptionHandler(handler).build();
+        Queue<Runnable> rejections = new LinkedList<>();
+        RejectedExecutionHandler rejectionHandler = new 
RejectedExecutionHandler() {
+            @Override
+            public void rejectedExecution(Runnable r, ThreadPoolExecutor 
executor) {
+                synchronized(rejections) {
+                    rejections.add(r);
+                }
+            }
+        };
+        ThreadPoolExecutor executor = new ThreadPoolExecutor(THREADS, THREADS, 
0, TimeUnit.SECONDS, workQueue, factory, rejectionHandler);
+        executor.prestartAllCoreThreads();
+        for (int i = 0; i < THREADS; i++) {
+            VFSClassLoader loader = createClassLoader();
+            workQueue.put(new VfsClassLoaderTests.LoadClass(loader));
+        }
+        while (!workQueue.isEmpty()) {
+            Thread.sleep(10);
+        }
+        while (!rejections.isEmpty() && executor.getActiveCount() > 0) {
+            List<Runnable> rejected = new ArrayList<>();
+            synchronized(rejections) {
+                rejected.addAll(rejections);
+                rejections.clear();
+            }
+            workQueue.addAll(rejected);
+        }
+        executor.shutdown();
+        executor.awaitTermination(30, TimeUnit.SECONDS);
+        assertEquals(THREADS, executor.getCompletedTaskCount());
+        if (!exceptions.isEmpty()) {
+            for (Throwable t : exceptions) {
+                System.out.println(t.toString());
+                t.printStackTrace(System.out);
+            }
+        }
+        assertTrue(exceptions.size() + " threads failed", 
exceptions.isEmpty());
+    }
+
+    private class LoadClass implements Runnable {
+        private final VFSClassLoader loader;
+        public LoadClass(VFSClassLoader loader) {
+            this.loader = loader;
+        }
+
+        @Override
+        public void run() {
+            try {
+                final Class<?> testClass = 
loader.findClass("code.ClassToLoad");
+                final Package pack = testClass.getPackage();
+                assertEquals("code", pack.getName());
+                verifyPackage(pack, false);
+
+                final Object testObject = testClass.newInstance();
+                assertEquals("**PRIVATE**", testObject.toString());
+            } catch (ClassNotFoundException e) {

Review Comment:
   Use muilt-catch clause and Assert.fail()



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to