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 1 commit 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
24 changes: 24 additions & 0 deletions kotlinx-coroutines-test/common/src/TestBuilders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,32 @@ 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