Skip to content

test: Fixed nondeterministic failures in factoryTest() #6494

Open
yonghanlin wants to merge 1 commit intoINRIA:masterfrom
yonghanlin:fix/factorytest-nondeterminism
Open

test: Fixed nondeterministic failures in factoryTest() #6494
yonghanlin wants to merge 1 commit intoINRIA:masterfrom
yonghanlin:fix/factorytest-nondeterminism

Conversation

@yonghanlin
Copy link
Contributor

What does this PR do?

This PR fixes a nondeterministic test failure in FactoryTest.factoryTest() within . module when running with NonDex.

Problem

The nondeterminism occurs because the test randomly generates arguments for every create*() method. These arguments come from unordered collections and mixed element types, so their structure can change between runs.

Observed failures include:

  • Invalid pattern nodes (CtTypePattern, CtUnnamedPattern) used outside of valid pattern-matching contexts (e.g., instanceof or switch).
  • Null declaringType in CtField arguments, causing errors in methods like createVariableReads() or createVariableAssignments().
  • Illegal record hosts (CtRecord) being used for constructs that records cannot legally contain.

Reproduce Test

To reproduce the failure, run NonDex on . module using the following commands:

mvn -pl . edu.illinois:nondex-maven-plugin:2.1.7:nondex -Dtest=spoon.test.factory.FactoryTest#factoryTest

The Fix

The fix introduces argument normalization before invoking each factory method:

  1. Literal Replacement: Replace invalid pattern nodes (CtTypePattern, CtUnnamedPattern) with a harmless literal (CtLiteral(0)), which is a valid expression in all contexts.
  2. Declaring Type Enforcement: If any CtField lacks a declaringType, attach it to a synthetic dummy host class (TmpHost_*) and ensure it has a valid type and simpleName.
  3. Deep Normalization: Recursively traverse lists, arrays, and collections to normalize their contained elements (clone already-parented elements and replace illegal nodes).
  4. Record Substitution: Replace CtRecord arguments with dummy classes when the target method requires a host that supports instance members.
  5. Context-Specific Fixes: Special handling for createVariableReads and createVariableAssignments ensures that every nested field reference has a valid host.

@yonghanlin yonghanlin changed the title fix: FactoryTest nondeterministic failures by normalizing invalid arguments fix: Prevent nondeterministic failures behavior in FactoryTest() Oct 24, 2025
@yonghanlin yonghanlin changed the title fix: Prevent nondeterministic failures behavior in FactoryTest() fix: Prevent nondeterministic failures behavior in factoryTest() Oct 24, 2025
@yonghanlin yonghanlin changed the title fix: Prevent nondeterministic failures behavior in factoryTest() test: Fixed nondeterministic failures in factoryTest() Oct 29, 2025
@yonghanlin yonghanlin changed the title test: Fixed nondeterministic failures in factoryTest() review: test: Fixed nondeterministic failures in factoryTest() Oct 29, 2025
@yonghanlin yonghanlin changed the title review: test: Fixed nondeterministic failures in factoryTest() test: Fixed nondeterministic failures in factoryTest() Nov 19, 2025
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