New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a way to unset kotlinx.coroutines.DefaultDelay #3895
Comments
It only does this if the |
* trunk: Prepare next development version Prepare to release v0.6.1 Workaround Kotlin/kotlinx.coroutines#3895 Remove usage of collectLatest in BitmapCache
Unfortunately not because setting that flag to false will prevent screenshot tests from working. That flag is enabled at build time so that the main dispatcher can be used for scheduling tasks in screenshot tests. |
Please provide a simplified example of a "screenshot test" so we can work out together how to make it not rely on |
cc: @gamepro65 |
Sorry, I haven't been able to find time to create a demo project. However you should be able to checkout paparazzi's sample project and run the code mentioned here to reproduce the problem: |
I understand why tests hang/fail when you set |
The PR that introduced |
It does, thanks. Some background information:
val FRAME_DURATION: Duration = 11111.microseconds // 90 Hz refresh rate
// does delay skipping in the test dispatchers
kotlinx.coroutines.test.runTest {
// this block of code can be provided by paparazzi for convenience
val screen: StateFlow<ScreenState> = MutableStateFlow(renderScreen())
backgroundScope.launch {
while (true) {
delay(FRAME_DURATION)
screen.value = renderScreen()
}
}
launch {
delay(FRAME_DURATION * 30)
addAThingToScreen()
}
delay(FRAME_DURATION * 31)
validateThingOnScreen(screen.value)
} Something like that: you have delay-skipping, you execute normal-looking code that's time-aware, and the changes get reflected on the screen in time. However, paparazzi is not a library for which I'm responsible, so it's not my call. If @saket, I see you are active in the paparazzi's repository, so could you please forward this information to the responsible parties? |
👋 Thanks @dkhalanskyjb for the background on this flag it really helps. The missing piece for us was where to integrate our own Delay as the CoroutineContext used is abstracted away from consumers inside ComposeUi. Using this flag was convenient but agreed it isn't great for our end users. Took another look and found a better integration point which lets us ditch this flag. |
Good to know that you found a solution! |
On Android,
kotlinx.coroutines.initializeDefaultDelay()
chooses between aandroid.os.Looper
-backed dispatcher or a custom event loop depending on whetherDispatchers.Main.immediate
is available. It is an immutable value that can't be changed once it is initialized. This assumption made sense when it was initially implemented, as test sources were not expected to include a realLooper
implementation.However, this has changed with the introduction of JVM based screenshot testing with libraries such as paparazzi because they (transitively) ship with a fake implementation of
Looper
. This is causing unit tests that usedelay
to fail through out the module becausepaparazzi
can't unsetDefaultDelay
once it is set (more context).Can kotlin provide a way to unset or reinitialize
kotlinx.coroutines.DefaultDelay
to address this?The text was updated successfully, but these errors were encountered: