Skip to content

Keep unit test JVMs for the whole task, flushing the Introspector after each class - #16837

Merged
jdaugherty merged 4 commits into
apache:8.0.xfrom
matrei:experiment/test-fork-reuse
Nov 4, 2026
Merged

jdaugherty merged 4 commits into
apache:8.0.xfrom
matrei:experiment/test-fork-reuse

Conversation

@matrei

@matrei matrei commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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.

  1. A new, unpublished module, grails-test-listener, registers a JUnit Platform TestExecutionListener through META-INF/services that calls Introspector.flushCaches() after each test class. gradle/test-config.gradle and grails-test-suite-uber put it on the test runtime classpath.
  2. forkEvery now defaults to 0 in gradle/test-config.gradle, and grails-test-suite-uber no longer sets it. -PforkEveryUnitTest=<n> still sets it.

Why

  • Memory is no longer a reason to restart. The Introspector's BeanInfo cache, the one Release the Introspector's BeanInfo cache after each TCK spec #16834 flushes after each TCK spec, is what grows with the number of classes a JVM has run: Groovy reads the BeanInfo of every class it builds a MetaClass for, and the JDK keeps it until the class is unloaded, which the classes of a test JVM never are. With the flush after each class, the live heap of a JVM stays flat.
  • The current setting mostly does nothing. Gradle counts the top-level classes it sends to a JVM, not the test classes JUnit runs, and deals them to the JVMs in turn, so with CI's 4 JVMs a module restarts only past 200 such classes. Of the four largest modules that need no Docker, grails-core (187) and grails-gsp (192) never restart, and most modules are much smaller. Where it does restart, it is largely by accident: grails-test-suite-uber does not apply test-config.gradle and so has no **/*$*.class exclude, so its 910 class files, closures included, count, and its JVMs restart about every 12 test classes.
  • It continues Flaky Build Test Cleanup #15335, which raised forkEvery from 20 to 50 to cut the restart overhead once the tests that leaked state were fixed.
  • The functional tests already run this way: functional-test-config.gradle sets forkEvery only with -PtestForked, so each example application's test tasks run in one JVM.
  • One magic number fewer, and CI and local runs behave the same.

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 -PtestBisect already runs with forkEvery = 0 to find the class that leaks.

Local measurements

The four largest modules that need no Docker, at the CI values (-PmaxTestParallel=4 -PtestForkHeapMb=768). A is forkEvery = 50 with the listener off, B forkEvery = 0 with the listener off, C forkEvery = 0 with 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.

Module JVMs, A → B/C Time, A / B / C Peak live heap, A / B / C Cached BeanInfos, B → C
grails-test-suite-uber 20 → 4 55 s / 39 s / 38 s 111 / 164 / 132 MB 1,470 → 4
grails-datamapping-core 8 → 4 52 s / 44 s / 45 s 122 / 170 / 141 MB 1,737 → 2
grails-gsp 4 → 4 72 s / 73 s / 69 s 313 / 313 / 220 MB 3,196 → 5
grails-core 8 → 8 47 s / 46 s / 47 s 105 / 105 / 93 MB 799 → 0

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 for test and 4 for testCli. 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, PerTestRecordingSpec in grails-test-examples-geb, which applies neither test-config.gradle nor 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-Core jobs 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's compileGroovy, so the misses came from the remote cache, not from this change.

Not changed here

The configureEach of test-config.gradle runs after the settings of the suites that apply it: grails-test-suite-web:execIsolatedTests runs with forkEvery = 50, not the 1 its build script sets, and grails-test-suite-persistence loses its forkEvery, its local 2 GB heap and its three excludes, so the classes it isolates in their own tasks also run in test.

./gradlew clean aggregateViolations --continue passes locally.

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.
@matrei matrei added the run ci label Nov 3, 2026
@matrei matrei changed the title Experiment: keep unit test JVMs for the whole task, flushing the Introspector after each class Keep unit test JVMs for the whole task, flushing the Introspector after each class Nov 3, 2026
@matrei
matrei marked this pull request as ready for review November 3, 2026 14:00

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and excludes) is overwritten by test-config.gradle, as the description says.
  • grails-test-suite-web/build.gradle: defaultTestConfig still sets forkEvery = isCiBuild ? 50 : 100, and execIsolatedTests sets forkEvery = 1. Both are overwritten by test-config.gradle, which now means 0. execIsolatedTests was only ever isolated by accident before, but after this PR its classes share JVMs on purpose.
  • grails-test-suite-uber/build.gradle: isolatedTestsTwo keeps forkEvery = 100.
  • grails-data-neo4j/core/build.gradle: forkEvery = 10. This module doesn't apply test-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.
@matrei

matrei commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @sbglasius.

  1. Remove the test suites' isolation settings that test-config.gradle overrides #16839 removes the leftovers in grails-test-suite-persistence and grails-test-suite-web, and documents in test-config.gradle that a module sets maxParallelForks, maxHeapSize and forkEvery after applying it. isolatedTestsTwo no longer sets forkEvery = 100 (fd8a16f); its two classes never reached it. The Neo4j module is in grails-data-neo4j-core restarts its test JVMs every 10 classes, without the Introspector flush #16856: it doesn't apply test-config.gradle, so its forkEvery = 10 and heap need measuring before they change.
  2. The Javadoc now says that BeanInfo cached on threads of other thread groups is not released (fd8a16f).
  3. Uber now has the same excludes as test-config.gradle (fd8a16f). It still runs 122 classes and 624 tests in test, and 10 classes and 42 tests in its isolated tasks.
  4. The spec has a feature for the engine, an EngineDescriptor, which keeps the cache (fd8a16f).

1fcaf1d only changes the style of the spec.

@testlens-app

testlens-app Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 1 failed test 🚨

Test Summary

Tip

Inspect all details with the TestLens CLI: testlens pr 16837

CI / Functional Tests (Java 21, indy=false) > :grails-test-examples-geb:integrationTest

Test Runs Flakiness
PerTestRecordingSpec > the recordings of the previous two tests are different ❌ 2% 🟡

🏷️ Commit: 1fcaf1d
▶️ Tests: 28256 executed
🟡 Checks: 91/92 completed

Test Failures

PerTestRecordingSpec > 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)
Condition not satisfied:

secondRecording.length() > MIN_MEANINGFUL_RECORDING_BYTES
|               |        | |
|               2790     | 5000
|                        false
build/gebContainer/recordings/20261104_173258/PASSED-PerTestRecordingSpec_setup_running_a_second_test_to_create_another_recording-20261104-173620.mp4

	at org.demo.spock.PerTestRecordingSpec.the recordings of the previous two tests are different(PerTestRecordingSpec.groovy:105)

Rerun Controls

Note

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 jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@matrei
matrei requested a review from sbglasius November 4, 2026 21:05
@jdaugherty
jdaugherty merged commit 9b7e9ff into apache:8.0.x Nov 4, 2026
166 of 167 checks passed
@matrei
matrei deleted the experiment/test-fork-reuse branch November 5, 2026 07:12
@matrei
matrei removed the request for review from sbglasius November 5, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants