|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.drools.compiler.builder.impl; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.AfterEach; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.RecursiveAction; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 31 | + |
| 32 | +public class KnowledgeBuilderImplCompilerFJPoolTest { |
| 33 | + |
| 34 | + private ClassLoader originalClassLoader; |
| 35 | + private ClassLoader customClassLoader; |
| 36 | + |
| 37 | + @BeforeEach |
| 38 | + void setUp() { |
| 39 | + // Save the original classloader |
| 40 | + originalClassLoader = Thread.currentThread().getContextClassLoader(); |
| 41 | + |
| 42 | + // Create a custom classloader for testing |
| 43 | + customClassLoader = new ClassLoader(ClassLoader.getSystemClassLoader()) {}; |
| 44 | + Thread.currentThread().setContextClassLoader(customClassLoader); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + void tearDown() { |
| 49 | + // Restore original classloader |
| 50 | + Thread.currentThread().setContextClassLoader(originalClassLoader); |
| 51 | + } |
| 52 | + |
| 53 | + private static class ClassLoaderCheckTask extends RecursiveAction { |
| 54 | + private final List<ClassLoader> results; |
| 55 | + private final int index; |
| 56 | + |
| 57 | + public ClassLoaderCheckTask(List<ClassLoader> results, int index) { |
| 58 | + this.results = results; |
| 59 | + this.index = index; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void compute() { |
| 64 | + results.set(index, Thread.currentThread().getContextClassLoader()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testCompilerPoolClassLoader() throws Exception { |
| 70 | + int numTasks = 5; |
| 71 | + List<ClassLoader> results = new ArrayList<>(numTasks); |
| 72 | + for (int i = 0; i < numTasks; i++) { |
| 73 | + results.add(null); |
| 74 | + } |
| 75 | + |
| 76 | + // Submit tasks to the COMPILER_POOL |
| 77 | + for (int i = 0; i < numTasks; i++) { |
| 78 | + KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.submit(new ClassLoaderCheckTask(results, i)); |
| 79 | + } |
| 80 | + |
| 81 | + // Wait for completion |
| 82 | + KnowledgeBuilderImpl.ForkJoinPoolHolder.COMPILER_POOL.awaitQuiescence(1, TimeUnit.SECONDS); |
| 83 | + |
| 84 | + // Verify all worker threads have the expected classloader |
| 85 | + for (ClassLoader workerClassLoader : results) { |
| 86 | + assertSame( |
| 87 | + customClassLoader, |
| 88 | + workerClassLoader, |
| 89 | + "Worker thread should have inherited the custom classloader" |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments