Changed HashMap to LinkedHashMap for deterministic order of headers. #1818
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.
Created this PR to fix 5 flaky tests listed as follows:
(shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverValuesUsingThis)
(shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverEntrySet)
(shouldHandleHttpRequestsWithVelocityResponseTemplateWithLoopOverValues)
(shouldHandleHttpRequestsWithJavaScriptResponseTemplateWithLoopOverValuesUsingThis)
(shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverKeysUsingThis)
How were these test identified as flaky?
These tests were identified as flaky by using an open-source research tool named NonDex which is responsible for finding and diagnosing non-deterministic runtime exceptions in Java programs.
What was the error?
The tests were failing due to non-deterministic order of headers returned in HttpResponse's object httpResponse. While creating the httpResponse object, the code uses HashMap here to store headers. But, as the order of the keys returned by HashMap is not guaranteed, the order of elements in httpResponse object's headers array is non-deterministic.
All the above listed 5 tests have the below error and fixing one test would fix all the tests.
To make the order of elements inside the httpResponse's object's headers array deterministic, we should use LinkedHashMap instead of HashMap because LinkedHashMap guarantees the order of elements and thus, provides deterministic order of keys. This PR proposes changing HashMap to LinkedHashMap here to fix all the listed tests.
You can run the following commands to run the 5 tests using NonDex tool respectively:
Test Environment:
Kindly let me know if this fix is acceptable.
Thank you