review: test: Fixed nondeterministic failures in ContractOnSettersParametrizedTest.data() #6499
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?
This PR fixes a nondeterministic test failure in
ContractOnSettersParametrizedTest.data()when running with NonDex.Problem
ContractOnSettersParametrizedTestbuilds a list of (receiver, setter, argument) combinations by iterating over unordered collections. When NonDex randomizes the iteration order, some seeds produce combinations where a setter is invoked with an argument that is type-compatible at the Java level but invalid according to Spoon’s API. When those combinations are reached, the reflective invocation fails and the test becomes nondeterministicIn these cases:
IMPORT_REFERENCE:
CtImport.setReference()has a parameter of typeCtReference, but Spoon only accepts four specific subtypes:CtTypeReference,CtExecutableReference,CtFieldReference, andCtPackageReference. The argument generator can produce otherCtReferencesubtypes, which are assignable toCtReferencebut not accepted by Spoon when invoked. When NonDex orders the candidates such thatCtImport.setReference()is called with one of these unsupported subtypes, the reflective call violates the contract and the test fails.Pattern arguments: For
CtCasePattern.setPattern(), the parameter type allows patterns in general, so the generator may produce aCtUnnamedPatternas the argument. However,CtCasePatterncannot directly take aCtUnnamedPattern; such patterns are only valid when nested inside a record pattern. When NonDex produces an ordering whereCtCasePattern.setPattern()is invoked with aCtUnnamedPattern, the reflective call is invalid and throws during execution, causing the test to fail for some seeds.Reproduce Test
To reproduce the failure, run NonDex on
.module using the following commands:The Fix
Refined type handling for CtReference: Replaced the broad check
isAssignableFrom(CtReference.class)with an exact-type matchequals(CtReference.class)increateCompatibleObject(). In addition, added precise constructors forCtReferencesubtypes.Validated IMPORT_REFERENCE arguments: Before invoking
CtImport.setReference(), ensured the argument is one of the four valid reference types.Validated Pattern arguments: Skipped invalid invocations of
CtCasePattern.setPattern()withCtUnnamedPattern.Stabilized Set iteration: Replaced
HashSetcreation withLinkedHashSetto preserve element order and remove nondeterminism during argument generation.