Skip to content

dataconnect(test): Fix thread thrashing by using Dispatchers.IO in tests#8391

Draft
dconeybe wants to merge 6 commits into
mainfrom
dconeybe/dataconnect/TestDispatchers
Draft

dataconnect(test): Fix thread thrashing by using Dispatchers.IO in tests#8391
dconeybe wants to merge 6 commits into
mainfrom
dconeybe/dataconnect/TestDispatchers

Conversation

@dconeybe

@dconeybe dconeybe commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This PR resolves thread thrashing and resource overhead in Firebase Data Connect unit and instrumentation tests by replacing hardcoded Dispatchers.IO with Dispatchers.Default or test-scoped StandardTestDispatcher. Additionally, it introduces cleanupsScope, a coroutine-friendly test resource cleanup utility to manage synchronous and suspending close routines safely.

Highlights

  • Coroutine-Aware Cleanup Scope: Introduced cleanupsScope, a coroutine-aware utility for registering and safely executing synchronous and suspending test cleanups. It ensures proper exception propagation and suppression when block or cleanup operations fail.
  • Test Dispatcher Alignment: Replaced hardcoded Dispatchers.IO and Dispatchers.Default in DataConnectGrpcRPCsUnitTest with StandardTestDispatcher bound to the test scheduler, ensuring deterministic execution and preventing thread leaks.
  • Thread Thrashing Avoidance: Replaced Dispatchers.IO with Dispatchers.Default in DataConnectAuthUnitTest and TestFirebaseAppFactory to limit maximum concurrent threads, avoiding thread thrashing when spawning hundreds of background tasks.
Changelog
  • Cleanups.kt
    • Added suspendingClose() to support suspending cleanup actions within a coroutine context.
    • Added CleanupsScope interface and its implementation CleanupsScopeImpl to provide a scope for registering synchronous and suspending cleanup tasks.
    • Added the cleanupsScope top-level builder to run a block of test code and guarantee execution of registered cleanups, ensuring suppressed exceptions are added to any thrown block exception.
  • CleanupsUnitTest.kt
    • Added unit tests verifying cleanupsScope behavior, including return values, block exception propagation, and proper suppression of exceptions thrown during cleanup.
  • DataConnectGrpcRPCsUnitTest.kt
    • Refactored tests to use the new cleanupsScope instead of manually closing resources with Cleanups().use.
    • Updated newDataConnectGrpcRPCs and cacheArb to accept a TestCoroutineScheduler and use StandardTestDispatcher for both blocking and non-blocking coroutines, making the tests more deterministic and eliminating thread-leaks.
  • DataConnectAuthUnitTest.kt
    • Replaced Dispatchers.IO with Dispatchers.Default for launching concurrent background tasks to prevent thread thrashing.
    • Simplified latch execution syntax from latch.run { countDown(); await() } to latch.countDown().await().
  • TestFirebaseAppFactory.kt
    • Replaced Dispatchers.IO with Dispatchers.Default for launching the delayed delete task of FirebaseApp.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
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.

@dconeybe

dconeybe commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 introduces a new cleanupsScope utility in Cleanups.kt to manage resource cleanup in tests, supporting suspending cleanups and proper exception suppression. It refactors DataConnectGrpcRPCsUnitTest to use this new scope, while also migrating tests to use StandardTestDispatcher with TestCoroutineScheduler instead of hardcoded dispatchers like Dispatchers.IO or Dispatchers.Default for better test reliability. Additionally, minor dispatcher updates and code simplifications were made in TestFirebaseAppFactory and DataConnectAuthUnitTest. There are no review comments, and I have no feedback to provide.

@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

📝 PRs merging into main branch

Our main branch should always be in a releasable state. If you are working on a larger change, or if you don't want this change to see the light of the day just yet, consider using a feature branch first, and only merge into the main branch when the code complete and ready to be released.

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.

1 participant