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

cleanupTestCoroutines does not detect awaiting CompletableDeferred #1749

Closed
mhernand40 opened this issue Jan 3, 2020 · 1 comment
Closed
Assignees
Labels

Comments

@mhernand40
Copy link

Please see the following test:

@ExperimentalCoroutinesApi
class Repro {

    private val scope = TestCoroutineScope()
    private val subject = Subject(scope)

    @After
    fun tearDown() {
        scope.cleanupTestCoroutines()
    }

    @Test
    fun `test with delay`() {
        subject.launchCoroutineWithDelay(delayMillis = 5L)

        assertNull(subject.result)
    }

    @Test
    fun `test with CompletableDeferred`() {
        val deferred = CompletableDeferred<String>()
        subject.launchCoroutineAndAwait(deferred)

        assertNull(subject.result)
    }
}

class Subject(private val scope: CoroutineScope) {
    var result: String? = null

    fun launchCoroutineWithDelay(delayMillis: Long) {
        scope.launch {
            delay(timeMillis = delayMillis)
            result = "Result"
        }
    }

    fun launchCoroutineAndAwait(deferred: Deferred<String>) {
        scope.launch {
            result = deferred.await()
        }
    }
}

When I run the test class, only test with delay fails with:

kotlinx.coroutines.test.UncompletedCoroutinesError: Unfinished coroutines during teardown. Ensure all coroutines are completed or cancelled by your test.
	at kotlinx.coroutines.test.TestCoroutineDispatcher.cleanupTestCoroutines(TestCoroutineDispatcher.kt:178)
	at kotlinx.coroutines.test.TestCoroutineScopeImpl.cleanupTestCoroutines(TestCoroutineScope.kt:35)

However, test with CompletableDeferred passes even though the CompletableDeferred is never completed nor cancelled. Is this considered an undetected leak in the test?

@qwwdfsad qwwdfsad added the test label Jan 15, 2020
@qwwdfsad qwwdfsad self-assigned this Jan 15, 2020
@StolarczykJakub
Copy link

dkhalanskyjb added a commit that referenced this issue Oct 11, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 13, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 13, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 15, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 25, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 27, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 27, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
* Complete the scope's job if a new job was created for it
Fixes #1772
dkhalanskyjb added a commit that referenced this issue Oct 28, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
dkhalanskyjb added a commit that referenced this issue Nov 17, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
dkhalanskyjb added a commit that referenced this issue Nov 17, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
dkhalanskyjb added a commit that referenced this issue Nov 19, 2021
* Add more detailed documentation;
* Move most verification logic from `runBlockingTest` to
  `cleanupTestCoroutines`
Fixes #1749
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

4 participants