Skip to content

Conversation

@gnosly
Copy link

@gnosly gnosly commented Jan 25, 2025

Hi,

when I recently activated the junit thread parallelisation in my project I discovered that different tests are waiting each other even if they are creating different application contexts.
In fact a synchronised block inside DefaultCacheAwareContextLoaderDelegate.loadContext is preventing different threads to load also different contexts.
Waiting the context creation is essential to reuse the context when different tests are requesting the same context (context caching). But it is inefficient when the contexts are different.

So I replaced the synchronised block with a lazy creation of the context (using the combination of Map, Future and a separated thread pool).

Now during the test execution the DefaultCacheAwareContextLoaderDelegate will put the context key and the lazy computation in the contextMap - and this operation is really fast. Then DefaultCacheAwareContextLoaderDelegate will wait the context creation during the Future.get invocation . If different tests are requesting the same context they will wait the same unique computation, preserving the caching feature. If they are requesting different contexts the creation of them will be scheduled in parallel.

I prepared this poc to show you the idea. If you believe this is correct, please give me some guideline in order to prepare a better pull request.

I also created this project that reproduce the bottleneck.

Just to give you an real example, with this change I reduce the testing time from 4 to 3 minutes for a project.

Please let me know,
Thank you

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 25, 2025
@sbrannen sbrannen added the in: test Issues in the test module label Jan 25, 2025
@sbrannen sbrannen changed the title [POC] Optimisation in context creation for testing [POC] Optimize parallel context creation in the TestContext framework Jan 25, 2025
@sbrannen sbrannen changed the title [POC] Optimize parallel context creation in the TestContext framework [POC] Optimize parallel context loading in the TestContext framework Jan 25, 2025
Signed-off-by: fabrizio giovannetti <[email protected]>
@gnosly
Copy link
Author

gnosly commented Sep 16, 2025

Hi @sbrannen,
have you got a chance to look at this improvement?
I would like to propose an incremental step to you. We could make the CacheAwareContextLoaderDelegate implementation selectable using a system property (in BootstrapUtils class). In this way the community can experiment with this suggestion using a custom implementation. what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: test Issues in the test module status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants