[ISSUE #12884] Fix Flaky Behavior in NewMetadataSelector Tests #12885
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In response to ISSUE #12884
What is the purpose of the change
The purpose is to fix a flakiness issue in the
com.alibaba.nacos.client.naming.selector.NamingSelectorFactoryTest.testNewMetadataSelector
andtestNewMetadataSelector2
tests, caused by the non-deterministic iteration order of HashMap, which led to inconsistent test outcomes when metadata key-value pairs were accessed.You can see this issue because
testNewMetadataSelector
andtestNewMetadataSelector2
passed when I ran them normally using the command:mvn -pl client test -Dtest=com.alibaba.nacos.client.naming.selector.NamingSelectorFactoryTest#testNewMetadataSelector
but were failing when ran under NonDex, a testing tool that identifies flaky tests caused by non-deterministic behaviors. To run with NonDex, I used the command:mvn -pl client edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=com.alibaba.nacos.client.naming.selector.NamingSelectorFactoryTest#testNewMetadataSelector
Brief changelog
Replaced HashMap with LinkedHashMap in testNewMetadataSelector to ensure deterministic iteration order for metadata matching.
Explicitly set LinkedHashMap for instance metadata to preserve insertion order during matching.
Verifying this change
To verify this change, I ran the following to execute the tests with NonDex for 10 iterations:
mvn -pl client edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=com.alibaba.nacos.client.naming.selector.NamingSelectorFactoryTest#testNewMetadataSelector -DnondexRuns=10
and for the second test I ran:
mvn -pl client edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=com.alibaba.nacos.client.naming.selector.NamingSelectorFactoryTest#testNewMetadataSelector2 -DnondexRuns=10
These commands ensured each test ran 10 times under NonDex. Both
testNewMetadataSelector
andtestNewMetadataSelector2
passed consistently, confirming the absence of flakiness.Follow this checklist to help us incorporate your contribution quickly and easily:
[ISSUE #123] Fix UnknownException when host config not exist
. Each commit in the pull request should have a meaningful subject line and body.mvn -B clean package apache-rat:check findbugs:findbugs -Dmaven.test.skip=true
to make sure basic checks pass. Runmvn clean install -DskipITs
to make sure unit-test pass. Runmvn clean test-compile failsafe:integration-test
to make sure integration-test pass.