Skip to content

Commit e615346

Browse files
committed
Support CoroutineScope without a Job in testIn
Most `CoroutineScope`s contain a `Job`, and should contain a `Job` to work with structured concurrency. However, some `CoroutineScope`s do not have a `Job` (`GlobalScope` is an example). This change adds support for `CoroutineScope`s with no `Job` to Turbine's `testIn` function, which previously would throw `IllegalStateException` if called with such a `CoroutineScope`. A `CoroutineScope` with no `Job` never completes, so a call to `invokeOnCompletion` is not needed in that case.
1 parent 813f4b7 commit e615346

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Nothing yet!
1111

1212
### Fixed
13-
- Nothing yet!
13+
- Calling `testIn` with a `CoroutineScope` that does not contain a `Job` no longer throws `IllegalStateException`.
1414

1515

1616
## [1.2.0] - 2024-10-16

src/commonMain/kotlin/app/cash/turbine/flow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import kotlinx.coroutines.CoroutineScope
2424
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
2525
import kotlinx.coroutines.Dispatchers.Unconfined
2626
import kotlinx.coroutines.ExperimentalCoroutinesApi
27+
import kotlinx.coroutines.Job
2728
import kotlinx.coroutines.channels.Channel
2829
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
2930
import kotlinx.coroutines.coroutineScope
3031
import kotlinx.coroutines.currentCoroutineContext
3132
import kotlinx.coroutines.flow.Flow
32-
import kotlinx.coroutines.job
3333
import kotlinx.coroutines.launch
3434
import kotlinx.coroutines.plus
3535
import kotlinx.coroutines.test.TestCoroutineScheduler
@@ -180,7 +180,7 @@ public fun <T> Flow<T>.testIn(
180180

181181
val turbine = collectTurbineIn(scope, timeout, name)
182182

183-
scope.coroutineContext.job.invokeOnCompletion { exception ->
183+
scope.coroutineContext[Job]?.invokeOnCompletion { exception ->
184184
if (debug) println("Scope ending ${exception ?: ""}")
185185

186186
// Only validate events were consumed if the scope is exiting normally.

0 commit comments

Comments
 (0)