Merge pull request #784 from grote/check-storage

Test for free space and allow 0 response
This commit is contained in:
Torsten Grote 2024-11-20 13:38:10 -03:00 committed by GitHub
commit 25e88caf88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 1 deletions

View file

@ -36,4 +36,9 @@ class SafBackendTest : BackendTest(), KoinComponent {
fun `test remove create write file`(): Unit = runBlocking { fun `test remove create write file`(): Unit = runBlocking {
testRemoveCreateWriteFile() testRemoveCreateWriteFile()
} }
@Test
fun `test free space and create app blob without root folder`(): Unit = runBlocking {
testTestFreeSpaceAndCreateBlob()
}
} }

View file

@ -122,6 +122,7 @@ class AppBackupWorker(
Log.e(TAG, "Error while running setForeground: ", e) Log.e(TAG, "Error while running setForeground: ", e)
} }
val freeSpace = backendManager.getFreeSpace() val freeSpace = backendManager.getFreeSpace()
Log.i(TAG, "freeSpace: $freeSpace")
if (freeSpace != null && freeSpace < MIN_FREE_SPACE) { if (freeSpace != null && freeSpace < MIN_FREE_SPACE) {
nm.onInsufficientSpaceError() nm.onInsufficientSpaceError()
return Result.failure() return Result.failure()

View file

@ -12,6 +12,7 @@ import kotlin.test.assertContentEquals
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@VisibleForTesting @VisibleForTesting
public abstract class BackendTest { public abstract class BackendTest {
@ -159,4 +160,23 @@ public abstract class BackendTest {
} }
} }
protected suspend fun testTestFreeSpaceAndCreateBlob() {
assertTrue(backend.test())
// not asserting free space as each backend may have different amounts available
println("Free space: ${backend.getFreeSpace()}")
val repoId = Random.nextBytes(32).toHexString()
val blob = AppBackupFileType.Blob(repoId, Random.nextBytes(32).toHexString())
val bytes = Random.nextBytes(2342)
try {
backend.save(blob).use {
it.write(bytes)
}
assertContentEquals(bytes, backend.load(blob as FileHandle).readAllBytes())
} finally {
backend.remove(blob)
}
}
} }

View file

@ -111,7 +111,7 @@ public class WebDavBackend(
log.debugLog { "getFreeSpace() = $response" } log.debugLog { "getFreeSpace() = $response" }
val quota = response.properties.getOrNull(0) as? QuotaAvailableBytes val quota = response.properties.getOrNull(0) as? QuotaAvailableBytes
val availableBytes = quota?.quotaAvailableBytes ?: -1 val availableBytes = quota?.quotaAvailableBytes ?: -1
if (availableBytes > 0) { if (availableBytes >= 0) {
cont.resume(availableBytes) cont.resume(availableBytes)
} else { } else {
cont.resume(null) cont.resume(null)

View file

@ -22,4 +22,9 @@ public class WebDavBackendTest : BackendTest() {
public fun `test remove, create, write file`(): Unit = runBlocking { public fun `test remove, create, write file`(): Unit = runBlocking {
testRemoveCreateWriteFile() testRemoveCreateWriteFile()
} }
@Test
public fun `test, free space and create app blob without root folder`(): Unit = runBlocking {
testTestFreeSpaceAndCreateBlob()
}
} }