DataflowTemplates icon indicating copy to clipboard operation
DataflowTemplates copied to clipboard

Fix flaky test in DeadLetterQueueTest

Open LucaDai opened this issue 2 months ago • 2 comments

Fix Issue: https://github.com/GoogleCloudPlatform/DataflowTemplates/issues/2914 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.writer.DeadLetterQueueTest.

This test originally asserted partial JSON string containment using:

assertThat(dlq.rowContextToDlqElement(r1).getPayload())
    .contains(expectedDataWithSuccessfulConversion);

However, the generated JSON payloads within DeadLetterQueue#rowContextToDlqElement() are constructed using standard JSON serialization (via GSON / JSONObject), which does not guarantee deterministic field ordering. During serialization, additional metadata fields (e.g., metadata*) are dynamically inserted into a Map, and the iteration order of these keys may vary between JVM runs or environments.

As a result, the same logical payload could appear in different orders:

Run 1: {"timestamp_col":"1970-01-01T00:29:09.630376Z","new_quantity":"42"}
Run 2: {"_metadata_source_type":"mysql","timestamp_col":"1970-01-01T00:29:09.630376Z","new_quantity":"42","_metadata_table":"srcTable"}

Since the test relied on raw string matching, it failed whenever the key order changed, even though the underlying JSON content was semantically identical.

This issue is not specific to NonDex—it reflects an order-dependent assertion pattern that could also fail under normal JVM hash-seed variation.

Fix

Update the test to validate JSON semantics instead of raw string order: • Parse both the actual and expected payloads using JsonParser.parseString(...) and obtain JsonObject instances. • Assert equivalence of relevant fields (timestamp_col, new_quantity) instead of relying on the full serialized string. • Preserve the logic for verifying exception handling, but make the comparison robust to field order.

LucaDai avatar Oct 23 '25 16: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 addresses a flaky test in DeadLetterQueueTest.java. The changes replace string contains assertions with JsonObject assertions to ensure accurate and reliable verification of the payload content, thus reducing the chance of false negatives.

Highlights

  • Test Improvement: The PR modifies the DeadLetterQueueTest.java to improve the reliability of the test by using JsonObject for assertions instead of string contains.
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 23 '25 16:10 gemini-code-assist[bot]

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 55.33%. Comparing base (df0dbee) to head (46369c2). :warning: Report is 117 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2915      +/-   ##
============================================
+ Coverage     50.06%   55.33%   +5.27%     
+ Complexity     4973     1577    -3396     
============================================
  Files           967      462     -505     
  Lines         59329    25902   -33427     
  Branches       6448     2705    -3743     
============================================
- Hits          29703    14333   -15370     
+ Misses        27522    10701   -16821     
+ Partials       2104      868    -1236     
Components Coverage Δ
spanner-templates 71.35% <ø> (+0.85%) :arrow_up:
spanner-import-export ∅ <ø> (∅)
spanner-live-forward-migration 79.69% <ø> (ø)
spanner-live-reverse-replication 77.42% <ø> (ø)
spanner-bulk-migration 88.21% <ø> (ø)
see 524 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 18:10 codecov[bot]