Use TestScope.backgroundScope

This commit is contained in:
Benoit Marty 2025-10-23 16:38:30 +02:00 committed by Benoit Marty
parent b8c7e16c50
commit fdc64e9be7

View file

@ -15,7 +15,6 @@ import io.element.android.libraries.sessionstorage.impl.aDbSessionData
import io.element.android.libraries.sessionstorage.impl.toApiModel import io.element.android.libraries.sessionstorage.impl.toApiModel
import io.element.android.tests.testutils.testCoroutineDispatchers import io.element.android.tests.testutils.testCoroutineDispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runCurrent
@ -55,7 +54,6 @@ class DefaultSessionObserverTest {
databaseSessionStore.addSession(sessionData.toApiModel()) databaseSessionStore.addSession(sessionData.toApiModel())
listener.assertEvents(TestSessionListener.Event.Created(sessionData.userId)) listener.assertEvents(TestSessionListener.Event.Created(sessionData.userId))
sut.removeListener(listener) sut.removeListener(listener)
coroutineContext.cancelChildren()
} }
@Test @Test
@ -72,7 +70,6 @@ class DefaultSessionObserverTest {
TestSessionListener.Event.Created(sessionData.userId), TestSessionListener.Event.Created(sessionData.userId),
TestSessionListener.Event.Deleted(sessionData.userId, true), TestSessionListener.Event.Deleted(sessionData.userId, true),
) )
coroutineContext.cancelChildren()
} }
@Test @Test
@ -93,13 +90,12 @@ class DefaultSessionObserverTest {
TestSessionListener.Event.Deleted(sessionData2.userId, wasLastSession = false), TestSessionListener.Event.Deleted(sessionData2.userId, wasLastSession = false),
TestSessionListener.Event.Deleted(sessionData1.userId, wasLastSession = true), TestSessionListener.Event.Deleted(sessionData1.userId, wasLastSession = true),
) )
coroutineContext.cancelChildren()
} }
private fun TestScope.createDefaultSessionObserver(): DefaultSessionObserver { private fun TestScope.createDefaultSessionObserver(): DefaultSessionObserver {
return DefaultSessionObserver( return DefaultSessionObserver(
sessionStore = databaseSessionStore, sessionStore = databaseSessionStore,
coroutineScope = this, coroutineScope = backgroundScope,
dispatchers = testCoroutineDispatchers(useUnconfinedTestDispatcher = true), dispatchers = testCoroutineDispatchers(useUnconfinedTestDispatcher = true),
) )
} }