Skip to content

Commit

Permalink
Test for free space and allow 0
Browse files Browse the repository at this point in the history
  • Loading branch information
grote committed Oct 22, 2024
1 parent 0fa1402 commit f1224f0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ class SafBackendTest : BackendTest(), KoinComponent {
fun `test remove create write file`(): Unit = runBlocking {
testRemoveCreateWriteFile()
}

@Test
fun `test free space and create app blob without root folder`(): Unit = runBlocking {
testTestFreeSpaceAndCreateBlob()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class AppBackupWorker(
Log.e(TAG, "Error while running setForeground: ", e)
}
val freeSpace = backendManager.getFreeSpace()
Log.i(TAG, "freeSpace: $freeSpace")
if (freeSpace != null && freeSpace < MIN_FREE_SPACE) {
nm.onInsufficientSpaceError()
return Result.failure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

@VisibleForTesting
public abstract class BackendTest {
Expand Down Expand Up @@ -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)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class WebDavBackend(
log.debugLog { "getFreeSpace() = $response" }
val quota = response.properties.getOrNull(0) as? QuotaAvailableBytes
val availableBytes = quota?.quotaAvailableBytes ?: -1
if (availableBytes > 0) {
if (availableBytes >= 0) {
cont.resume(availableBytes)
} else {
cont.resume(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class WebDavBackendTest : BackendTest() {
public fun `test remove, create, write file`(): Unit = runBlocking {
testRemoveCreateWriteFile()
}

@Test
public fun `test, free space and create app blob without root folder`(): Unit = runBlocking {
testTestFreeSpaceAndCreateBlob()
}
}

0 comments on commit f1224f0

Please sign in to comment.