DataflowTemplates icon indicating copy to clipboard operation
DataflowTemplates copied to clipboard

Fix flaky test in FirestoreConvertersTest caused by nondeterministic Json message

Open LucaDai opened this issue 2 months ago • 5 comments

Fix Issue: https://github.com/GoogleCloudPlatform/DataflowTemplates/issues/2901 Similar fix before: https://github.com/GoogleCloudPlatform/DataflowTemplates/pull/1176

Root Cause

The flaky behavior was detected by NonDex when running com.google.cloud.teleport.v2.utils.FirestoreConvertersTest.

This test originally compared JSON strings directly using strict string equality inside PAssert.that(...).containsInAnyOrder(expectedErrors).

However, the generated JSON strings were created through serialization of objects such as ErrorMessage and EntityJsonPrinter, which internally rely on non-deterministic field ordering in Map implementations (e.g., HashMap) during JSON construction. Because of this, the same logical JSON data could appear in different field orders across runs:

Run 1: {"message":"Datastore Entity Without Key","stacktrace":null,"data":"{\"properties\":{\"street\":{\"stringValue\":\"Some street\"},\"number\":{\"integerValue\":\"1\"}}}"}
Run 2: {"data":"{\"properties\":{\"street\":{\"stringValue\":\"Some street\"},\"number\":{\"integerValue\":\"1\"}}}","message":"Datastore Entity Without Key","stacktrace":null}
}

Since the test asserted equality on the raw string representation, it failed whenever the JSON fields appeared in a different order, even though the logical content was identical.

This issue is not specific to NonDex—it indicates the test’s sensitivity to iteration order during JSON serialization.

Fix

Update the test to compare the semantic JSON content instead of relying on string order: • Parse both the actual and expected JSON strings using JsonParser.parseString(...). • Compare them as structured JSON objects (JsonObject), ensuring equivalence regardless of key ordering. • Keep PAssert.that(...).satisfies(...) to validate all outputs while allowing flexible order matching. This fix ensures deterministic validation across JVM runs and environments while preserving full logical equivalence of JSON data.

LucaDai avatar Oct 20 '25 22:10 LucaDai

Summary of Changes

Hello @LucaDai, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a persistent flakiness issue in the FirestoreConvertersTest suite. The problem stemmed from tests directly comparing JSON strings, which could vary in field order due to non-deterministic serialization. The solution involves enhancing the test logic to perform semantic JSON comparisons, ensuring that the tests validate the actual data content rather than its incidental string representation. This change significantly improves test reliability and stability.

Highlights

  • Flaky Test Fix: Addressed a persistent flaky test in FirestoreConvertersTest that was failing due to non-deterministic JSON field ordering during serialization, even when the logical content was identical.
  • Semantic JSON Comparison: Updated test assertions to compare JSON content semantically by parsing strings into JsonObjects, ensuring tests pass regardless of the order of fields within the JSON.
  • Dependency Update: Introduced com.google.gson.JsonParser to facilitate robust JSON parsing within the test suite, enabling object-level comparison.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Oct 20 '25 22:10 gemini-code-assist[bot]

/gemini review

LucaDai avatar Oct 20 '25 22:10 LucaDai

/gemini review

LucaDai avatar Oct 20 '25 22:10 LucaDai

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 50.04%. Comparing base (87c6fe9) to head (a537295). :warning: Report is 12 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2902      +/-   ##
============================================
- Coverage     50.07%   50.04%   -0.04%     
- Complexity     4969     4970       +1     
============================================
  Files           967      967              
  Lines         59298    59329      +31     
  Branches       6441     6448       +7     
============================================
- Hits          29693    29690       -3     
- Misses        27501    27532      +31     
- Partials       2104     2107       +3     
Components Coverage Δ
spanner-templates 70.44% <ø> (-0.06%) :arrow_down:
spanner-import-export 68.93% <ø> (-0.16%) :arrow_down:
spanner-live-forward-migration 79.69% <ø> (ø)
spanner-live-reverse-replication 77.42% <ø> (ø)
spanner-bulk-migration 88.21% <ø> (ø)
see 8 files with indirect coverage changes
:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar Oct 23 '25 14:10 codecov[bot]

Hi @Abacn, Thanks for labeling this PR! 🙏 I've fixed the spotless issue. Could you please also help review it (or assign the right reviewer) when you have time?

LucaDai avatar Oct 24 '25 00:10 LucaDai