sort fields for deterministic order #27
Open
+19
−11
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.
Same as #26, the old PR commits had some issues that made me unable to sign the CLA.
The tests
com.cloudhopper.commons.util.MetaFieldUtilTest.toMetaFieldInfoArrayis a flaky test, it can pass mvn test while but when run using the tool NonDex, it fails. NonDex is a tool that will introduce non-determinism in certain java collections.The test shows below
The root cause is that when executing
MetaFieldUtil.toMetaFieldInfoArray(emp)inMetaFieldUtilTest.java, it will invoke theinternalToMetaFieldInfoArray, which callsjava.lang.Class.getDeclaredFields. The Java8 specification aboutgetDeclaredFieldsis that "The elements in the returned array are not sorted and are not in any particular order", with reference here: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html. Therefore, this test may fail due to a different order.In order to make the
for(Field field : classType.getDeclaredFields())iteration order stable and get rid of this non-deterministic behavior, this fix is to sort the Field array returned bygetDeclaredFields()by its name and then change the order of the assertions in toMetaFieldInfoArrayto match with the sorted order.