Skip to content

docs: add example for runTest to reiterate that runTest waits for children to finish #4416

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions kotlinx-coroutines-test/common/src/TestBuilders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public expect class TestResult
* }
* ```
*
* The platform difference entails that, in order to use this function correctly in common code, one must always
* immediately return the produced [TestResult] from the test method, without doing anything else afterwards. See
* The platform difference entails that, to use this function correctly in common code, one must always
* immediately return the produced [TestResult] from the test method, without doing anything else afterward. See
* [TestResult] for details on this.
*
* The test is run on a single thread, unless other [CoroutineDispatcher] are used for child coroutines.
Expand All @@ -69,6 +69,7 @@ public expect class TestResult
* // 2
* job.join() // the main test coroutine suspends here, so the child is executed
* // 4
* // use the results here
* }
*
* @Test
Expand All @@ -80,9 +81,31 @@ public expect class TestResult
* // 2
* testScheduler.advanceUntilIdle() // runs the tasks until their queue is empty
* // 4
* // use the results here
* }
* ```
*
* If the results of children coroutines computations are not needed in the runTest scope,
* there is no need to wait for children coroutines to finish, they are awaited for automatically
* by runTest parent coroutine.
* ```
* @Test
* fun exampleWaitingForAsyncTasks3() = runTest {
* val x = 0
* val y = 1
* // 1
* launch {
* // 3
* assertEquals(0, x)
* }
* launch {
* // 4
* assertEquals(1, y)
* }
* // 2
* } // 5
* ```
*
* ### Task scheduling
*
* Delay skipping is achieved by using virtual time.
Expand Down Expand Up @@ -188,8 +211,8 @@ public fun runTest(
* }
* ```
*
* The platform difference entails that, in order to use this function correctly in common code, one must always
* immediately return the produced [TestResult] from the test method, without doing anything else afterwards. See
* The platform difference entails that, to use this function correctly in common code, one must always
* immediately return the produced [TestResult] from the test method, without doing anything else afterward. See
* [TestResult] for details on this.
*
* The test is run in a single thread, unless other [CoroutineDispatcher] are used for child coroutines.
Expand All @@ -207,6 +230,7 @@ public fun runTest(
* // 2
* job.join() // the main test coroutine suspends here, so the child is executed
* // 4
* // use the results here
* }
*
* @Test
Expand All @@ -218,9 +242,31 @@ public fun runTest(
* // 2
* advanceUntilIdle() // runs the tasks until their queue is empty
* // 4
* // use the results here
* }
* ```
*
* If the results of children coroutines computations are not needed in the runTest scope,
* there is no need to wait for children coroutines to finish, they are awaited for automatically
* by runTest parent coroutine.
* ```
* @Test
* fun exampleWaitingForAsyncTasks3() = runTest {
* val x = 0
* val y = 1
* // 1
* launch {
* // 3
* assertEquals(0, x)
* }
* launch {
* // 4
* assertEquals(1, y)
* }
* // 2
* } // 5
* ```
*
* ### Task scheduling
*
* Delay-skipping is achieved by using virtual time.
Expand Down