kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Dispatchers.setMain not working for 1.6+ versions

Open akolubau opened this issue 3 years ago • 7 comments
trafficstars

Currently, using 1.5.2, everything works fine. After library update to 1.6.0 or 1.6.1 version, I'm receiving message Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used. Same message I receive, if I just remove @BeforeTest fun setup() { Dispatchers.setMain ... for 1.5.2. Tried to add MainCoroutineRule - no use.

What could be a problem?

akolubau avatar Apr 09 '22 03:04 akolubau

same here

gbusiri avatar Apr 11 '22 07:04 gbusiri

Hi! This could be caused by a version mismatch, that is, if the kotlinx-coroutines-core version was increased, but the kotlinx-coroutines-test version wasn't.

If this is not the case, please provide an example of a project where this issue reproduces so I could take a look at it.

dkhalanskyjb avatar Apr 11 '22 08:04 dkhalanskyjb

kotlinx-coroutines-core and kotlinx-coroutines-test using the same versions. Yeah it's different case i think

gbusiri avatar Apr 11 '22 09:04 gbusiri

kotlinx-coroutines-android , kotlinx-coroutines-core and kotlinx-coroutines-test updated all together. Unfortunately, I can't share the project. I'll try to create a new one and step by step add using libraries to find out the problem. Return back with the result, but not sure when.

akolubau avatar Apr 11 '22 09:04 akolubau

trying to add @RunWith(AndroidJUnit4::class) to my test class and then solved, may it can help if your problem same with me.

gbusiri avatar Apr 12 '22 08:04 gbusiri

It would be really helpful if someone could add a reproducing project.

Mismatched versions shouldn't be an issue since #2952, so it's probably something else that we potentially can workaround on our side

qwwdfsad avatar May 16 '22 07:05 qwwdfsad

If your class under test starts coroutines from its constructor and is constructed as a field in your test this will run before any Rule/ Before blocks.

class MyTest {
val classUnderTest = MyClass(TestScope()) //This runs before Rule/ Before
//Do Test
}

class MyClass(val scope: CoroutineScope) {
  init {
    scope.launch {...} //This now needs Main to be set when using TestScope
  }
}

Simplest fix is to change to

val classUnderTest by lazy { MyClass(TestScope()) }

RobertLK avatar May 17 '22 09:05 RobertLK

Any new update on the fix ? I have the same issue even if I add kotlinx-coroutines-android , kotlinx-coroutines-core and kotlinx-coroutines-test on the same project while writing and android instrumented test :/

DroidZed avatar Aug 14 '22 14:08 DroidZed

We still need the reproducer in order to fix the issue, or at least to report it to the responsible party.

It works in general setups, but for some Android projects it doesn't, and we need a reproducer to pinpoint the issue

qwwdfsad avatar Aug 15 '22 13:08 qwwdfsad

val classUnderTest by lazy { MyClass(TestScope()) }

This works for me. With Dispatchers.setMain(Dispatchers.Unconfined)

Suggest to close this issue, because for my case issue is not relevant.

akolubau avatar Aug 29 '22 11:08 akolubau