-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
System property to control BlockingChecker extension point for runBlocking #458
Conversation
aed938b
to
752f8d8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
752f8d8
to
94bbe9f
Compare
How can I use this in unit tests only? This broke all my robolectric tests. |
@PaulWoitaschek, execute |
Do you know how I can specify in my root gradle file how to disable this for just everything? Trying to figure that out for ever now. |
This needs to have a better solution. runBlocking is the base of all my unit tests. Maybe a dedicated method that disables this check? Like |
|
As said above use the system property. test {
systemProperty "kotlinx.coroutines.blocking.checker", "disable"
} |
Test doesn't exist. When I run my android unit tests, they get executed on the UI thread and all fail. |
I don't understand. Can you share an example? It may be simpler. |
Sure: If you run
If I add test {
} to my app/build.gradle it immediately fails with: In general I really dislike this change. When I add runBlocking on the main thread I have a really good reason to do so. For example I have functions that depends on However sometimes i just need to block for these 5ms because my state expects this for further processing. |
To set the property to run the test with
Unfortunately, this does not work when running inside Android Studio. To set in Android Studio: Android Studio runs the test using Java directly, that's why gradle properties doesn't work: @jcornaz the way you wrote works for Java modules, but not in Android ones. |
Another option is to override
|
I wrote an extension I now use in all my tests because I don't want to actually disable this check if its running. Then my implementation could fail while my tests pass. fun <T> runTest(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T
): T {
val original = System.getProperty(BLOCKING_CHECKER_PROPERTY_NAME)
System.setProperty(BLOCKING_CHECKER_PROPERTY_NAME, BLOCKING_CHECKER_VALUE_DISABLE)
return runBlocking(context) {
if (original == null) {
System.clearProperty(BLOCKING_CHECKER_PROPERTY_NAME)
} else {
System.setProperty(BLOCKING_CHECKER_PROPERTY_NAME, original)
}
block()
}
} However I strongly think this should be an opt-in. @elizarov |
Actually I would not be against an argument in runBlocking(failsInUI = false) {
// call suspending code
} It would still fails by default, but allowing users to block the UI when they really want to. (Of course it is bad practice, and should be avoided. But developers should be able to do it they want it / need it) |
I don't understand why now usages of |
There are some valid use cases to blocking main thread, such as code on app initialization (you probably want to use some existing suspend function) or for some cases like component destroy when you want explicitly block the main thread to save state or clean up some resources. Using |
@PaulWoitaschek @svenjacobs @tsuharesu @jcornaz We should automatically detect test environment (with mocked classes) and you will not have this problem in tests and there will be no need to mess with system properties for tests. Let's move that discussion to #464 |
Using
-Dkotlinx.coroutines.blocking.checker=disable
to ignore all installed blocking checkers.