Skip to content

Fix flaky test by acking messages asap in pubsub test#39183

Merged
shunping merged 1 commit into
apache:masterfrom
shunping:fix-pubsub-test
Jul 1, 2026
Merged

Fix flaky test by acking messages asap in pubsub test#39183
shunping merged 1 commit into
apache:masterfrom
shunping:fix-pubsub-test

Conversation

@shunping

@shunping shunping commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

This PR resolves a flakiness issue in test_batch_write_with_ordering_key by acknowledging pulled messages immediately inside the retry loop instead of waiting until the loop finishes.

Follow-up to #39134 and addressing the previous comment #39134 (comment)

Failed run:
https://github.com/apache/beam/runs/84438206754

Traceback:

self = <apache_beam.io.gcp.pubsub_integration_test.PubSubIntegrationTest testMethod=test_batch_write_with_ordering_key>

    @pytest.mark.it_postcommit
    def test_batch_write_with_ordering_key(self):
      """Test WriteToPubSub in batch mode with ordering keys.
    
  ...
        # Retry pulling to handle PubSub delivery delays
        received_messages = []
        received_message_ids = set()
        ack_ids = []
        deadline = time.time() + 60  # wait up to 60 seconds
        while time.time() < deadline:
          response = self.sub_client.pull(
              request={
                  'subscription': ordering_sub.name,
                  'max_messages': 10,
              })
          for msg in response.received_messages:
            ack_ids.append(msg.ack_id)
            # Pub/Sub guarantees at-least-once delivery, so we must deduplicate
            # messages by message_id to handle potential duplicate deliveries.
            if msg.message.message_id not in received_message_ids:
              received_message_ids.add(msg.message.message_id)
              received_messages.append(msg)
          if len(received_messages) >= len(test_messages):
            break
          time.sleep(5)
    
>       self.assertEqual(len(received_messages), len(test_messages))
E       AssertionError: 2 != 3

apache_beam/io/gcp/pubsub_integration_test.py:385: AssertionError

@shunping

shunping commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator Author

r: @derrickaw

@shunping shunping marked this pull request as ready for review July 1, 2026 13:47
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! 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 flakiness issue in the Pub/Sub integration tests where batch writes with ordering keys were failing due to message delivery delays. By modifying the test logic to acknowledge messages as soon as they are pulled, the PR prevents outstanding messages from blocking the delivery of subsequent messages sharing the same ordering key, thereby stabilizing the test execution.

Highlights

  • Immediate Message Acknowledgment: Updated the Pub/Sub integration test to acknowledge messages immediately upon receipt within the retry loop.
  • Ordering Key Block Prevention: Ensured that unacknowledged messages do not block the delivery of subsequent messages with the same ordering key, which was causing intermittent test failures.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 the 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 counterproductive. You can react with 👍 and 👎 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.

Footnotes

  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 gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Pub/Sub integration test test_batch_write_with_ordering_key to acknowledge received messages immediately within the pull loop instead of at the end of the test. This change ensures that outstanding unacknowledged messages do not block the delivery of subsequent messages with the same ordering key. There are no review comments, and I have no additional feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment assign set of reviewers

@shunping shunping merged commit d0d754c into apache:master Jul 1, 2026
103 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants