Skip to content

Conversation

ffranr
Copy link
Contributor

@ffranr ffranr commented Sep 26, 2025

This PR makes several changes to stabilize MockKeyRing and reduce test flakiness in queueSeedlingsInBatch:

  • Fix incorrect return values in NewMockKeyRing On calls.
  • Replace RLock with full mutex locks, always acquired first in each method.
  • Add an atomic counter for DeriveNextKey calls to avoid depending on the mock’s mutex.
  • Update the test helper to use wait.NoError instead of require.Eventually and relax equality checks to >= expected.

This is the error we're trying to fix (as seen in a recent CI run):

--- FAIL: TestBatchedAssetIssuance (87.41s)
    --- FAIL: TestBatchedAssetIssuance/minting_with_cancellation (61.97s)
        test_sqlite.go:11: Creating new SQLite DB handle for testing: /tmp/TestBatchedAssetIssuanceminting_with_cancellation1885226529/001/tmp.db
        planter_test.go:313: 
                Error Trace:    /home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:313
                                            /home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:1313
                                            /home/runner/work/taproot-assets/taproot-assets/tapgarden/planter_test.go:2051
                Error:          Condition never satisfied
                Test:           TestBatchedAssetIssuance/minting_with_cancellation

Copy link

Summary of Changes

Hello @ffranr, 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 test flakiness in the tapgarden package, specifically within the queueSeedlingsInBatch test, by enhancing the stability and reliability of the MockKeyRing. The changes involve correcting mock return values, implementing more robust locking mechanisms, and introducing an atomic counter for key derivation calls. These improvements aim to eliminate race conditions and provide more accurate and consistent test assertions, ultimately leading to a more stable CI environment.

Highlights

  • MockKeyRing Hardening: The MockKeyRing implementation has been made more robust by correctly initializing return values for mocked methods and switching from read-locks (RLock) to full mutex locks (Lock) in several methods to prevent potential race conditions.
  • Atomic Counter for Key Derivation: An atomic counter (deriveNextKeyCallCount) was introduced in MockKeyRing to accurately track calls to DeriveNextKey, decoupling this count from the mock's internal call history and its mutex.
  • Flaky Test Fix: The queueSeedlingsInBatch test in planter_test.go was refactored to use the new atomic counter for key derivation calls and wait.NoError instead of require.Eventually. The assertion now checks for >= expected calls, making the test more resilient to timing variations.
  • Improved Test Helper: The test helper now explicitly resets the key derivation call count before each seedling processing, ensuring accurate tracking within the test loop.
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 👍 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.

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.

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.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

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 effectively addresses the flaky test TestBatchedAssetIssuance/minting_with_cancellation by hardening the MockKeyRing and refining the test logic. The introduction of an atomic counter for DeriveNextKey calls is a robust solution to avoid race conditions in test assertions. Replacing read locks with full mutex locks provides stronger consistency guarantees for the mock object. The test helper queueSeedlingsInBatch is also improved by using wait.NoError, which is more suitable for the asynchronous nature of the test.

My review includes a couple of suggestions to enhance the comments for the new exported functions in tapgarden/mock.go to better align with the project's documentation standards.

Replace read-only locks with full mutex locks in MockKeyRing methods
for simplicity. Ensure the full lock is acquired as the first operation
in each method to maintain consistent and safe access.
Introduce an atomic counter `deriveNextKeyCallCount` in `MockKeyRing` to
track calls to `DeriveNextKey` without relying on the internal mock
mutex. This allows tests to query and reset the count directly in a
thread-safe way.

Update `queueSeedlingsInBatch` to:
* replace `require.Eventually` with `wait.NoError`, so failures log the
  actual state for easier debugging,
* relax the check from strict equality to `>=` expected, tolerating
  extra key derivations and reducing flakiness.
@ffranr ffranr force-pushed the wip/fix-flake-queueSeedlingsInBatch branch from ff09d60 to e28ce8d Compare September 26, 2025 13:45
Replace the use of `require.Eventually` with `wait.NoError` in
`assertPendingBatchExists`. This lets the helper return detailed error
messages (nil batch, wrong number of seedlings, fetch failure) instead
of a generic boolean failure.
Copy link
Member

@jtobin jtobin left a comment

Choose a reason for hiding this comment

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

LGTM. 👍

Copy link
Member

@GeorgeTsagk GeorgeTsagk left a comment

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation bot moved this from 🏗 In progress to 👀 In review in Taproot-Assets Project Board Sep 30, 2025
@GeorgeTsagk GeorgeTsagk added this pull request to the merge queue Sep 30, 2025
Merged via the queue into main with commit e85e942 Sep 30, 2025
19 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

3 participants