Comments (5)
There's a potential for the documentation to be improved, but yes, it's not enough to replace the Main
dispatcher with a TestDispatcher
: someone has to actually execute the tasks on that dispatcher (that is, something should emulate the event loop of the main thread), and runTest
is the one doing it. It's certainly not enough to just override the main dispatcher, even if your coroutines don't throw exceptions: some tasks spawned on the main thread just won't run.
In theory, you can use a TestDispatcher
with a TestCoroutineScheduler
separately from runTest
, but this is a very advanced use case that requires a deep understanding, so their docs certainly should mention runTest
.
from kotlinx.coroutines.
Would there be a way to detect if a test is not using runTest
when required, and throw an exception, so it's immediately clear that this is missing?
(Or an existing test needs to be updated, after a change)
I don't know if the TestDispatcher
can perhaps check if it's running in a TestScope
, for example?
from kotlinx.coroutines.
Unfortunately, no: injecting a TestDispatcher
before a TestScope
exists or runTest
is run is an important pattern. Dependency injection is typically initialized before the test is started.
from kotlinx.coroutines.
Ah yes, I understand now.
Classes create their own / use another CoroutineScope.
There's no link between the TestScope and the coroutines launched from it's scope.
I've found that apparently there's a Detekt rule to check if tests which interact with coroutines make use of runTest
.
Will need to try this out.
from kotlinx.coroutines.
It's safe to assume that, as a general rule of thumb:
all of the tests in a test class which replaces the Main scheduler; and injects the TestDispatcher for all of the other dispatchers; should use runTest
, to avoid running into potential (future) issues, even if the test itself is not directly interacting with a coroutine / using the time methods, right?
So even if a test just calls a method which is not launching any coroutine at all, it would be safer to just use runTest
regardless?
from kotlinx.coroutines.
Related Issues (20)
- BlockHound false positive in kotlin.jvm.internal.Reflection.renderLambdaToString HOT 3
- java.lang.NullPointerException: Cannot invoke "kotlinx.coroutines.flow.Flow.collect when bumping up coroutines 1.6.4 to 1.8.1 HOT 2
- Improve the API reference HOT 3
- Coroutine on EventLoop dispatcher fails to yield to a task which is resuming after a delay HOT 5
- TestScope swallows an exception thrown from `launch` outside `runTest` HOT 4
- Flow collection silently hangs when dispatcher throws an exception
- Suggestion for a potential new Flow's `timeout` extension HOT 2
- Mutex is unlocked on cancellation
- Crash on GraalVM at `1.9.0-RC` HOT 7
- Non-linearizable behavior in `cancel` + `awaitClose` inside of `produce` HOT 1
- Inherit from `kotlinx.coroutines` warning even with interface delegation ? HOT 1
- Consider stabilizing `CoroutineStart.ATOMIC`
- 1.9.0-RC: `kotlinx-coroutines-core/jvm/src/internal/ProbesSupport.kt` calls ` kotlinx.coroutines.debug.internal.probeCoroutineResumed` HOT 1
- 1.9.0-RC: DispatchersToStringTest.testLimitedParallelism fails when CORE_POOL_SIZE == 2 HOT 3
- CancellableContinuation.invokeOnCancellation should accept a suspend callback HOT 1
- ActorCoroutine with ConflatedBufferedChannel capactiy
- Calling CompletionStage.asDeferred() on the same future many times causes stack overflows HOT 2
- [Performance impacting] Upgrade Android Build rules to use standard android library Gradle plugin
- Flow.catch skips upstream exceptions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.coroutines.