Skip to content
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

Dispatchers.setMain not working for 1.6+ versions #3244

Closed
akolubau opened this issue Apr 9, 2022 · 10 comments
Closed

Dispatchers.setMain not working for 1.6+ versions #3244

akolubau opened this issue Apr 9, 2022 · 10 comments
Labels

Comments

@akolubau
Copy link

akolubau commented Apr 9, 2022

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?

@gbusiri
Copy link

gbusiri commented Apr 11, 2022

same here

@dkhalanskyjb
Copy link
Contributor

dkhalanskyjb commented Apr 11, 2022

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.

@gbusiri
Copy link

gbusiri commented Apr 11, 2022

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

@akolubau
Copy link
Author

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.

@gbusiri
Copy link

gbusiri commented Apr 12, 2022

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

@qwwdfsad
Copy link
Contributor

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

@RobertLK
Copy link

RobertLK commented May 17, 2022

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()) }

@DroidZed
Copy link

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 :/

@qwwdfsad
Copy link
Contributor

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

@akolubau
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants