Fix non-deterministic test writer_readingMultipleTopics_shouldBatchSeparate() #28
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.
What does this PR do?
Hi, I am from the Software Testing research group at the University of Illinois. We found that the test
com.datadoghq.connect.logs.sink.DatadogLogsApiWriterTest.writer_readingMultipleTopics_shouldBatchSeparate()
is non deterministic or flaky. We use the NonDex tool to detect the non determinism (https://github.com/TestingResearchIllinois/NonDex). This PR highlights the source of non determinism and proposes a fix.Reproduction steps:
mvn install -am
mvn test -Dtest=com.datadoghq.connect.logs.sink.DatadogLogsApiWriterTest#writer_readingMultipleTopics_shouldBatchSeparate
This will run the test as is. Confirm that this passes.mvn edu.illinois:nondex-maven-plugin:1.1.2:nondex -DnondexMode=ONE -Dtest=com.datadoghq.connect.logs.sink.DatadogLogsApiWriterTest#writer_readingMultipleTopics_shouldBatchSeparate
This command runs the same test with the NonDex tool. Notice that the build fails this time, which means that the test is non deterministic.Source of non determinism:
The non determinism arises from line
62
invoid com.datadoghq.connect.logs.sink.DatadogLogsApiWriter.flushBatches()
. On that line, we are iterating on a HashMap. Depending on the iteration order, either of request1 or request2 may come first in the loop. This is because the HashMap interface is not guaranteed to have a deterministic order of iteration of its elements. From java documentation on HashMap:This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.
(https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html). Therefore, our tests need to account for all the possibilities of iteration order. Currently, it only accounts for one of the order, and it only works because of the underlying specific implementation of HashMap, which may change anytime.Proposed fix:
We account for both the iteration orders (request1 first or request2 first) in the test. You may see the files changed for the exact fix.
Motivation
What inspired you to submit this pull request?
At the Software Testing research group, one of our projects is to find and fix flaky tests in various repositories like this one. See our project repository at https://github.com/TestingResearchIllinois/idoft.
Additional Notes
Anything else we should know when reviewing?