#3940 Fix the non-determinism by modifying file assertion logic #3941
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.
Summary
This pull request addresses non-deterministic behavior in the processor module, such as
Java8CustomPatternDateTimeFormatterGeneratedTest, which fail intermittently due to non-deterministic field ordering in the generated code. This behavior was identified using the NonDex tool.The Root Cause
The core issue is that the code generation process does not guarantee a stable order for helper fields (e.g.,
DateTimeFormatterinstances). This is because the processor iterates over a list of methods, which are stored in HashSet initially, in an order that can vary between builds. As a result, the generated mapper implementation can have its fields declared in different sequences, causing tests that perform a direct string comparison against a "golden file" to fail.The Implemented Solution
This PR makes the test suite more robust by changing the assertion logic rather than the code generation logic itself.
The
JavaFileAssertutility has been modified to handle non-deterministic field order. The new assertion logic works as follows:This approach ensures that the test correctly verifies the generated code's content and structure while being resilient to changes in field ordering.
Alternative Considered
An alternative approach would be to make the code generation itself deterministic by sorting the list of methods in
MethodRetrievalProcessorbefore processing. This PR takes the approach of hardening the test assertion as it is a more localized change that directly addresses the test's flakiness without modifying the core processor's behavior.Issue related: #3940