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

Provide access to TestCoroutineScope's dispatcher #1609

Closed
ebrowne72 opened this issue Oct 11, 2019 · 0 comments
Closed

Provide access to TestCoroutineScope's dispatcher #1609

ebrowne72 opened this issue Oct 11, 2019 · 0 comments
Assignees
Labels

Comments

@ebrowne72
Copy link

I have a class where I inject both a CoroutineScope (such as MainScope) and a background dispatcher (such as Dispatchers.Default). I have a method which launches a coroutine in the scope and then uses withContext to switch to the background dispatcher, where it calls delay().

class ClassUnderTest(private val mainScope: CoroutineScope, private val bgDispatcher: CoroutineDispatcher {
    fun doSomething() {
        mainScope.launch {
            val data = withContext(bgDispatcher) {
                delay(1000)
                42
            }
            doSomethingWithData(data)
        }
    }
}

private val testBgDispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope()
private lateinit var subject: ClassUnderTest

@BeforeEach
fun setUp() {
    subject = ClassUnderTest(testScope, testBgDispatcher)
}

@Test
fun testSomething() {
    subject.doSomething()
    testScope.advanceUntilIdle()
}

When I try to test this function I get an UncompletedCoroutinesError because the delay() doesn't advance when I call testScope.advanceUntilIdle().

As a workaround I could call testDispatcher.advanceUntilIdle(). A better workaround is to pass my TestCoroutineDispatcher to the TestCoroutineScope's constructor:

private val testBgDispatcher = TestCoroutineDispatcher()
private val testScope = TestCoroutineScope(testBgDispatcher)

However, I think the best solution would be to expose the TestCoroutineScope's dispatcher, so I can retrieve it and inject it in my class.

subject = ClassUnderTest(testScope, testScope.dispatcher)
@qwwdfsad qwwdfsad added the test label Oct 11, 2019
@qwwdfsad qwwdfsad self-assigned this Feb 18, 2020
yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this issue Jan 28, 2022
This commit introduces the new version of the test module.
Please see README.md and MIGRATION.md for a thorough
discussion of the changes.

Fixes Kotlin#1203
Fixes Kotlin#1609
Fixes Kotlin#2379
Fixes Kotlin#1749
Fixes Kotlin#1204
Fixes Kotlin#1390
Fixes Kotlin#1222
Fixes Kotlin#1395
Fixes Kotlin#1881
Fixes Kotlin#1910
Fixes Kotlin#1772
Fixes Kotlin#1626
Fixes Kotlin#1742
Fixes Kotlin#2082
Fixes Kotlin#2102
Fixes Kotlin#2405
Fixes Kotlin#2462

Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
pablobaxter pushed a commit to pablobaxter/kotlinx.coroutines that referenced this issue Sep 14, 2022
This commit introduces the new version of the test module.
Please see README.md and MIGRATION.md for a thorough
discussion of the changes.

Fixes Kotlin#1203
Fixes Kotlin#1609
Fixes Kotlin#2379
Fixes Kotlin#1749
Fixes Kotlin#1204
Fixes Kotlin#1390
Fixes Kotlin#1222
Fixes Kotlin#1395
Fixes Kotlin#1881
Fixes Kotlin#1910
Fixes Kotlin#1772
Fixes Kotlin#1626
Fixes Kotlin#1742
Fixes Kotlin#2082
Fixes Kotlin#2102
Fixes Kotlin#2405
Fixes Kotlin#2462

Co-authored-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
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

3 participants