when cached snapshot is corrupted fall back to loading from backend
This commit is contained in:
parent
14775b5c3e
commit
9339f9f0fb
2 changed files with 27 additions and 1 deletions
|
@ -133,7 +133,12 @@ internal class SnapshotManager(
|
|||
val file = File(snapshotFolder, snapshotHandle.name)
|
||||
snapshotFolder.mkdirs()
|
||||
val inputStream = if (file.isFile) {
|
||||
loader.loadFile(file, snapshotHandle.hash)
|
||||
try {
|
||||
loader.loadFile(file, snapshotHandle.hash)
|
||||
} catch (e: Exception) {
|
||||
log.error(e) { "Error loading $snapshotHandle from local cache. Trying backend..." }
|
||||
loader.loadFile(snapshotHandle, file)
|
||||
}
|
||||
} else {
|
||||
loader.loadFile(snapshotHandle, file)
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ import java.io.IOException
|
|||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
import java.nio.file.Path
|
||||
import java.security.GeneralSecurityException
|
||||
import java.security.MessageDigest
|
||||
import kotlin.random.Random
|
||||
|
||||
|
@ -132,6 +133,26 @@ internal class SnapshotManagerTest : TransportTest() {
|
|||
assertEquals(listOf(snapshot), snapshotManager.loadCachedSnapshots())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `snapshot corrupted on cache falls back to backend`(@TempDir tmpDir: Path) = runBlocking {
|
||||
val snapshotManager = getSnapshotManager(File(tmpDir.toString()))
|
||||
val snapshot = snapshot { token = 1337 }
|
||||
val snapshotData = snapshot.toByteArray()
|
||||
val inputStream = ByteArrayInputStream(snapshotData)
|
||||
val snapshotHandle = AppBackupFileType.Snapshot(repoId, chunkId1)
|
||||
val file = getSnapshotFolder(tmpDir, snapshotHandle.name)
|
||||
|
||||
every { crypto.repoId } returns repoId
|
||||
coEvery { loader.loadFile(file, snapshotHandle.hash) } throws GeneralSecurityException()
|
||||
coEvery { loader.loadFile(snapshotHandle, file) } returns inputStream
|
||||
|
||||
assertEquals(listOf(snapshot), snapshotManager.onSnapshotsLoaded(listOf(snapshotHandle)))
|
||||
|
||||
coVerify { // did load from backend
|
||||
loader.loadFile(snapshotHandle, file)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `failing to load a snapshot isn't fatal`(@TempDir tmpDir: Path) = runBlocking {
|
||||
val snapshotManager = getSnapshotManager(File(tmpDir.toString()))
|
||||
|
|
Loading…
Reference in a new issue