Skip to content

Commit

Permalink
Add test that snapshot saving can be re-tried
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Jan 7, 2025
1 parent 8edc3bc commit 8ee538a
Showing 1 changed file with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,59 @@ internal class SnapshotManagerTest : TransportTest() {
}
}

@Test
fun `test that saving can be re-tried`(@TempDir tmpDir: Path) = runBlocking {
val loader = Loader(crypto, backendManager) // need a real loader
val snapshotManager = getSnapshotManager(File(tmpDir.toString()), loader)

val bytes = slot<ByteArray>()
val outputStream1 = ByteArrayOutputStream()
val outputStream2 = ByteArrayOutputStream()
val outputStream3 = ByteArrayOutputStream()

every { crypto.getAdForVersion() } returns ad
every { crypto.newEncryptingStream(capture(passThroughOutputStream), ad) } answers {
passThroughOutputStream.captured // not really encrypting here
}
every { crypto.repoId } returns repoId
every { crypto.sha256(capture(bytes)) } answers {
messageDigest.digest(bytes.captured)
}
val saverSlot = slot<BackendSaver>()
var size1 = -1L
var size2 = -1L
coEvery { backend.save(capture(snapshotHandle), capture(saverSlot)) } answers {
// saver saves 3 times
size1 = saverSlot.captured.save(outputStream1)
size2 = saverSlot.captured.save(outputStream2)
saverSlot.captured.save(outputStream3)
}

snapshotManager.saveSnapshot(snapshot)

// check that data was saved to all streams
assertTrue(outputStream1.size() > 0)
assertTrue(outputStream2.size() > 0)
assertTrue(outputStream3.size() > 0)

assertEquals(size1, outputStream1.size().toLong())
assertEquals(size2, outputStream2.size().toLong())

// check that file content hash matches snapshot hash for each write
assertEquals(
messageDigest.digest(outputStream1.toByteArray()).toHexString(),
snapshotHandle.captured.hash,
)
assertEquals(
messageDigest.digest(outputStream2.toByteArray()).toHexString(),
snapshotHandle.captured.hash,
)
assertEquals(
messageDigest.digest(outputStream3.toByteArray()).toHexString(),
snapshotHandle.captured.hash,
)
}

@Test
fun `remove snapshot removes from backend and cache`(@TempDir tmpDir: Path) = runBlocking {
val snapshotManager = getSnapshotManager(File(tmpDir.toString()))
Expand Down

0 comments on commit 8ee538a

Please sign in to comment.