Reset latest snapshot to avoid it getting stale

When switching to new storage that doesn't yet have any snapshots, we would otherwise keep the old latest snapshot around.
This commit is contained in:
Torsten Grote 2024-10-01 16:49:00 -03:00
parent 7696b88a5a
commit 11270ff694
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
2 changed files with 8 additions and 0 deletions

View file

@ -47,6 +47,9 @@ internal class SnapshotManager(
* currently available on the backend.
*/
suspend fun onSnapshotsLoaded(handles: List<AppBackupFileType.Snapshot>): List<Snapshot> {
// first reset latest snapshot, otherwise we'd hang on to a stale one
// e.g. when switching to new storage without any snapshots
latestSnapshot = null
return handles.mapNotNull { snapshotHandle ->
val snapshot = try {
loadSnapshot(snapshotHandle)

View file

@ -23,6 +23,7 @@ import org.calyxos.seedvault.core.toHexString
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
@ -75,6 +76,10 @@ internal class SnapshotManagerTest : TransportTest() {
// snapshot with largest token is latest
assertEquals(20, snapshotManager.latestSnapshot?.token)
// when switching storage and now not having any snapshots, we must clear latestSnapshot
snapshotManager.onSnapshotsLoaded(emptyList())
assertNull(snapshotManager.latestSnapshot)
}
@Test