kotlinx.coroutines: [question] Is this "IllegalStateException: This job has not completed yet" while using runBlockingTest normal?
Why when executing the following test:
@Test
fun foo() = runBlockingTest {
bar()
}
suspend fun bar() {
println(1)
withContext(Dispatchers.IO) {
println(2)
}
println(3)
}
The output is:
1
2
3
java.lang.IllegalStateException: This job has not completed yet
at kotlinx.coroutines.JobSupport.getCompletionExceptionOrNull(JobSupport.kt:1114)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest(TestBuilders.kt:53)
at kotlinx.coroutines.test.TestBuildersKt.runBlockingTest$default(TestBuilders.kt:45)
at com.foopkg.MyTests.foo(MyTests.kt:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:184)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:180)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:127)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1251)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(ArrayList.java:1251)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
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)
Is it related to #1204 ?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 18
- Comments: 22 (6 by maintainers)
Commits related to this issue
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/kotlinx.corout... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Simplify service test pattern (#4817) * Make a simple test lifecycle registry, instead of creating empty testactivity * Remove use of `runBlockingTest`: according to https://github.com/Kotlin/ko... — committed to PolymerLabs/arcs by jblebrun 4 years ago
- Squashed commit of the following: commit 4b05a287434dcd3a3622ea9c9ebb499fe1d5bc46 Merge: f4c1d26a 99000c25 Author: Alex Rosengarten <alxrsngrtn@google.com> Date: Tue Mar 3 11:18:14 2020 -0800 ... — committed to alxmrs/arcs by alxmrs 4 years ago
- Squashed commit of the following: commit b3d92d0f69f98571237f06e91ff480209f7aab9d Author: Alex Rosengarten <alxrsngrtn@google.com> Date: Wed Mar 4 12:17:17 2020 -0800 updated test name commit... — committed to alxmrs/arcs by alxmrs 4 years ago
- recipe2plan 2/n: Validate Handles, Tests (#4834) * creating structure of ts r2p script * Created fast CLI rapper for r2p script * WIP figuring out ways to resolve a recipe * fix sp * Pass... — committed to PolymerLabs/arcs by alxmrs 4 years ago
- Squashed commit of the following: commit 17a2ab1277abfbb4af6499e78f41aaf6c3ea59a6 Author: Alex Rosengarten <alxrsngrtn@google.com> Date: Wed Mar 4 16:30:45 2020 -0800 Fixed indentation commit... — committed to alxmrs/arcs by alxmrs 4 years ago
- recipe2plan 3/n: Plan code-generation (#4865) ## Summary First iteration of generating Plan objects. ## Changelog * creating structure of ts r2p script * Created fast CLI rapper for r2p scrip... — committed to PolymerLabs/arcs by alxmrs 4 years ago
- recipe2plan 4/n: Generating Kotlin `Type`s (#4869) ## Summary Enhances #4865, generates Types within Kotlin Plan classes. For an example, see Golden's modification. ## Changelog * creating st... — committed to PolymerLabs/arcs by alxmrs 4 years ago
- Implement 'runTest' that waits for asynchronous completion (#2978) Implement a multiplatform runTest as an initial implementation of #1996. Fixes #1204 Fixes #1222 Fixes #1395 Fixes #1881 Fixe... — committed to Kotlin/kotlinx.coroutines by dkhalanskyjb 3 years ago
- Implement 'runTest' that waits for asynchronous completion (#2978) Implement a multiplatform runTest as an initial implementation of #1996. Fixes #1204 Fixes #1222 Fixes #1395 Fixes #1881 Fixes #191... — committed to Kotlin/kotlinx.coroutines by dkhalanskyjb 3 years ago
- Implement 'runTest' that waits for asynchronous completion (#2978) Implement a multiplatform runTest as an initial implementation of #1996. Fixes #1204 Fixes #1222 Fixes #1395 Fixes #1881 Fixes #191... — committed to Kotlin/kotlinx.coroutines by dkhalanskyjb 3 years ago
- Implement 'runTest' that waits for asynchronous completion (#2978) Implement a multiplatform runTest as an initial implementation of #1996. Fixes #1204 Fixes #1222 Fixes #1395 Fixes #1881 Fixes #191... — committed to Kotlin/kotlinx.coroutines by dkhalanskyjb 3 years ago
- Update kotlinx-coroutines-test (#2973) 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 #1203 Fix... — committed to Kotlin/kotlinx.coroutines by dkhalanskyjb 3 years ago
- Workaround for crash when using runBlockingTest - https://github.com/Kotlin/kotlinx.coroutines/issues/1222 — committed to mobilejazz/harmony-kotlin by deleted user 3 years ago
- Update kotlinx-coroutines-test (#2973) 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 #1203 Fix... — committed to yorickhenning/kotlinx.coroutines by dkhalanskyjb 3 years ago
I replaced
runBlockingTestonrunBlockingand it helped.Thanks for the minimal repro!
In the current version there’s an issue that causes runBlockingTest to throw this exception incorrectly when there are multiple threads. This is the same issue reported in https://github.com/Kotlin/kotlinx.coroutines/issues/1204
Patch https://github.com/Kotlin/kotlinx.coroutines/pull/1206 will change this to pass as expected.
In my case, I had to do this while using
runBlockingTest:I am not sure if it is the right way but the test passed thou.
The one annoying thing about runBlocking is that JUnit tests don’t run unless they return Unit, but it’s hard to remember to always type
runBlocking<Unit> orfun testFoo(): Unit = runBlocking`.We use this just to get around that:
Still continuing. I am using a custom
CoroutinesTestRulebut still getting this issue. Any workaround except usingrunBlocking?runBlockingworks for me too@qwwdfsad Is there any place where we can track the progress? I can see the https://github.com/Kotlin/kotlinx.coroutines/pull/1206 was closed, but I couldn’t find any further updates.
My problem was I was using Robolectric to test a class that used Google’s LocationProvider for Location and using a suspend function locationManager.getLocation() which caused the exception with
runBlockingTest.The only solution was this to use
GlobalScope.launch { }to test it. I didn’t even need the coroutine test rules or anything:Initially I was running the test like this, with coroutine test dispatchers and instant task executor rules and whatnot, and got the Job not completed exception:
this also caused an exception:
Maybe this will help someone.
Hi,
i’m facing same issue as described above executing my room database unit tests.
Has there been found out any fix for that ?
using this if assert value is not equal then also the test will get pass