diff --git a/src/main/java/org/burningwave/core/classes/ClassPathScanner.java b/src/main/java/org/burningwave/core/classes/ClassPathScanner.java index 7193c53c4..25f113196 100644 --- a/src/main/java/org/burningwave/core/classes/ClassPathScanner.java +++ b/src/main/java/org/burningwave/core/classes/ClassPathScanner.java @@ -245,11 +245,9 @@ public R findBy(SearchConfig input) { if (!currentScannedPath.isContainer()) { throw new IllegalArgumentException(Strings.compile("{} is not a folder or archive", currentScannedPath.getAbsolutePath())); } + Collection itemsFound = scanAndAddToPathScannerClassLoader(context, currentScannedPath); outputHandler.accept(output -> { - output.put( - currentScannedPath, - scanAndAddToPathScannerClassLoader(context, currentScannedPath) - ); + output.put(currentScannedPath, itemsFound); }); } ).parallelIf( diff --git a/src/test/java/org/burningwave/core/ClassHunterTest.java b/src/test/java/org/burningwave/core/ClassHunterTest.java index ebf500a99..49b8339e3 100644 --- a/src/test/java/org/burningwave/core/ClassHunterTest.java +++ b/src/test/java/org/burningwave/core/ClassHunterTest.java @@ -1,6 +1,8 @@ package org.burningwave.core; import static org.burningwave.core.assembler.StaticComponentContainer.Cache; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.io.Closeable; import java.io.Serializable; @@ -11,6 +13,11 @@ import java.util.Collection; import java.util.Date; import java.util.Iterator; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.burningwave.core.assembler.ComponentSupplier; import org.burningwave.core.bean.Complex; @@ -52,6 +59,39 @@ public void findAllTestOne() throws Exception { ); } + @Test + public void findAllInParallelTestOne() throws Exception { + assumeTrue(Runtime.getRuntime().availableProcessors() > 1); + ComponentSupplier componentSupplier = getComponentSupplier(); + PathHelper pathHelper = componentSupplier.getPathHelper(); + Set pathsInScanning = ConcurrentHashMap.newKeySet(); + CountDownLatch allPathsInScanning = new CountDownLatch(2); + Collection scanningOverlaps = new CopyOnWriteArrayList<>(); + testNotEmpty( + () -> componentSupplier.getClassHunter().findBy( + SearchConfig.forPaths( + pathHelper.getAbsolutePathOfResource("../../src/test/external-resources/commons-lang"), + pathHelper.getAbsolutePathOfResource("../../src/test/external-resources/spring-core-4.3.4.RELEASE.jar") + ).setMinimumCollectionSizeForParallelIteration(2).addFileFilter( + FileSystemItem.Criteria.forAllFileThat((child, scannedPath) -> { + if (pathsInScanning.add(scannedPath.getAbsolutePath())) { + allPathsInScanning.countDown(); + try { + scanningOverlaps.add(allPathsInScanning.await(30, TimeUnit.SECONDS)); + } catch (InterruptedException exc) { + scanningOverlaps.add(Boolean.FALSE); + } + } + return true; + }) + ) + ), + (result) -> + result.getClasses() + ); + assertEquals(Arrays.asList(Boolean.TRUE, Boolean.TRUE), new ArrayList<>(scanningOverlaps)); + } + @Test public void findAllTestTwo() throws Exception { ComponentSupplier componentSupplier = getComponentSupplier();