Make PaywallViewModel dispatcher injectable to fix flaky tests#3544
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3544 +/- ##
=======================================
Coverage 80.11% 80.11%
=======================================
Files 371 371
Lines 15166 15166
Branches 2100 2100
=======================================
Hits 12150 12150
Misses 2166 2166
Partials 850 850 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vegaro
marked this pull request as ready for review
June 5, 2026 08:57
…ky workflow tests preWarmWorkflowStepCache launched on viewModelScope (the test Main dispatcher in tests) but hopped to withContext(Dispatchers.Default) for the heavy computeStateForStep work. Dispatchers.Default is a real thread pool the StandardTestDispatcher's scheduler doesn't control, so advanceUntilIdle() never awaited it. The coroutine then resumed back on the test Main after the test had finished and @after resetMain() had unset it, throwing IllegalStateException from TestMainDispatcher and surfacing at the resetMain() call. A timing/order race, hence the flakiness in PaywallViewModelWorkflowTest. Make the dispatcher injectable (backgroundDispatcher: CoroutineDispatcher = Dispatchers.Default), mirroring AppStyleExtractor, and inject the test dispatcher in the workflow test's VM construction so the pre-warm runs on the test scheduler and advanceUntilIdle() drains it. No production behavior change: the default stays Dispatchers.Default. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
vegaro
force-pushed
the
cesar/wfl-fix-prewarm-dispatcher-flake
branch
from
June 5, 2026 08:57
eb46f70 to
a99fef5
Compare
facumenzella
approved these changes
Jun 5, 2026
facumenzella
left a comment
Member
There was a problem hiding this comment.
There isn't any clear way to make sure this addresses the flamines right? It would be great to link the failures to the PR description, so we can track this back
Member
Author
|
added the failures @facumenzella |
vegaro
enabled auto-merge
June 5, 2026 09:16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PaywallViewModelWorkflowTestlooks flaky on CII believe the reason is that
preWarmWorkflowStepCache()launches onviewModelScope(the test Main dispatcher) but hops towithContext(Dispatchers.Default)for the heavycomputeStateForStepwork.Making the dispatcher injectable and injecting the test dispatcher in the workflow test's VM construction so the pre-warm runs on the test scheduler should fix it.
Example failure:
https://app.circleci.com/pipelines/github/RevenueCat/purchases-android/22653/workflows/97ebb0de-5850-4621-ae49-725e31304259/jobs/223710/tests
Failure in
PaywallViewModelWorkflowTestNote
Low Risk
Production behavior is unchanged thanks to the default dispatcher; only test construction and an internal injectable hook for pre-warm offloading.
Overview
Adds an optional
backgroundDispatcherconstructor parameter toPaywallViewModelImpl(defaultDispatchers.Default) and uses it inpreWarmWorkflowStepCacheinstead of a hardcodedDispatchers.DefaultforcomputeStateForStep.PaywallViewModelWorkflowTestpasses the testStandardTestDispatcherso workflow step pre-warming stays on the test coroutine scheduler. That avoids work continuing on the real default pool afterDispatchers.resetMain(), which was causing flaky CI failures. Production wiring viaPaywallViewModelFactoryis unchanged and still uses the default dispatcher.Reviewed by Cursor Bugbot for commit a99fef5. Bugbot is set up for automated code reviews on this repo. Configure here.