Fix flaky test in SimpleCrane4jGlobalConfigurationTest #180
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.
Change Assert condition to compare two sets instead of lists to make the test non-flaky.
Flaky test
crane4j/crane4j-core/src/test/java/cn/crane4j/core/support/SimpleCrane4jGlobalConfigurationTest.java
Line 34 in b65f732
Problem
Test
operatePropertyMappingStrategy
inSimpleCrane4jGlobalConfigurationTest
is detected as flaky with the NonDex tool. The test failed with the following error:Root cause
Multiple objects of class
PropertyMappingStrategy
are added to theconfiguration
object of classSimpleCrane4jGlobalConfiguration
using theaddPropertyMappingStrategy
function. These strategy objects are stored as a HashMap inSimplePropertyMappingStrategyManager
. To test whether they are added correctly or not, all the strategies are fetched using functiongetAllPropertyMappingStrategies()
which is converted to a ArrayList for assertion. But, a HashMap may not necessarily maintain the order of elements as they are inserted. Therefore, when fetched we may get a different order of elements and thus this test is detected as flaky using Nondex tool.Strategies are added here:
crane4j/crane4j-core/src/test/java/cn/crane4j/core/support/SimpleCrane4jGlobalConfigurationTest.java
Line 40 in b65f732
HashMap is used here:
crane4j/crane4j-core/src/main/java/cn/crane4j/core/parser/handler/strategy/SimplePropertyMappingStrategyManager.java
Line 22 in b65f732
Assertion is made here:
crane4j/crane4j-core/src/test/java/cn/crane4j/core/support/SimpleCrane4jGlobalConfigurationTest.java
Line 41 in b65f732
Fix
We can change the HashMap to a LinkedHashMap to keep the order deterministic. But this requires a change in the actual code and LinkedHashMap is slightly more computationally expensive than HashMap due to the overhead of maintaining the linked list. But since the order is not required in the code, I only updated the test to compare the result as a set instead of a List. This removes the flakiness in the test without touching the actual code.
This fix will not affect the code since the change is only made in tests.
How this has been tested?
Java: openjdk version "11.0.20.1"
Maven: Apache Maven 3.6.3
Command used -
Command used -
Command used -
NonDex tests passed after the fix.