-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kie-issue#1738: customize ForkJoinPool to explicitly inherit the cont…
…ext ClassLoader from the parent thread
- Loading branch information
Showing
2 changed files
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...rc/test/java/org/drools/compiler/builder/impl/KnowledgeBuilderImplCompilerFJPoolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.drools.compiler.builder.impl; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.RecursiveAction; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
||
public class KnowledgeBuilderImplCompilerFJPoolTest { | ||
|
||
private ClassLoader originalClassLoader; | ||
private ClassLoader customClassLoader; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
// Save the original classloader | ||
originalClassLoader = Thread.currentThread().getContextClassLoader(); | ||
|
||
// Create a custom classloader for testing | ||
customClassLoader = new ClassLoader(ClassLoader.getSystemClassLoader()) {}; | ||
Thread.currentThread().setContextClassLoader(customClassLoader); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
// Restore original classloader | ||
Thread.currentThread().setContextClassLoader(originalClassLoader); | ||
} | ||
|
||
private static class ClassLoaderCheckTask extends RecursiveAction { | ||
private final List<ClassLoader> results; | ||
private final int index; | ||
|
||
public ClassLoaderCheckTask(List<ClassLoader> results, int index) { | ||
this.results = results; | ||
this.index = index; | ||
} | ||
|
||
@Override | ||
protected void compute() { | ||
results.set(index, Thread.currentThread().getContextClassLoader()); | ||
} | ||
} | ||
|
||
@Test | ||
void testCompilerPoolClassLoader() throws Exception { | ||
int numTasks = 5; | ||
List<ClassLoader> results = new ArrayList<>(numTasks); | ||
for (int i = 0; i < numTasks; i++) { | ||
results.add(null); | ||
} | ||
|
||
// Submit tasks to the COMPILER_POOL | ||
for (int i = 0; i < numTasks; i++) { | ||
KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(new ClassLoaderCheckTask(results, i)); | ||
} | ||
|
||
// Wait for completion | ||
KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.awaitQuiescence(1, TimeUnit.SECONDS); | ||
|
||
// Verify all worker threads have the expected classloader | ||
for (ClassLoader workerClassLoader : results) { | ||
assertSame( | ||
customClassLoader, | ||
workerClassLoader, | ||
"Worker thread should have inherited the custom classloader" | ||
); | ||
} | ||
} | ||
} |