Skip to content

Commit 8ce0a74

Browse files
committed
test(android): own Wear actor dispatchers
1 parent 5a291f9 commit 8ce0a74

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

‎apps/android/app/src/test/java/ai/openclaw/app/wear/WearProxyBridgeTest.kt‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ import com.google.android.gms.tasks.Tasks
1111
import kotlinx.coroutines.CancellationException
1212
import kotlinx.coroutines.CompletableDeferred
1313
import kotlinx.coroutines.CoroutineScope
14-
import kotlinx.coroutines.Dispatchers
14+
import kotlinx.coroutines.ExecutorCoroutineDispatcher
1515
import kotlinx.coroutines.ExperimentalCoroutinesApi
1616
import kotlinx.coroutines.SupervisorJob
17+
import kotlinx.coroutines.asCoroutineDispatcher
1718
import kotlinx.coroutines.async
1819
import kotlinx.coroutines.cancel
1920
import kotlinx.coroutines.test.runCurrent
@@ -27,16 +28,25 @@ import org.junit.After
2728
import org.junit.Assert.assertEquals
2829
import org.junit.Assert.assertTrue
2930
import org.junit.Test
31+
import java.util.concurrent.Executors
3032

3133
@OptIn(ExperimentalCoroutinesApi::class)
3234
class 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

Comments
 (0)