Fix: Flaky Test in snippets.SnippetsTests.union_test
#18
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 is the purpose of this PR
This pull request fixes a flaky test in
snippets.SnippetsTests.union_test. The original test assumed that the order of elements in the union would always be deterministic. However, the underlying implementation ofSnippets.unionuses aSet(likelyHashSet) to compute the union, which does not guarantee element order. As a result, the test failed intermittently when the iteration order changed.The fix ensures the test assertion is order-independent by sorting the result before comparison.
Why the test fails
Snippets.unioninternally returns an array built from aSetwith nondeterministic iteration order.HashSetdoes not guarantee element order, the returned array may be[1, 2, 3, 4]in one run and[2, 4, 1, 3]in another.How to reproduce the test failure
Run the test multiple times:
mvn test -Dtest=snippets.SnippetsTests#union_testIt may pass or fail depending on execution order.
Run with NonDex (to shuffle iteration orders):
This consistently exposes the nondeterminism and reproduces the failure.
Expected results
The union of
{1, 2, 3}and{1, 2, 4}should always contain all unique elements:Order should not matter because this is a set operation.
Actual results (before fix)
causing the assertion to fail intermittently.
Description for fix
The test assertion was updated to sort the output before comparison, ensuring order independence:
This guarantees that the union result always matches the expected elements, regardless of iteration order.
With this fix, the test passes deterministically and is no longer prone to flakiness.