Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

perf: move provider snapshot reads off caller

David Nashashf16b1a4af746
diff --git a/data/src/main/java/com/streamvault/data/provider/RoomProviderSnapshotRepository.kt b/data/src/main/java/com/streamvault/data/provider/RoomProviderSnapshotRepository.kt
index b21639ee..eff8821a 100644
--- a/data/src/main/java/com/streamvault/data/provider/RoomProviderSnapshotRepository.kt
+++ b/data/src/main/java/com/streamvault/data/provider/RoomProviderSnapshotRepository.kt
@@ -10,22 +10,42 @@ import com.streamvault.data.local.dao.StalkerPortalStateDao
import com.streamvault.data.local.entity.StalkerPortalStateEntity
import com.streamvault.domain.model.*
import com.streamvault.domain.repository.ProviderSnapshotRepository
+import kotlinx.coroutines.CoroutineDispatcher
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
-class RoomProviderSnapshotRepository @Inject constructor(
+class RoomProviderSnapshotRepository internal constructor(
private val providerDao: ProviderDao,
private val snapshotDao: ProviderSnapshotDao,
private val stalkerPortalStateDao: StalkerPortalStateDao,
private val codec: ProviderConfigurationCodec,
- private val gson: Gson
+ private val gson: Gson,
+ private val workDispatcher: CoroutineDispatcher
) : ProviderSnapshotRepository {
+ @Inject
+ constructor(
+ providerDao: ProviderDao,
+ snapshotDao: ProviderSnapshotDao,
+ stalkerPortalStateDao: StalkerPortalStateDao,
+ codec: ProviderConfigurationCodec,
+ gson: Gson
+ ) : this(
+ providerDao = providerDao,
+ snapshotDao = snapshotDao,
+ stalkerPortalStateDao = stalkerPortalStateDao,
+ codec = codec,
+ gson = gson,
+ workDispatcher = Dispatchers.IO
+ )
+
private val stringListType = object : TypeToken<List<String>>() {}.type
- override suspend fun getSnapshot(providerId: Long): ProviderSnapshot? {
- val provider = providerDao.getById(providerId) ?: return null
- val storedConfig = snapshotDao.getConfig(providerId) ?: return null
+ override suspend fun getSnapshot(providerId: Long): ProviderSnapshot? = withContext(workDispatcher) {
+ val provider = providerDao.getById(providerId) ?: return@withContext null
+ val storedConfig = snapshotDao.getConfig(providerId) ?: return@withContext null
if (storedConfig.type != provider.type) {
throw IllegalStateException("Provider/configuration type mismatch for $providerId")
}
@@ -36,7 +56,7 @@ class RoomProviderSnapshotRepository @Inject constructor(
storedConfig.configurationGeneration
)
} else null
- return ProviderSnapshot(
+ ProviderSnapshot(
provider = StableProvider(
id = provider.id,
name = provider.name,
diff --git a/data/src/test/java/com/streamvault/data/provider/RoomProviderSnapshotRepositoryTest.kt b/data/src/test/java/com/streamvault/data/provider/RoomProviderSnapshotRepositoryTest.kt
new file mode 100644
index 00000000..5e526eb6
--- /dev/null
+++ b/data/src/test/java/com/streamvault/data/provider/RoomProviderSnapshotRepositoryTest.kt
@@ -0,0 +1,85 @@
+package com.streamvault.data.provider
+
+import com.google.common.truth.Truth.assertThat
+import com.google.gson.Gson
+import com.streamvault.data.local.dao.ProviderDao
+import com.streamvault.data.local.dao.ProviderSnapshotDao
+import com.streamvault.data.local.dao.StalkerPortalStateDao
+import com.streamvault.data.local.entity.ProviderConfigEntity
+import com.streamvault.data.local.entity.ProviderEntity
+import com.streamvault.domain.model.ProviderType
+import com.streamvault.domain.model.XtreamConfig
+import kotlinx.coroutines.CoroutineStart
+import kotlinx.coroutines.async
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.runTest
+import org.junit.Test
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.verify
+import org.mockito.kotlin.verifyNoInteractions
+import org.mockito.kotlin.whenever
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class RoomProviderSnapshotRepositoryTest {
+
+ @Test
+ fun `snapshot reads and decodes run on supplied dispatcher`() = runTest {
+ val dispatcher = StandardTestDispatcher(testScheduler)
+ val gson = Gson()
+ val provider = ProviderEntity(
+ id = 9L,
+ name = "Living room",
+ type = ProviderType.XTREAM_CODES
+ )
+ val codec = ProviderConfigurationCodec(
+ gson = gson,
+ credentialCrypto = object : com.streamvault.data.security.CredentialCrypto {
+ override fun encryptIfNeeded(value: String): String = value
+ override fun decryptIfNeeded(value: String): String = value
+ }
+ )
+ val config = ProviderConfigEntity(
+ providerId = provider.id,
+ type = provider.type,
+ schemaVersion = 1,
+ configurationGeneration = 3L,
+ identityKey = "identity",
+ encryptedConfigJson = codec.encode(
+ XtreamConfig("https://example.test", "user", "password")
+ ),
+ updatedAt = 1L
+ )
+ val providerDao = mock<ProviderDao>()
+ val snapshotDao = mock<ProviderSnapshotDao>()
+ val stalkerPortalStateDao = mock<StalkerPortalStateDao>()
+ whenever(providerDao.getById(provider.id)).thenReturn(provider)
+ whenever(snapshotDao.getConfig(provider.id)).thenReturn(config)
+ whenever(snapshotDao.getRuntime(provider.id)).thenReturn(null)
+ whenever(stalkerPortalStateDao.get(provider.id)).thenReturn(null)
+
+ val repository = RoomProviderSnapshotRepository(
+ providerDao = providerDao,
+ snapshotDao = snapshotDao,
+ stalkerPortalStateDao = stalkerPortalStateDao,
+ codec = codec,
+ gson = gson,
+ workDispatcher = dispatcher
+ )
+
+ val result = async(start = CoroutineStart.UNDISPATCHED) {
+ repository.getSnapshot(provider.id)
+ }
+
+ assertThat(result.isCompleted).isFalse()
+ verifyNoInteractions(providerDao, snapshotDao, stalkerPortalStateDao)
+
+ advanceUntilIdle()
+
+ assertThat(result.await()?.provider?.id).isEqualTo(provider.id)
+ verify(providerDao).getById(provider.id)
+ verify(snapshotDao).getConfig(provider.id)
+ verify(snapshotDao).getRuntime(provider.id)
+ }
+}