Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid repeated attempts to load a failing ApplicationContext in the TestContext framework [SPR-9548] #14182

Closed
spring-projects-issues opened this issue Jun 27, 2012 · 11 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Mukarram Baig opened SPR-9548 and commented

Lets say I have a test class called ServiceTest with three test methods test1, test2, and test3. All three methods use some resources which are provided by Spring. In the current state of affairs, if there is a problem with the loading of the spring context, the context load is retried for each test method.

Is there a way that I can have it aborted on the first failure itself with existing hook-ins?

There might be good reasons behind it - but I fail to appreciate it. Any pointers would be helpful.

The real problem is that the context load takes a few minutes, and it is useless to keep reattempting to load the context if it has already failed for the first time and only seeks to prolong the time that the CI server takes to report failures.

I was thinking of having an attemptMap in ContextCache that the TestContext modifies when it encounters a failed ApplicationContext. A repeated load will be (on an opt-in flag set) disallowed via an exception. I have attached a rudimentary patch to indicate what I meant. Sorry that I haven't tested it (don't have gradle installed or an IDE plugin that can do it for me - am still a maven boy :))


Affects: 3.1 GA

Reference URL: http://stackoverflow.com/questions/11202535/ability-to-avoid-repeated-load-of-application-context-in-spring-testcontext-via

Attachments:

6 votes, 8 watchers

@spring-projects-issues
Copy link
Collaborator Author

muhamadto commented

Is there any update on when this enhancement might be added?

@spring-projects-issues
Copy link
Collaborator Author

Michael Tecourt commented

This feature would really speed up feedback of failing integration tests on a complete Spring (Boot) application.
CI builds last a long time looping desperately on a test context that will never load due to a bad configuration.
And if you have tests for every use case, the longer the wait.

We also let Spring start embedded databases or TestContainers rather than JUnit rules because the same context is reused for all test classes, when a JUnit class rule is reloaded for each test class.
When the context crashes though, we end up looping on costly database startups/shutdowns.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

I've moved this to the 5.x Backlog in order to look into this in the 5.x time frame.

@sbrannen
Copy link
Member

Related discussion on Stack Overflow.

@sbrannen
Copy link
Member

@dreis2211
Copy link
Contributor

Any progress on this would be highly appreciated as it unnecessarily prolongs build times and is hurting effectivity quite badly...
Gradle users might get away with configuring

test {
   failFast = true
}

But that is not the same thing as it just stops after the first test vs. executing all tests (but doing this in a fast way if we know the context won't start)

@sbrannen sbrannen modified the milestones: 6.0.x, 6.1.0-M1 Apr 24, 2023
@sbrannen
Copy link
Member

sbrannen commented Jun 8, 2023

Update: current work on this feature can be viewed in the following feature branch.

main...sbrannen:spring-framework:issues/gh-14182-TCF-context-load-failure-threshold

@sbrannen sbrannen changed the title Avoid repeated attempts to load failing ApplicationContext in the TestContext framework [SPR-9548] Avoid repeated attempts to load failing a ApplicationContext in the TestContext framework [SPR-9548] Jun 9, 2023
@sbrannen sbrannen changed the title Avoid repeated attempts to load failing a ApplicationContext in the TestContext framework [SPR-9548] Avoid repeated attempts to load a failing ApplicationContext in the TestContext framework [SPR-9548] Jun 9, 2023
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 9, 2023
This commit introduces initial support for a new "context failure
threshold" feature in the Spring TestContext Framework (TCF).

Specifically, DefaultCacheAwareContextLoaderDelegate now tracks how
many times a failure occurs when attempting to load an
ApplicationContext and preemptively throws an IllegalStateException for
subsequent attempts to load the same context if the configured failure
threshold has been exceeded.

See spring-projectsgh-14182
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 9, 2023
This commit makes the failure threshold value configurable via a JVM
system property or Spring property named
"spring.test.context.failure.threshold".

See spring-projectsgh-14182
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 9, 2023
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 9, 2023
sbrannen added a commit to sbrannen/spring-framework that referenced this issue Jun 9, 2023
@sbrannen
Copy link
Member

sbrannen commented Jun 9, 2023

If you've voted for this issue, you'll be happy to know that...

Spring Framework 6.1 will finally have "test context failure threshold" support! 🍃 🥳

It's been a long time coming, but you can now rest assured that your test suite won't take forever due to failing ApplicationContext configuration.

For details, check out the documentation and the related commit.

sbrannen added a commit that referenced this issue Jun 11, 2023
In the previous commit which introduced the new context failure threshold
support in the TestContext framework, the context failure tracking was
tied to an instance of DefaultCacheAwareContextLoaderDelegate.
Consequently, the feature was only supported within a given test class.

This commit therefore moves context failure tracking to the ContextCache
SPI (and DefaultContextCache) so that the feature applies to all test
classes within the current test suite (i.e., JVM).

This commit also includes the total failure count in the statistics
logged by the DefaultContextCache.

See gh-14182
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 type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants