Fix non-deterministic behavior in testMultipartFormData #649
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.
Description
I encountered non-deterministic behavior while running the following tests using NonDex:
core.src.test.java.org.nanohttpd.junit.protocols.http.HttpServerTest.testMultipartFormData
Steps to reproduce
NonDex: https://github.com/TestingResearchIllinois/NonDex
Run the tests with NonDex:
The error message shows:
Click to view
Potential Solution
The issue occurs due to non-deterministic behavior of function
public Response serve(IHTTPSession session)
in testMultipartFormData. The function iterates through the key set ofMap<String, String>
objectfiles
and then appends the keys toStringBuilder
objectresponseMsg
. However, the order of the key set of a Map object in Java is non-deterministic, which means the keys appended toresponseMsg
can be in any order. This causes theString
variableline
at line 266 to be either"bincomment"
or"commentbin"
, and thusassertEquals
at line 268 could fail.Here I propose a solution that may fix the issue. Before we iterate the key set of the map object, we can store all the keys in a
List<String>
object and sort the list. Now, the order of keys in the list becomes deterministic, and we can iterate through the list and append the keys toresponseMsg
in a deterministic order. In this way,line
variable will always be"bincomment"
, the same as the expected value.