Upgrade test testExpectedJson by removing non-determinism #1335
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.
The test TestJsonSubType#testExpectedJson fails non-deterministically due to non-deterministic ordering of fields. This happens because
objectMapper.writeValueAsString()
internally usesjava.lang.Class.getDeclaredFields
method and according to javadocs, 'The elements in the returned array are not sorted and are not in any particular order.'How to reproduce the issue:
mvn -pl json edu.illinois:nondex-maven-plugin:2.2.1:nondex -Dtest=TestJsonSubType#testExpectedJson -DnondexRuns=10
Error
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.983 s <<< FAILURE! -- in io.airlift.json.subtype.TestJsonSubType
[ERROR] io.airlift.json.subtype.TestJsonSubType.testExpectedJson -- Time elapsed: 0.903 s <<< FAILURE!
org.opentest4j.AssertionFailedError:
expected: "{"name":"Jane","reports":[{"name":"Joe","type":"Programmer"},{"name":"Rachel","type":"Programmer"}],"type":"Manager"}"
but was: "{"reports":[{"name":"Joe","type":"Programmer"},{"name":"Rachel","type":"Programmer"}],"name":"Jane","type":"Manager"}"
at io.airlift.json.subtype.TestJsonSubType.testExpectedJson(TestJsonSubType.java:208)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Proposed Solution
Add JsonPropertyOrder to record Manager to specify deterministic order of fields at the time of serialization.
Happy to discuss if you have any questions or need any additional justification/changes from my side.
Airlift contribution check list