-
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 (#6211) * kie-issue#1738: customize ForkJoinPool to explicitly inherit the context ClassLoader from the parent thread * kie-issue#1738: ops, forgot the header :)
- Loading branch information
Showing
2 changed files
with
97 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
93 changes: 93 additions & 0 deletions
93
...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,93 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
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" | ||
); | ||
} | ||
} | ||
} |