Skip to content

Commit 7014961

Browse files
authored
Update the documentation for runTest (#3632)
1 parent c5f73ba commit 7014961

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt

+9-8
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte
8383
* }
8484
* ```
8585
*
86-
* ### Implementation details
86+
* ### Uncaught exceptions with no handler
8787
*
88-
* By default, when no handler is installed, uncaught exception are handled in the following way:
89-
* * If exception is [CancellationException] then it is ignored
90-
* (because that is the supposed mechanism to cancel the running coroutine)
91-
* * Otherwise:
92-
* * if there is a [Job] in the context, then [Job.cancel] is invoked;
93-
* * Otherwise, all instances of [CoroutineExceptionHandler] found via [ServiceLoader]
94-
* * and current thread's [Thread.uncaughtExceptionHandler] are invoked.
88+
* When no handler is installed, exception are handled in the following way:
89+
* * If exception is [CancellationException], it is ignored, as these exceptions are used to cancel coroutines.
90+
* * Otherwise, if there is a [Job] in the context, then [Job.cancel] is invoked.
91+
* * Otherwise, as a last resort, the exception is processed in a platform-specific manner:
92+
* - On JVM, all instances of [CoroutineExceptionHandler] found via [ServiceLoader], as well as
93+
* the current thread's [Thread.uncaughtExceptionHandler], are invoked.
94+
* - On Native, the whole application crashes with the exception.
95+
* - On JS, the exception is logged via the Console API.
9596
*
9697
* [CoroutineExceptionHandler] can be invoked from an arbitrary thread.
9798
*/

kotlinx-coroutines-test/common/src/TestBuilders.kt

+16-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public expect class TestResult
3939
* Executes [testBody] as a test in a new coroutine, returning [TestResult].
4040
*
4141
* On JVM and Native, this function behaves similarly to `runBlocking`, with the difference that the code that it runs
42-
* will skip delays. This allows to use [delay] in without causing the tests to take more time than necessary.
42+
* will skip delays. This allows to use [delay] in tests without causing them to take more time than necessary.
4343
* On JS, this function creates a `Promise` that executes the test body with the delay-skipping behavior.
4444
*
4545
* ```
@@ -60,7 +60,7 @@ public expect class TestResult
6060
* immediately return the produced [TestResult] from the test method, without doing anything else afterwards. See
6161
* [TestResult] for details on this.
6262
*
63-
* The test is run in a single thread, unless other [CoroutineDispatcher] are used for child coroutines.
63+
* The test is run on a single thread, unless other [CoroutineDispatcher] are used for child coroutines.
6464
* Because of this, child coroutines are not executed in parallel to the test body.
6565
* In order for the spawned-off asynchronous code to actually be executed, one must either [yield] or suspend the
6666
* test body some other way, or use commands that control scheduling (see [TestCoroutineScheduler]).
@@ -84,19 +84,19 @@ public expect class TestResult
8484
* // 3
8585
* }
8686
* // 2
87-
* advanceUntilIdle() // runs the tasks until their queue is empty
87+
* testScheduler.advanceUntilIdle() // runs the tasks until their queue is empty
8888
* // 4
8989
* }
9090
* ```
9191
*
9292
* ### Task scheduling
9393
*
94-
* Delay-skipping is achieved by using virtual time.
94+
* Delay skipping is achieved by using virtual time.
9595
* If [Dispatchers.Main] is set to a [TestDispatcher] via [Dispatchers.setMain] before the test,
9696
* then its [TestCoroutineScheduler] is used;
9797
* otherwise, a new one is automatically created (or taken from [context] in some way) and can be used to control
9898
* the virtual time, advancing it, running the tasks scheduled at a specific time etc.
99-
* Some convenience methods are available on [TestScope] to control the scheduler.
99+
* The scheduler can be accessed via [TestScope.testScheduler].
100100
*
101101
* Delays in code that runs inside dispatchers that don't use a [TestCoroutineScheduler] don't get skipped:
102102
* ```
@@ -126,25 +126,27 @@ public expect class TestResult
126126
* There's a built-in timeout of 10 seconds for the test body. If the test body doesn't complete within this time,
127127
* then the test fails with an [AssertionError]. The timeout can be changed by setting the [timeout] parameter.
128128
*
129-
* The test finishes by the timeout procedure cancelling the test body. If the code inside the test body does not
130-
* respond to cancellation, we will not be able to make the test execution stop, in which case, the test will hang
131-
* despite our best efforts to terminate it.
129+
* On timeout, the test body is cancelled so that the test finishes. If the code inside the test body does not
130+
* respond to cancellation, the timeout will not be able to make the test execution stop.
131+
* In that case, the test will hang despite the attempt to terminate it.
132132
*
133133
* On the JVM, if `DebugProbes` from the `kotlinx-coroutines-debug` module are installed, the current dump of the
134-
* coroutines' stack is printed to the console on timeout.
134+
* coroutines' stack is printed to the console on timeout before the test body is cancelled.
135135
*
136136
* #### Reported exceptions
137137
*
138138
* Unhandled exceptions will be thrown at the end of the test.
139-
* If the uncaught exceptions happen after the test finishes, the error is propagated in a platform-specific manner.
139+
* If uncaught exceptions happen after the test finishes, they are propagated in a platform-specific manner:
140+
* see [handleCoroutineException] for details.
140141
* If the test coroutine completes with an exception, the unhandled exceptions are suppressed by it.
141142
*
142143
* #### Uncompleted coroutines
143144
*
144-
* This method requires that, after the test coroutine has completed, all the other coroutines launched inside
145-
* [testBody] also complete, or are cancelled.
146-
* Otherwise, the test will be failed (which, on JVM and Native, means that [runTest] itself will throw
147-
* [AssertionError], whereas on JS, the `Promise` will fail with it).
145+
* Otherwise, the test will hang until all the coroutines launched inside [testBody] complete.
146+
* This may be an issue when there are some coroutines that are not supposed to complete, like infinite loops that
147+
* perform some background work and are supposed to outlive the test.
148+
* In that case, [TestScope.backgroundScope] can be used to launch such coroutines.
149+
* They will be cancelled automatically when the test finishes.
148150
*
149151
* ### Configuration
150152
*

0 commit comments

Comments
 (0)