Test for free space and allow 0

This commit is contained in:
Torsten Grote 2024-10-22 14:04:19 -03:00
parent 0fa14025b3
commit f1224f09f8
No known key found for this signature in database
GPG key ID: 3E5F77D92CF891FF
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

@ -115,6 +115,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()
}
} }