Discover tests with ClassLoader other than ImageClassLoader - #445
Conversation
|
I am not sure this change is safe. Probably @sbrannen has more expertise than I do on this particular aspect. I'm in particular wondering about the use of the context classloader. |
|
Just found this commit is not enough to isolate more complicated class initializations during tests discovery. SubstrateVM's pointsto analysis scans constants and adds them into native image regardless how it is loaded. If such a constant's class should not be initialized at build time, but is initialized during test discovery. The build of native test will never succeed.
For your question. SubstrateVM sets the context classloader to |
JUnitPlatformFeature discovers tests before analysis. The discovery procsee will initialize some of the test classes when @RunWith, @parameterized annotations are used. As they are loaded by the ImageClassloader, the initialization may cause eager class initialization error during native image building time. This commit lauches the test discovery in a another Java process in the JUnitPlatformFeature to make sure the test class initialization at discovery time won't affect the native image class initialization policy.
d4a6011 to
3853830
Compare
ClassLoader other than ImageClassLoader
|
We're going to have to revert this PR as it breaks builds with build time initialization happening with JUnit classes. |
|
@melix Can you provide a test case? |
|
You simply have to run |
JUnitPlatformFeaturediscovers tests before analysis. The discovery process will initialize some of the test classes. As they are loaded by theImageClassloader, the initialization will cause eager class initialization error during native image building time.This PR uses a different classloader than the
ImageClassLoaderin theJUnitPlatformFeatureto make sure the test class initialization at discovery time won't affect the native image class initialization policy.==updated July 7th 2023 ==
This PR launches a separated Java process to run the test discovery if
-DisolateTestDiscovery=trueis set (it is set by default), and goes to the original way otherwise. The reason is the initialization of test class may initialize some JDK classes that are loaded by null classloader. In this case, classloader isolation is not sufficient.The attached demo shows the issue.
The GraalVM version is java11-22.3.1.
Simply unzip the file and run
mvn -Pnative testandmvn -Pnative test -DisolateTestDiscovery=falseto see the difference.native-build-tool-demo.zip