dataconnect(test): Fix thread thrashing by using Dispatchers.IO in tests#8391
dataconnect(test): Fix thread thrashing by using Dispatchers.IO in tests#8391dconeybe wants to merge 6 commits into
Conversation
… Dispatchers.IO and Dispatchers.Default
…chers.IO to avoid creating hundreds of threads
…hers.IO to avoid creating unnecessary threads
Using Gemini Code AssistThe 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
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 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. |
|
/gemini review |
There was a problem hiding this comment.
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.
📝 PRs merging into main branchOur 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. |
This PR resolves thread thrashing and resource overhead in Firebase Data Connect unit and instrumentation tests by replacing hardcoded
Dispatchers.IOwithDispatchers.Defaultor test-scopedStandardTestDispatcher. Additionally, it introducescleanupsScope, a coroutine-friendly test resource cleanup utility to manage synchronous and suspending close routines safely.Highlights
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.Dispatchers.IOandDispatchers.DefaultinDataConnectGrpcRPCsUnitTestwithStandardTestDispatcherbound to the test scheduler, ensuring deterministic execution and preventing thread leaks.Dispatchers.IOwithDispatchers.DefaultinDataConnectAuthUnitTestandTestFirebaseAppFactoryto limit maximum concurrent threads, avoiding thread thrashing when spawning hundreds of background tasks.Changelog
suspendingClose()to support suspending cleanup actions within a coroutine context.CleanupsScopeinterface and its implementationCleanupsScopeImplto provide a scope for registering synchronous and suspending cleanup tasks.cleanupsScopetop-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.cleanupsScopebehavior, including return values, block exception propagation, and proper suppression of exceptions thrown during cleanup.cleanupsScopeinstead of manually closing resources withCleanups().use.newDataConnectGrpcRPCsandcacheArbto accept aTestCoroutineSchedulerand useStandardTestDispatcherfor both blocking and non-blocking coroutines, making the tests more deterministic and eliminating thread-leaks.Dispatchers.IOwithDispatchers.Defaultfor launching concurrent background tasks to prevent thread thrashing.latch.run { countDown(); await() }tolatch.countDown().await().Dispatchers.IOwithDispatchers.Defaultfor launching the delayed delete task ofFirebaseApp.