Publish and capture snapshot libraries during indexing
This commit is contained in:
@@ -21,6 +21,7 @@ package org.oxycblt.auxio.music
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
import org.oxycblt.musikr.Config
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
@@ -29,6 +30,7 @@ import kotlinx.coroutines.CancellationException
|
|||||||
import org.oxycblt.auxio.image.ImageSettings
|
import org.oxycblt.auxio.image.ImageSettings
|
||||||
import org.oxycblt.auxio.music.locations.LocationMode
|
import org.oxycblt.auxio.music.locations.LocationMode
|
||||||
import org.oxycblt.musikr.Interpretation
|
import org.oxycblt.musikr.Interpretation
|
||||||
|
import org.oxycblt.musikr.Library
|
||||||
import org.oxycblt.musikr.MutableLibrary
|
import org.oxycblt.musikr.MutableLibrary
|
||||||
import org.oxycblt.musikr.fs.mediastore.MediaStore.FilterMode
|
import org.oxycblt.musikr.fs.mediastore.MediaStore.FilterMode
|
||||||
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
import org.oxycblt.musikr.playlist.db.StoredPlaylists
|
||||||
@@ -62,6 +64,8 @@ internal constructor(
|
|||||||
|
|
||||||
interface SnapshotRestorer {
|
interface SnapshotRestorer {
|
||||||
fun start(): SnapshotRestoration
|
fun start(): SnapshotRestoration
|
||||||
|
|
||||||
|
suspend fun capture(library: Library, config: Config) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MusikrSnapshotRestorer
|
class MusikrSnapshotRestorer
|
||||||
@@ -85,6 +89,15 @@ constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun capture(library: Library, config: Config) {
|
||||||
|
LibrarySnapshot(context, config)
|
||||||
|
.capture(
|
||||||
|
library,
|
||||||
|
LibrarySnapshotStore(context.filesDir),
|
||||||
|
fingerprint(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun fingerprint(): LibrarySnapshotFingerprint {
|
private fun fingerprint(): LibrarySnapshotFingerprint {
|
||||||
val sourceMode = musicSettings.locationMode
|
val sourceMode = musicSettings.locationMode
|
||||||
val sourceRoots: List<String>
|
val sourceRoots: List<String>
|
||||||
|
|||||||
@@ -385,7 +385,10 @@ constructor(
|
|||||||
|
|
||||||
private suspend fun indexImpl(withCache: Boolean) {
|
private suspend fun indexImpl(withCache: Boolean) {
|
||||||
L.d("Index requested, initializing")
|
L.d("Index requested, initializing")
|
||||||
snapshotRestoration.await()
|
val snapshot = snapshotRestoration.await()
|
||||||
|
if (snapshot != null && !snapshot.empty()) {
|
||||||
|
emitLibrary(snapshot)
|
||||||
|
}
|
||||||
// Obtain configuration information
|
// Obtain configuration information
|
||||||
val separators = Separators.from(musicSettings.separators)
|
val separators = Separators.from(musicSettings.separators)
|
||||||
L.d("Separators: $separators")
|
L.d("Separators: $separators")
|
||||||
@@ -437,6 +440,13 @@ constructor(
|
|||||||
// Clean up old data that is now impossible for the app to be using.
|
// Clean up old data that is now impossible for the app to be using.
|
||||||
L.d("Cleanup")
|
L.d("Cleanup")
|
||||||
result.cleanup()
|
result.cleanup()
|
||||||
|
try {
|
||||||
|
snapshotRestorer.capture(result.library, config)
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
throw e
|
||||||
|
} catch (e: Exception) {
|
||||||
|
L.w(e, "Library snapshot capture failed")
|
||||||
|
}
|
||||||
// Finish up loading.
|
// Finish up loading.
|
||||||
L.d("Indexing complete")
|
L.d("Indexing complete")
|
||||||
emitIndexingCompletion(null)
|
emitIndexingCompletion(null)
|
||||||
|
|||||||
@@ -0,0 +1,269 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2026 Auxio Project
|
||||||
|
* LibrarySnapshotPublicationTest.kt is part of Auxio.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.oxycblt.auxio.music
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import io.mockk.Runs
|
||||||
|
import io.mockk.coEvery
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.just
|
||||||
|
import io.mockk.justRun
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkObject
|
||||||
|
import io.mockk.unmockkObject
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.Collections
|
||||||
|
import java.util.concurrent.ExecutorService
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.test.resetMain
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import kotlinx.coroutines.test.setMain
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNull
|
||||||
|
import org.junit.Assert.assertSame
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
import org.oxycblt.auxio.image.covers.SettingCovers
|
||||||
|
import org.oxycblt.auxio.music.locations.LocationMode
|
||||||
|
import org.oxycblt.musikr.Config
|
||||||
|
import org.oxycblt.musikr.Library
|
||||||
|
import org.oxycblt.musikr.LibraryResult
|
||||||
|
import org.oxycblt.musikr.MutableLibrary
|
||||||
|
import org.oxycblt.musikr.Musikr
|
||||||
|
import org.oxycblt.musikr.Song
|
||||||
|
import org.oxycblt.musikr.covers.Cover
|
||||||
|
import org.oxycblt.musikr.covers.MutableCovers
|
||||||
|
import org.oxycblt.musikr.fs.saf.SAF
|
||||||
|
import org.oxycblt.musikr.tag.Name
|
||||||
|
import org.oxycblt.musikr.tag.ReplayGainAdjustment
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
class LibrarySnapshotPublicationTest {
|
||||||
|
private lateinit var mainExecutor: ExecutorService
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun installMain() {
|
||||||
|
mainExecutor = Executors.newSingleThreadExecutor { Thread(it, "snapshot-test-main") }
|
||||||
|
Dispatchers.setMain(mainExecutor.asCoroutineDispatcher())
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
fun resetMain() {
|
||||||
|
Dispatchers.resetMain()
|
||||||
|
mainExecutor.shutdownNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun validNonemptySnapshot_publishesBeforeRepositoryIndexing() = runTest {
|
||||||
|
val cached = library(empty = false)
|
||||||
|
val scanCalls = AtomicInteger()
|
||||||
|
val repository =
|
||||||
|
scanBoundaryRepository(
|
||||||
|
TestSnapshotRestorer(SharedSnapshotRestoration { cached }),
|
||||||
|
scanCalls,
|
||||||
|
)
|
||||||
|
val emissions = mutableListOf<Int>()
|
||||||
|
repository.addUpdateListener(
|
||||||
|
object : MusicRepository.UpdateListener {
|
||||||
|
override fun onMusicChanges(changes: MusicRepository.Changes) {
|
||||||
|
emissions += scanCalls.get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
emissions.clear()
|
||||||
|
|
||||||
|
repository.index(mockk(), withCache = true)
|
||||||
|
|
||||||
|
assertEquals(listOf(0), emissions)
|
||||||
|
assertSame(cached, repository.library)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun absentCorruptOrEmptySnapshot_emitsNoLibraryAndStillIndexes() = runTest {
|
||||||
|
val restorations =
|
||||||
|
listOf(
|
||||||
|
object : SnapshotRestoration {
|
||||||
|
override suspend fun await(): MutableLibrary? = null
|
||||||
|
},
|
||||||
|
SharedSnapshotRestoration { throw IOException("Forced corrupt snapshot") },
|
||||||
|
SharedSnapshotRestoration { library(empty = true) },
|
||||||
|
)
|
||||||
|
|
||||||
|
for (restoration in restorations) {
|
||||||
|
val scanCalls = AtomicInteger()
|
||||||
|
val repository =
|
||||||
|
scanBoundaryRepository(TestSnapshotRestorer(restoration), scanCalls)
|
||||||
|
val emissions = AtomicInteger()
|
||||||
|
repository.addUpdateListener(
|
||||||
|
object : MusicRepository.UpdateListener {
|
||||||
|
override fun onMusicChanges(changes: MusicRepository.Changes) {
|
||||||
|
emissions.incrementAndGet()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
emissions.set(0)
|
||||||
|
|
||||||
|
repository.index(mockk(), withCache = true)
|
||||||
|
|
||||||
|
assertNull(repository.library)
|
||||||
|
assertEquals(0, emissions.get())
|
||||||
|
assertEquals(1, scanCalls.get())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun snapshotChange_deliversUpdateListenerOnMain() = runTest {
|
||||||
|
val repository =
|
||||||
|
scanBoundaryRepository(
|
||||||
|
TestSnapshotRestorer(
|
||||||
|
SharedSnapshotRestoration { library(empty = false) }
|
||||||
|
),
|
||||||
|
AtomicInteger(),
|
||||||
|
)
|
||||||
|
val callbackThreads = Collections.synchronizedList(mutableListOf<String>())
|
||||||
|
repository.addUpdateListener(
|
||||||
|
object : MusicRepository.UpdateListener {
|
||||||
|
override fun onMusicChanges(changes: MusicRepository.Changes) {
|
||||||
|
callbackThreads += Thread.currentThread().name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
callbackThreads.clear()
|
||||||
|
|
||||||
|
repository.index(mockk(), withCache = true)
|
||||||
|
|
||||||
|
assertEquals(listOf("snapshot-test-main"), callbackThreads)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun finalFreshResult_isPublishedCapturedAndCaptureFailureIsIsolated() = runTest {
|
||||||
|
for (failure in listOf<IOException?>(null, IOException("Forced capture failure"))) {
|
||||||
|
val cached = library(empty = false)
|
||||||
|
val fresh = library(empty = false)
|
||||||
|
val restorer =
|
||||||
|
TestSnapshotRestorer(SharedSnapshotRestoration { cached }).apply {
|
||||||
|
captureFailure = failure
|
||||||
|
}
|
||||||
|
val context = mockk<Context>()
|
||||||
|
every { context.applicationContext } returns context
|
||||||
|
every { context.contentResolver } returns mockk()
|
||||||
|
val settings = mockk<MusicSettings>()
|
||||||
|
every { settings.separators } returns ",;"
|
||||||
|
every { settings.intelligentSorting } returns true
|
||||||
|
every { settings.revision } returns null
|
||||||
|
justRun { settings.revision = any() }
|
||||||
|
every { settings.locationMode } returns LocationMode.SAF
|
||||||
|
every { settings.safQuery } returns
|
||||||
|
SAF.Query(emptyList(), emptyList(), withHidden = false, multithread = false)
|
||||||
|
val covers = mockk<SettingCovers>()
|
||||||
|
coEvery { covers.mutate(context, any()) } returns mockk<MutableCovers<out Cover>>()
|
||||||
|
val repository =
|
||||||
|
MusicRepositoryImpl(
|
||||||
|
context,
|
||||||
|
mockk(),
|
||||||
|
mockk(),
|
||||||
|
covers,
|
||||||
|
settings,
|
||||||
|
restorer,
|
||||||
|
)
|
||||||
|
val emitted = mutableListOf<MutableLibrary?>()
|
||||||
|
repository.addUpdateListener(
|
||||||
|
object : MusicRepository.UpdateListener {
|
||||||
|
override fun onMusicChanges(changes: MusicRepository.Changes) {
|
||||||
|
emitted += repository.library
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
emitted.clear()
|
||||||
|
mockkObject(Musikr.Companion)
|
||||||
|
try {
|
||||||
|
val result = mockk<LibraryResult>()
|
||||||
|
every { result.library } returns fresh
|
||||||
|
coEvery { result.cleanup() } just Runs
|
||||||
|
val music = mockk<Musikr>()
|
||||||
|
coEvery { music.run(any()) } returns result
|
||||||
|
every { Musikr.new(any(), any()) } returns music
|
||||||
|
|
||||||
|
repository.index(mockk(), withCache = true)
|
||||||
|
} finally {
|
||||||
|
unmockkObject(Musikr.Companion)
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(listOf(cached, fresh), emitted)
|
||||||
|
assertSame(fresh, repository.library)
|
||||||
|
assertEquals(listOf(fresh), restorer.captures.map { it.first })
|
||||||
|
assertTrue(repository.indexingState is IndexingState.Completed)
|
||||||
|
assertNull((repository.indexingState as IndexingState.Completed).error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun scanBoundaryRepository(
|
||||||
|
restorer: SnapshotRestorer,
|
||||||
|
scanCalls: AtomicInteger,
|
||||||
|
): MusicRepositoryImpl {
|
||||||
|
val settings = mockk<MusicSettings>()
|
||||||
|
every { settings.separators } answers {
|
||||||
|
scanCalls.incrementAndGet()
|
||||||
|
""
|
||||||
|
}
|
||||||
|
every { settings.intelligentSorting } returns true
|
||||||
|
every { settings.revision } returns null
|
||||||
|
val covers = mockk<SettingCovers>()
|
||||||
|
coEvery { covers.mutate(any(), any()) } throws
|
||||||
|
IllegalStateException("Expected test indexing boundary")
|
||||||
|
return MusicRepositoryImpl(mockk(), mockk(), mockk(), covers, settings, restorer)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun library(empty: Boolean): MutableLibrary {
|
||||||
|
val library = mockk<MutableLibrary>()
|
||||||
|
every { library.empty() } returns empty
|
||||||
|
if (!empty) {
|
||||||
|
val name = mockk<Name.Known>()
|
||||||
|
every { name.raw } returns "Snapshot Song"
|
||||||
|
val song = mockk<Song>()
|
||||||
|
every { song.name } returns name
|
||||||
|
every { song.replayGainAdjustment } returns ReplayGainAdjustment(null, null)
|
||||||
|
every { library.songs } returns setOf(song)
|
||||||
|
} else {
|
||||||
|
every { library.songs } returns emptySet()
|
||||||
|
}
|
||||||
|
every { library.playlists } returns emptySet()
|
||||||
|
return library
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestSnapshotRestorer(
|
||||||
|
private val restoration: SnapshotRestoration
|
||||||
|
) : SnapshotRestorer {
|
||||||
|
val captures = mutableListOf<Pair<Library, Config>>()
|
||||||
|
var captureFailure: Exception? = null
|
||||||
|
|
||||||
|
override fun start() = restoration
|
||||||
|
|
||||||
|
override suspend fun capture(library: Library, config: Config) {
|
||||||
|
captures += library to config
|
||||||
|
captureFailure?.let { throw it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user