@@ -11,9 +11,10 @@ import com.google.android.gms.tasks.Tasks
1111import kotlinx.coroutines.CancellationException
1212import kotlinx.coroutines.CompletableDeferred
1313import kotlinx.coroutines.CoroutineScope
14- import kotlinx.coroutines.Dispatchers
14+ import kotlinx.coroutines.ExecutorCoroutineDispatcher
1515import kotlinx.coroutines.ExperimentalCoroutinesApi
1616import kotlinx.coroutines.SupervisorJob
17+ import kotlinx.coroutines.asCoroutineDispatcher
1718import kotlinx.coroutines.async
1819import kotlinx.coroutines.cancel
1920import kotlinx.coroutines.test.runCurrent
@@ -27,16 +28,25 @@ import org.junit.After
2728import org.junit.Assert.assertEquals
2829import org.junit.Assert.assertTrue
2930import org.junit.Test
31+ import java.util.concurrent.Executors
3032
3133@OptIn(ExperimentalCoroutinesApi ::class )
3234class WearProxyBridgeTest {
3335 private val actorScopes = mutableListOf<CoroutineScope >()
34-
35- private fun actorScope (): CoroutineScope = CoroutineScope (SupervisorJob () + Dispatchers .Unconfined ).also (actorScopes::add)
36+ private val actorDispatchers = mutableListOf<ExecutorCoroutineDispatcher >()
37+
38+ private fun actorScope (): CoroutineScope {
39+ // Robolectric's Android unit runner can retain Unconfined actors in runTest's scheduler.
40+ // A real owned thread matches the production actor lifecycle and makes cleanup explicit.
41+ val dispatcher = Executors .newSingleThreadExecutor().asCoroutineDispatcher()
42+ actorDispatchers + = dispatcher
43+ return CoroutineScope (SupervisorJob () + dispatcher).also (actorScopes::add)
44+ }
3645
3746 @After
3847 fun cancelActorScopes () {
3948 actorScopes.forEach(CoroutineScope ::cancel)
49+ actorDispatchers.forEach(ExecutorCoroutineDispatcher ::close)
4050 }
4151
4252 @Test
0 commit comments