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

review: test: Fixed 4 non-deterministic flaky tests #6084

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nikunjagarwal321
Copy link

What changes were proposed in this pull request?

Sorting the order of imports list variable and enums list variable in 4 flaky tests in order to make them deterministic. The assertions are written with an assumption that the list will always be in a particular predefined order, but that may not be the case. The PR contains fix for the below test cases to make them deterministic:

  • spoon.test.imports.ImportTest#staticImports_ofNestedTypes_shouldBeRecorded
  • spoon.test.imports.ImportTest#staticTypeAndMethodImport_importShouldAppearOnlyOnceIfTheirSimpleNamesAreEqual
  • spoon.test.position.PositionTest#testPositionOfCtImport
  • spoon.test.template.SubstitutionTest#testCreateTypeFromTemplate

Why are the changes needed?

Multiple flaky tests were detected while trying to run the tests using the nondex tool. NonDex is a tool for detecting and debugging wrong assumptions on under-determined Java APIs.

Sample ERROR logs:

Screenshot 2024-11-24 at 8 40 54 PM Screenshot 2024-11-24 at 8 43 33 PM

Reason for Failure

As mentioned above, the assertions assume that the list will always be in a particular predefined order, but that may not be the case. For eg, in test case : staticImports_ofNestedTypes_shouldBeRecorded, we have the following test case

List<CtImport> imports = mainType.getPosition().getCompilationUnit().getImports();
assertThat(imports, hasSize(2));

CtImport import0 = imports.get(0);
assertThat(import0.getReference().getSimpleName(), is("InnerClass"));

The above code assumes that the first element of the list will be InnerClass which may not be true. This is because the imports is being set by JDTImportBuilder.java class where the imports variable is implemented as HashSet. As per the javadocs, the order of elements is not maintained in HashSet.
Screenshot 2024-11-24 at 8 55 01 PM

Hence, we sort the fields before asserting based on certain fields to make the order deterministic.

Steps to reproduce flakiness using nondex -

mvn install -DskipTests

mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=spoon.test.position.PositionTest#testPositionOfCtImport
mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=spoon.test.template.SubstitutionTest#testCreateTypeFromTemplate
mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=spoon.test.imports.ImportTest#staticTypeAndMethodImport_importShouldAppearOnlyOnceIfTheirSimpleNamesAreEqual
mvn edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=spoon.test.imports.ImportTest#staticImports_ofNestedTypes_shouldBeRecorded

Please let me know if you have any questions or would like to discuss this further for any clarificaitions.

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.

1 participant