Flaky test fixed for testJvmMethodSorter #1765
Open
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
Flaky tests are common occurrences in open-source projects, yielding inconsistent results—sometimes passing and sometimes failing—without code changes. NonDex is a tool for detecting and debugging wrong assumptions on under-determined Java APIs. I have resolved a flaky test issue using NonDex tool, specifically in the
MethodSorterTest
class located atorg.junit.internal.MethodSorterTest.testJvmMethodSorter
Root cause
The flakiness comes from the original codes which tends to sort the list of methods of a class, and use this test to test whether the declared methods of a class is gotten in a predictable order after going through his method sorter. However, the codes here firstly gets the first list of methods using
java.lang.Class.getDeclaredMethods()
, then gets the second one by making the class of methods going through the sorter, notice that the sorter is written based on a comparator that compare the 'methods' words by getting their hashcode. The fact is,java.lang.Class.getDeclaredMethods()
method in Java does not guarantee a specific order for the returned methods. The order in which the methods are returned is not specified and may vary between different Java implementations or versions.Fix
The logical of comparator is correct, the test is made flaky only because of
java.lang.Class.getDeclaredMethods()
, thus remove that function to get the methods' list, instead directly apply the comparator's logic in the test to make it work normally.Setup
Java: openjdk version "11.0.20.1"
Maven: Apache Maven 3.6.3
How to test
mvn install -pl . -am -DskipTests
mvn -pl . test -Dtest=org.junit.internal.MethodSorterTest#testJvmMethodSorter
mvn -pl . edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=org.junit.internal.MethodSorterTest#testJvmMethodSorter
After fix, all tests pass