Repository navigation
Keep unit test JVMs for the whole task, flushing the Introspector after each class - #16837
Conversation
Groovy reads the BeanInfo of a class when it builds the class's MetaClass, and the JDK Introspector caches it until the class is unloaded, which the classes of a test JVM never are. The new, unpublished grails-test-listener module registers a JUnit Platform TestExecutionListener that flushes the cache after each test class, so a test JVM's live set no longer grows with the number of classes it has run. test-config.gradle and grails-test-suite-uber put it on the test runtime classpath.
test-config.gradle restarted every test JVM after 50 test classes on CI and 100 locally, and grails-test-suite-uber did the same, with closures counted as classes. With the Introspector flushed after each class the live set of a JVM no longer grows with the classes it has run, so forkEvery now defaults to 0; -PforkEveryUnitTest still sets it.
sbglasius
left a comment
There was a problem hiding this comment.
This an AI assisted review
Nice, well-measured change. The listener is small and does one thing, the "not published" reasoning holds up (the BOM only takes subprojects that apply a publish plugin, and end-to-end, grails-forge and grails-gradle each have their own gradle/test-config.gradle, so the project(':grails-test-listener') dependency doesn't leak into those builds), and CI is green. A few comments, none blocking:
1. Leftover forkEvery settings that now do nothing (or something unexpected)
After this PR, forkEvery is still set in a few places that either get overwritten or no longer match the new default:
grails-test-suite-persistence/build.gradle:forkEvery = isCiBuild ? 25 : 100(and the 2 GB heap andexcludes) is overwritten bytest-config.gradle, as the description says.grails-test-suite-web/build.gradle:defaultTestConfigstill setsforkEvery = isCiBuild ? 50 : 100, andexecIsolatedTestssetsforkEvery = 1. Both are overwritten bytest-config.gradle, which now means0.execIsolatedTestswas only ever isolated by accident before, but after this PR its classes share JVMs on purpose.grails-test-suite-uber/build.gradle:isolatedTestsTwokeepsforkEvery = 100.grails-data-neo4j/core/build.gradle:forkEvery = 10. This module doesn't applytest-config.gradle, so it also doesn't get the listener.
None of these come from this PR, and the description lists the persistence and web ones under "Not changed here". Still, the PR's goal is "one magic number fewer, CI and local behave the same", and these leftovers make it hard to tell what a module actually runs with. Either remove the dead lines here, or open a follow-up issue that fixes the ordering (e.g. test-config.gradle only sets forkEvery when the module hasn't set it, or the modules set theirs after applying the script). I'd prefer the follow-up issue be linked from this PR.
2. The flush only reaches the calling thread's thread group
The Javadoc says this correctly. Still, a test that builds MetaClasses on threads in another ThreadGroup (some executor/server thread factories create their own group) caches BeanInfos the flush never clears. ClassInfo.clear() inside flushCaches() is global, but the BeanInfo cache isn't. That's fine for what the PR measures. I'd add one sentence to the Javadoc saying caches built on threads in other thread groups are not released, so nobody later assumes the listener makes every JVM's heap flat.
3. grails-test-suite-uber still has no **/*$*.class exclude
With forkEvery = 0 the closure classes no longer cause restarts, so it no longer matters for that. Gradle still scans and dispatches them, though. Since this PR touches the uber build anyway, adding the same excludes as test-config.gradle would be a cheap consistency fix. Optional.
4. Nit: spec
IntrospectorFlushListenerSpec covers the service registration, the class-container case and three negative cases. Nice. You could also add a row for the engine root (CONTAINER with no source), which is the other container the launcher reports, to pin isTestClass down completely.
Overall 👍 from me once (1) has a follow-up issue or is cleaned up.
The listener's Javadoc says that BeanInfo cached on threads of other thread groups is not released, and its spec pins down that the engine, a container with no source, does not flush. The uber build takes the excludes of test-config.gradle, which it does not apply, and isolatedTestsTwo no longer sets a forkEvery its two classes never reached.
The spec's locals are final with an inferred type, and its nested and anonymous classes leave a blank line after their opening line.
|
Thanks for the review, @sbglasius.
1fcaf1d only changes the style of the spec. |
🚨 TestLens detected 1 failed test 🚨Test SummaryTip Inspect all details with the TestLens CLI: CI / Functional Tests (Java 21, indy=false) > :grails-test-examples-geb:integrationTest
🏷️ Commit: 1fcaf1d Test FailuresPerTestRecordingSpec > the recordings of the previous two tests are different (:grails-test-examples-geb:integrationTest in CI / Functional Tests (Java 21, indy=false) | Attempt 1/2)Rerun ControlsNote Checks are currently running using the configuration below. Select tests to mute in this pull request: 🔲 PerTestRecordingSpec > the recordings of the previous two tests are different Reuse successful test results: 🔲 ♻️ Only rerun the tests that failed or were muted before Click the checkbox to trigger a rerun: ☑️ Rerun jobs Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
The listener is on the test runtime classpath, including the uber suite, and flushes the Introspector cache after each test class. The module is unpublished. forkEvery defaults to 0, and -PforkEveryUnitTest still overrides it. Both reviewers found this clean.
Description
The unit test tasks keep their test JVMs for the whole task instead of restarting them every 50 test classes on CI and every 100 locally.
grails-test-listener, registers a JUnit PlatformTestExecutionListenerthroughMETA-INF/servicesthat callsIntrospector.flushCaches()after each test class.gradle/test-config.gradleandgrails-test-suite-uberput it on the test runtime classpath.forkEverynow defaults to 0 ingradle/test-config.gradle, andgrails-test-suite-uberno longer sets it.-PforkEveryUnitTest=<n>still sets it.Why
grails-core(187) andgrails-gsp(192) never restart, and most modules are much smaller. Where it does restart, it is largely by accident:grails-test-suite-uberdoes not applytest-config.gradleand so has no**/*$*.classexclude, so its 910 class files, closures included, count, and its JVMs restart about every 12 test classes.forkEveryfrom 20 to 50 to cut the restart overhead once the tests that leaked state were fixed.functional-test-config.gradlesetsforkEveryonly with-PtestForked, so each example application's test tasks run in one JVM.The speedup is small: about 25 seconds across the four largest modules locally (below), which is not measurable on CI against the variation between runs.
The cost is isolation: a test class that leaks static state now reaches every later class in its JVM, not at most the next 49. The known polluters already run in their own isolated tasks,
-PforkEveryUnitTest=<n>brings the restarts back, and-PtestBisectalready runs withforkEvery = 0to find the class that leaks.Local measurements
The four largest modules that need no Docker, at the CI values (
-PmaxTestParallel=4 -PtestForkHeapMb=768). A isforkEvery = 50with the listener off, BforkEvery = 0with the listener off, CforkEvery = 0with the listener on. A probe listener (not committed) forced a full GC every 10 test classes and at the end of each JVM and recorded the live heap and the size of the Introspector's cache. Times are the median of three runs for uber and datamapping-core and of one run for the others.Every run passed with the same test counts, and there was no full GC other than the probe's own.
grails-core's 8 JVMs are 4 fortestand 4 fortestCli. A JVM that runs a whole task holds 9-12 MB more metaspace.CI
The first run of this pull request ran 109,471 tests with one failure,
PerTestRecordingSpecingrails-test-examples-geb, which applies neithertest-config.gradlenor the listener and has failed before (#16435). No lane ran out of memory.Against the median of the last six 8.0.x runs, the Ubuntu JDK 25 and Windows
Build Grails-Corejobs took between 10% less and 9% more, macOS 52% more but within the range of those runs, and Ubuntu JDK 21 82 minutes against a median of 46. The Ubuntu JDK 21 job loaded 542 tasks from the remote build cache, against 1,140 for the same job of #16834 the same morning, and compiled the example applications itself. Builds of the base commit and of this branch from an empty cache give the same cache key for an example application'scompileGroovy, so the misses came from the remote cache, not from this change.Not changed here
The
configureEachoftest-config.gradleruns after the settings of the suites that apply it:grails-test-suite-web:execIsolatedTestsruns withforkEvery = 50, not the 1 its build script sets, andgrails-test-suite-persistenceloses itsforkEvery, its local 2 GB heap and its threeexcludes, so the classes it isolates in their own tasks also run intest../gradlew clean aggregateViolations --continuepasses locally.