From a18e6f6f9bd9f1222712f2f2748ffe29efa25306 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Thu, 19 Nov 2026 16:53:28 +0100 Subject: [PATCH] test(chat): fix races of the chat viewmodel tests with Dispatchers.Default The message pipeline of a viewmodel calls the repository from Dispatchers.Default. A call during the stubbing of the thread replies took the stub, so the thread test waited forever for replies. Stub before any viewmodel exists and fail on a timeout instead of hanging. After a test, the cancelled pipeline could still use Dispatchers.Main while it was reset. Clear every viewmodel and wait for its work to end before resetting Main. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marcel Hibbe --- .../viewmodels/ChatViewModelRoomJoinTest.kt | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/app/src/test/java/com/nextcloud/talk/chat/viewmodels/ChatViewModelRoomJoinTest.kt b/app/src/test/java/com/nextcloud/talk/chat/viewmodels/ChatViewModelRoomJoinTest.kt index 4229fa29a7f..33793100373 100644 --- a/app/src/test/java/com/nextcloud/talk/chat/viewmodels/ChatViewModelRoomJoinTest.kt +++ b/app/src/test/java/com/nextcloud/talk/chat/viewmodels/ChatViewModelRoomJoinTest.kt @@ -15,6 +15,7 @@ import androidx.lifecycle.LifecycleRegistry import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelStore +import androidx.lifecycle.viewModelScope import com.nextcloud.talk.chat.data.ChatMessageRepository import com.nextcloud.talk.chat.data.io.AudioFocusRequestManager import com.nextcloud.talk.chat.data.io.MediaRecorderManager @@ -49,6 +50,7 @@ import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.Flow @@ -57,10 +59,13 @@ import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.take import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.job +import kotlinx.coroutines.joinAll import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain +import kotlinx.coroutines.withTimeout import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -117,6 +122,7 @@ class ChatViewModelRoomJoinTest { private var screenResumed = false private var initialLoadDone = false private val viewModelStore = ViewModelStore() + private val viewModelJobs = mutableListOf() private val appScope = CoroutineScope(SupervisorJob() + UnconfinedTestDispatcher()) private class TestOwner : LifecycleOwner { @@ -141,8 +147,13 @@ class ChatViewModelRoomJoinTest { GenericOverall(GenericOCS(GenericMetaDto(statusCode = HTTP_OK))) } chatRepository = mockWithEmptyFlows() + // Stubbed before any viewmodel exists: the message pipeline of a viewmodel calls the repository from + // Dispatchers.Default, and a call between observeThreadReplies() and thenReturn() would take the stub, + // leaving the thread's replies on the default empty flow. + whenever(chatRepository.observeThreadReplies(any(), eq(THREAD_ID.toLong()))) + .thenReturn(flowOf(null, 3, 3, THREAD_REPLIES)) createViewModel = { threadId -> - ChatViewModel( + val newViewModel = ChatViewModel( mock(), mock(), network, @@ -159,16 +170,18 @@ class ChatViewModelRoomJoinTest { TOKEN, threadId ) + // every viewmodel goes into the store, so that tearDown clears it like the screen would + ViewModelProvider( + viewModelStore, + object : ViewModelProvider.Factory { + @Suppress("UNCHECKED_CAST") + override fun create(modelClass: Class): T = newViewModel as T + } + )["chat-${viewModelJobs.size}", ChatViewModel::class.java] + .also { viewModelJobs += it.viewModelScope.coroutineContext.job } } - val newViewModel = createViewModel(null) - viewModel = ViewModelProvider( - viewModelStore, - object : ViewModelProvider.Factory { - @Suppress("UNCHECKED_CAST") - override fun create(modelClass: Class): T = newViewModel as T - } - )[ChatViewModel::class.java] + viewModel = createViewModel(null) owner.lifecycle.addObserver(viewModel) // what ChatActivity does with the state of the capabilities and with a resume of the screen @@ -199,6 +212,9 @@ class ChatViewModelRoomJoinTest { fun tearDown() { owner.send(Lifecycle.Event.ON_DESTROY) viewModelStore.clear() + // The message pipeline of a viewmodel runs on Dispatchers.Default and hands its results to Main. Cancelled, + // it may still be finishing there: wait for it, or it uses Main while resetMain() below replaces it. + runBlocking { withTimeout(FLOW_TIMEOUT_MS) { viewModelJobs.joinAll() } } appScope.cancel() RxJavaPlugins.reset() RxAndroidPlugins.reset() @@ -438,11 +454,12 @@ class ChatViewModelRoomJoinTest { @Test fun `inside a thread the reply count follows the cached thread messages`() = runBlocking { - whenever(chatRepository.observeThreadReplies(any(), eq(THREAD_ID.toLong()))) - .thenReturn(flowOf(null, 3, 3, THREAD_REPLIES)) owner.send(Lifecycle.Event.ON_CREATE) - val replies = createViewModel(THREAD_ID.toLong()).cachedThreadReplies.take(2).toList() + // the flow never completes, so without the timeout too few values would hang the test + val replies = withTimeout(FLOW_TIMEOUT_MS) { + createViewModel(THREAD_ID.toLong()).cachedThreadReplies.take(2).toList() + } assertEquals(listOf(3, THREAD_REPLIES), replies) } @@ -492,6 +509,7 @@ class ChatViewModelRoomJoinTest { private const val THREAD_ID = 100 private const val THREAD_REPLIES = 23 private const val MAX_RECENT_THREADS = 50 + private const val FLOW_TIMEOUT_MS = 5000L private val USER = User( id = 1, userId = "me",