From 11270ff6945f6ea7a5d2f5c243e6fa7101e28689 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 1 Oct 2024 16:49:00 -0300 Subject: [PATCH] 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. --- .../java/com/stevesoltys/seedvault/repo/SnapshotManager.kt | 3 +++ .../com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt index 6d3bde26..15b2a931 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/repo/SnapshotManager.kt @@ -47,6 +47,9 @@ internal class SnapshotManager( * currently available on the backend. */ suspend fun onSnapshotsLoaded(handles: List): List { + // 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) diff --git a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt index ee712c7f..5104f5a6 100644 --- a/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt +++ b/app/src/test/java/com/stevesoltys/seedvault/repo/SnapshotManagerTest.kt @@ -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