Repository navigation
test: set the test Main dispatcher before every spec callback - #256
Conversation
Kotest runs spec beforeEach and beforeContainer callbacks before a BeforeTestListener, so a ViewModel built there found no Main and its viewModelScope fell back to Dispatchers.Default. A TestCaseExtension wraps every callback of a top-level test; it resets Main only after that test, so nested tests and after-callbacks still see the dispatcher.
MainDispatcherExtension now sets Main before beforeEach, so the specs drop their own setMain and resetMain. The OOBE spec builds its dispatcher in beforeEach: a TestDispatcher created there shares the scheduler of the test Main, while one created in the spec body gets its own scheduler and no longer advances the ViewModel's delay.
The spec holds the scheduler of the test Main dispatcher, taken in beforeEach, and advances it directly, so the dependency on Main's scheduler shows in the code.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (7)
💤 Files with no reviewable changes (3)
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe test suite replaces its per-test Main dispatcher listener with a Kotest test-case extension. New tests cover dispatcher behavior across test lifecycles, and ViewModel tests remove local Main dispatcher setup. ChangesMain dispatcher test setup
Priority: ⬇️ Low Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Other Merge Risk: ⚪ Minimal · up to The shared dispatcher setup covers nested ViewModel tests, with no identified issue requiring a fix before merge. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change is confined to test infrastructure, and no new application attack path was identified. The remaining risk is that overlapping tests could interfere with each other’s dispatcher state; whether such overlap occurs is not established. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks the dispatcher’s pace, Comment |
Plan limit reachedYou've used all Starter plan credits for this billing period (70 limit), so this pull request was not reviewed ( Upgrade to a higher plan or wait until your next billing period: See plans on the billing page |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Summary
TestDispatcherListenerwithMainDispatcherExtension, a projectTestCaseExtensionregistered inProjectConfig. Kotest 6.2.5 runs a spec'sbeforeContainer/beforeEachcallbacks before a project listener'sbeforeTest. The old listener therefore setDispatchers.Maintoo late.beforeEachfound no Main when it touchedviewModelScope. Lifecycle then falls back toDispatchers.Default, so its launches raced the test.SettingsViewModelTest's ViewModel now runs on the test thread instead ofDefaultDispatcher-worker-2, with no spec edit.AboutViewModelTest,AppPickerViewModelTest,SwitchBarViewModelTestandOOBEViewModelTestdrop their ownsetMain/resetMain.OOBEViewModelTestnames the Main scheduler it advances and builds it inbeforeEach. A dispatcher built in the spec body gets its own scheduler, so the ViewModel's delay never advances and the test times out.Behavior changes
None in app code. This PR changes test infrastructure only.
Coverage
MainDispatcherExtensionTestcoversbeforeEach,beforeContainer,afterContainer, a nested test, and a Main installed in a container body. ItsafterSpecasserts that Main is reset.Verification
spotlessCheck detekt lintDebug testDebugUnitTest koverVerifyDebug verifyRoborazziDebug: PASS, 535 tests.Notes
beforeSpecandafterSpecrun outside anyTestCaseExtension. The extension KDoc states this limit.38b5371test: set the test Main dispatcher before spec callbacks5604a93test: rely on the project Main dispatcher in ViewModel specs987389ftest: name the Main scheduler in the OOBE spec🤖 Generated with Claude Code
https://claude.ai/code/session_01UXgoU8egK3DzNLho2znqJz
Replace
TestDispatcherListenerwith a project-wideMainDispatcherExtensionthat setsDispatchers.Mainfor root tests and resets it after execution. Add lifecycle and nested-test coverage. Remove redundant per-test Main dispatcher setup from four ViewModel test suites.