Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed HashMap to LinkedHashMap for deterministic order of headers. #1818

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RugvedB
Copy link

@RugvedB RugvedB commented Nov 27, 2023

Created this PR to fix 5 flaky tests listed as follows:

  1. (shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverValuesUsingThis)

  2. (shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverEntrySet)

  3. (shouldHandleHttpRequestsWithVelocityResponseTemplateWithLoopOverValues)

  4. (shouldHandleHttpRequestsWithJavaScriptResponseTemplateWithLoopOverValuesUsingThis)

  5. (shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverKeysUsingThis)


  1. 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.

  2. 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.

Expected: is <{
  "statusCode" : 200,
  "body" : "{'headers': ['mock-server.com', 'plain/text']}"
}>
     but: was <{
  "statusCode" : 200,
  "body" : "{'headers': ['plain/text', 'mock-server.com']}"
}>
  1. What is the fix?
    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:

mvn -pl mockserver-core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.mockserver.templates.engine.mustache.MustacheTemplateEngineTest#shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverValuesUsingThis
mvn -pl mockserver-core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.mockserver.templates.engine.mustache.MustacheTemplateEngineTest#shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverEntrySet
mvn -pl mockserver-core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.mockserver.templates.engine.velocity.VelocityTemplateEngineTest#shouldHandleHttpRequestsWithVelocityResponseTemplateWithLoopOverValues
mvn -pl mockserver-core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.mockserver.templates.engine.javascript.JavaScriptTemplateEngineTest#shouldHandleHttpRequestsWithJavaScriptResponseTemplateWithLoopOverValuesUsingThis -Dnondexruns=100
mvn -pl mockserver-core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.mockserver.templates.engine.mustache.MustacheTemplateEngineTest#shouldHandleHttpRequestsWithMustacheResponseTemplateWithLoopOverKeysUsingThis

Test Environment:

java version 11.0.19
Apache Maven 3.9.5

Kindly let me know if this fix is acceptable.
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants