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

@ActiveProfiles with same profiles but different order results in duplicate ApplicationContext #25973

Closed
1 task done
sbrannen opened this issue Oct 26, 2020 · 0 comments
Closed
1 task done
Assignees
Labels
in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Oct 26, 2020

Overview

While working on #25800, I realized that two @ActiveProfiles declarations on different test classes using the same profiles but declared in a different order would result in the creation of a duplicate yet identical ApplicationContext in the context cache in the Spring TestContext Framework. This is because ActiveProfilesUtils stores the active profiles in a LinkedHashSet to preserve registration order. The set is then converted to a String[] which is handed off to MergedContextConfiguration which also uses a LinkedHashSet to ensure uniqueness but still retains the registration order of the active profiles. When the MergedContextConfiguration is used as the context cache key to check whether an ApplicationContext already exists for the given metadata, the implementation of equals() in MergedContextConfiguration uses Arrays.equals() to compare two sets of active profiles, and that results in a cache miss for semantically identical active profile arrays { apples, oranges } and { oranges, apples }. Consequently, an identical ApplicationContext is created and stored in the context cache when that should not be the case.

Deliverables

  • Ensure that active profiles are both unique and sorted within MergedContextConfiguration in order to avoid context cache misses.
@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Oct 26, 2020
@sbrannen sbrannen added this to the 5.2.10 milestone Oct 26, 2020
@sbrannen sbrannen self-assigned this Oct 26, 2020
@sbrannen sbrannen added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Oct 26, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Oct 26, 2020
sbrannen added a commit that referenced this issue Oct 26, 2020
…rder

Prior to this commit, two @activeprofiles declarations with the same
profiles but different order resulted in an identical duplicate
ApplicationContext in the context cache in the Spring TestContext
Framework.

This commit uses a TreeSet to ensure that registered active profiles
are both unique and sorted, thereby avoiding cache misses for
semantically identical active profiles configuration on different test
classes.

Closes gh-25973
izeye added a commit to izeye/spring-boot-throwaway-branches that referenced this issue Nov 1, 2020
@activeprofiles doesn't seem to keep registration order of profiles since spring-projects/spring-framework#25973.
sbrannen added a commit that referenced this issue Nov 2, 2020
With this commit, bean definition profiles declared via @activeprofiles
are once again stored in registration order, in order to support use
cases in Spring Boot and other frameworks that depend on the
registration order.

This effectively reverts the changes made in conjunction with gh-25973.

Closes gh-26004
sbrannen added a commit that referenced this issue Nov 2, 2020
With this commit, bean definition profiles declared via @activeprofiles
are once again stored in registration order, in order to support use
cases in Spring Boot and other frameworks that depend on the
registration order.

This effectively reverts the changes made in conjunction with gh-25973.

Closes gh-26004
sbrannen added a commit that referenced this issue Nov 3, 2020
With this commit, bean definition profiles declared via @activeprofiles
are once again stored in registration order, in order to support use
cases in Spring Boot and other frameworks that depend on the
registration order.

This effectively reverts the changes made in conjunction with gh-25973.

Closes gh-26004
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
…rder

Prior to this commit, two @activeprofiles declarations with the same
profiles but different order resulted in an identical duplicate
ApplicationContext in the context cache in the Spring TestContext
Framework.

This commit uses a TreeSet to ensure that registered active profiles
are both unique and sorted, thereby avoiding cache misses for
semantically identical active profiles configuration on different test
classes.

Closes spring-projectsgh-25973
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
With this commit, bean definition profiles declared via @activeprofiles
are once again stored in registration order, in order to support use
cases in Spring Boot and other frameworks that depend on the
registration order.

This effectively reverts the changes made in conjunction with spring-projectsgh-25973.

Closes spring-projectsgh-26004
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: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants