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

Provided example test for withTimeout fails #1390

Closed
ZakTaccardi opened this issue Jul 27, 2019 · 3 comments
Closed

Provided example test for withTimeout fails #1390

ZakTaccardi opened this issue Jul 27, 2019 · 3 comments
Assignees
Labels

Comments

@ZakTaccardi
Copy link

@ZakTaccardi ZakTaccardi commented Jul 27, 2019

I just copied the following into my IDE from the README.md

@Test(expected = TimeoutCancellationException::class)
fun testFooWithTimeout() = runBlockingTest {
    val uncompleted = CompletableDeferred<Unit>() // this Deferred<Foo> will never complete
    foo(uncompleted)
    advanceTimeBy(1_000) // advance time, which will cause the timeout to throw an exception
    // ...
}

fun CoroutineScope.foo(resultDeferred: Deferred<Unit>) {
    launch {
        withTimeout(1_000) {
            resultDeferred.await() // await() will suspend forever waiting for uncompleted
            // ...
        }
    }
}

I expected the test to pass, but it failed with the following exception:

java.lang.AssertionError: Expected exception: kotlinx.coroutines.TimeoutCancellationException

	at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
	at org.junit.rules.RunRules.evaluate(RunRules.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

advanceTimeBy(..) does cause the timeout exception to be thrown, but runBlockingTest may be swallowing it.

@qwwdfsad qwwdfsad added the test label Jul 27, 2019
@elizarov
Copy link
Member

@elizarov elizarov commented Aug 5, 2019

cc @objcode

@douo
Copy link

@douo douo commented Nov 26, 2019

Same Issue. @elizarov @objcode
pass:

    @Test(expected = Exception::class)
    fun throwException() = runBlockingTest {
        launch {
            throw Exception()
        }
    }

pass

    @Test(expected = TimeoutCancellationException::class)
    fun throwTimeoutCancellationExceptionDirectly() = runBlockingTest {
            withTimeout(-1) {}
    }

failed:

   @Test(expected = TimeoutCancellationException::class)
    fun throwTimeoutCancellationException() = runBlockingTest {
        launch {
            withTimeout(-1) {}
        }
    }

@qwwdfsad qwwdfsad self-assigned this Feb 18, 2020
@dkhalanskyjb dkhalanskyjb added docs and removed test labels Oct 1, 2021
@dkhalanskyjb
Copy link
Member

@dkhalanskyjb dkhalanskyjb commented Oct 1, 2021

The docs are faulty here. Not much can be done on the test module's side here to make the test pass as it is, unfortunately: this issue can be replicated easily with runBlocking as well and is purely an interplay between launch and withTimeout, where the parent job doesn't cancel upon its child's failure if the exception is a CancellationException, which TimeoutCancellationException is: #1374.

dkhalanskyjb added a commit that referenced this issue Nov 16, 2021
dkhalanskyjb added a commit that referenced this issue Nov 17, 2021
dkhalanskyjb added a commit that referenced this issue Nov 17, 2021
dkhalanskyjb added a commit that referenced this issue Nov 19, 2021
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants