-
Notifications
You must be signed in to change notification settings - Fork 1.9k
skip delays #3094
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
Comments
Also, the motivation for skipping delays is not clear. If the delay is too long or will create new threads, then these are the problems of those who use it |
Hi! In fact, the main point of the test framework is to skip delays. The motivation for this is simple: it is done so that tests don't take too long to complete, but instead finish immediately. The expectation is that every component of the system is mocked for testing. For example, it's common to avoid connecting to a real server from tests but instead, use a fake "server" returning pre-defined responses. Delays are skipped only in the This is also described in the documentation, take a look if you're interested: https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-test/README.md Also, please use the public Slack channel or Stack Overflow for such general questions. |
@dkhalanskyjb I think this was true when the For me the main reason for the switch is to get rid of my poor custom implementation for iOS suspending tests: Parts of my tests for this websocket library are using an Autobahn Test Suite server (which is a fast but real server in another process), and I'm seeing instant timeout failures there because of the delay skipping. This could be a problem for any sort of multiplatform integration test using coroutines. I have quickly tried the mentioned workaround for JVM and JS tests and indeed wrapping all such tests this way seems to work: @Test
fun test() = runTest {
withContext(Dispatchers.Default) {
TODO("test code here")
}
} But I don't believe this will work in iOS tests - I had to use the main thread and go through all this trouble before. Also it means I'm using a multithreaded dispatcher on JVM, so I'll need to put more thought into it. Overall, I think it would be nice if this was supported by default by the library so we wouldn't have to still write multiplatform helpers, that are probably poorly implemented. For instance ...or we could have a |
@joffrey-bion I implement WS client and server using sockets for k/n (native, jvm) :) @dkhalanskyjb Strange decision. Long test execution is a problem for those who write them. It seems to me that your solution might help in extreme rare cases. For example, a delay before the response of a function ... but why is it? |
@caffeine-mgn I disagree here. Most delays that are used for schedulers and timeouts of many sorts are very nicely testable with the controlled time approach given by The only problem I have with it is what I mentioned above: when you need to interact with something that is not time-controlled during your test, the delay skipping just breaks the behaviour. |
Could you elaborate on this?
If you don't want to use a multithreaded dispatcher, you can choose not to: @Test
fun test() = runTest {
withContext(Dispatchers.Default.limitedParallelism(1)) {
TODO("the test code here will run in a single thread")
}
} Any dispatcher not linked to a
If you don't mock
I think such a parameter would only be very slightly less cumbersome than forking off to a different dispatcher that doesn't know about the virtual time, and is much less flexible: one could conceivably only need a part of the test to respect the delays. |
I would have to check how this works with the new memory model (I don't have a mac, so testing those iOS tests has a very slow feedback loop for me using the CI), but essentially the issue boiled down to the combination of 2 things:
So I initially wrote my test util using Which in the end I simplified this way: But for some reason with coroutines 1.6.0 (non
Which I'm sure I had encountered when doing trial and error on this iOS test util, but this last approach worked fine in
Then you probably are consuming the main loop as well, concurrently with the test body, right?
When writing common code, if something needs me to write platform specific code with expect-actual just for this This is why I was waiting for |
Here is another example of someone needing to run real-time things in their tests, and failing to do so using |
How I can disable delays skiping in tests?
I trying to write network library for kotlin-common (https://github.com/caffeine-mgn/pw.binom.io/tree/master/network). And I shoud real wait until client connect to server. Network work in other thread.
The text was updated successfully, but these errors were encountered: