Fix bug where we could not do two subsequent restores

This probably never showed in practice, but it can be triggered easily
when testing with `adb shell bmgr restore`.
This commit is contained in:
Torsten Grote 2020-09-17 14:00:46 -03:00 committed by Chirayu Desai
parent 5f771ff4ec
commit 1e3263ec54
2 changed files with 15 additions and 2 deletions

View file

@ -25,7 +25,7 @@ import com.stevesoltys.seedvault.ui.notification.BackupNotificationManager
import libcore.io.IoUtils.closeQuietly import libcore.io.IoUtils.closeQuietly
import java.io.IOException import java.io.IOException
private class RestoreCoordinatorState( private data class RestoreCoordinatorState(
val token: Long, val token: Long,
val packages: Iterator<PackageInfo>, val packages: Iterator<PackageInfo>,
/** /**
@ -124,7 +124,7 @@ internal class RestoreCoordinator(
* or [TRANSPORT_ERROR] (an error occurred, the restore should be aborted and rescheduled). * or [TRANSPORT_ERROR] (an error occurred, the restore should be aborted and rescheduled).
*/ */
fun startRestore(token: Long, packages: Array<out PackageInfo>): Int { fun startRestore(token: Long, packages: Array<out PackageInfo>): Int {
check(state == null) { "Started new restore with existing state" } check(state == null) { "Started new restore with existing state: $state" }
Log.i(TAG, "Start restore with ${packages.map { info -> info.packageName }}") Log.i(TAG, "Start restore with ${packages.map { info -> info.packageName }}")
// If there's only one package to restore (Auto Restore feature), add it to the state // If there's only one package to restore (Auto Restore feature), add it to the state
@ -251,6 +251,7 @@ internal class RestoreCoordinator(
* or will call [finishRestore] to shut down the restore operation. * or will call [finishRestore] to shut down the restore operation.
*/ */
fun abortFullRestore(): Int { fun abortFullRestore(): Int {
Log.d(TAG, "abortFullRestore")
state?.currentPackage?.let { failedPackages.add(it) } state?.currentPackage?.let { failedPackages.add(it) }
return full.abortFullRestore() return full.abortFullRestore()
} }
@ -260,7 +261,9 @@ internal class RestoreCoordinator(
* freeing any resources and connections used during the restore process. * freeing any resources and connections used during the restore process.
*/ */
fun finishRestore() { fun finishRestore() {
Log.d(TAG, "finishRestore")
if (full.hasState()) full.finishRestore() if (full.hasState()) full.finishRestore()
state = null
} }
/** /**

View file

@ -108,6 +108,16 @@ internal class RestoreCoordinatorTest : TransportTest() {
} }
} }
@Test
fun `startRestore() can be be called again after restore finished`() {
assertEquals(TRANSPORT_OK, restore.startRestore(token, packageInfoArray))
every { full.hasState() } returns false
restore.finishRestore()
assertEquals(TRANSPORT_OK, restore.startRestore(token, packageInfoArray))
}
@Test @Test
fun `startRestore() optimized auto-restore with removed storage shows notification`() { fun `startRestore() optimized auto-restore with removed storage shows notification`() {
every { settingsManager.getStorage() } returns storage every { settingsManager.getStorage() } returns storage