Skip to content

Commit eac3c8c

Browse files
committed
Apply suggestions from review
1 parent 5c9a81e commit eac3c8c

9 files changed

Lines changed: 177 additions & 27 deletions

File tree

‎data/src/main/java/org/cryptomator/data/documents/DocumentMetadataCache.kt‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,16 @@ class DocumentMetadataCache @JvmOverloads constructor(
259259
}
260260

261261
/**
262-
* Lists [folder] from the cloud now and fails if that fails.
262+
* Lists [folder] from the cloud now and fails if that fails. A listing that keeps being overtaken by changes to
263+
* [folder] counts as done, since the cache holds those changes already, see [enumerateNow].
263264
*/
264265
fun listNow(folder: VaultPath) {
265266
ensureFolder(folder)
266-
enumerate(folder)
267+
try {
268+
enumerate(folder)
269+
} catch (e: FolderKeptChangingException) {
270+
LOG.i("${logId(folder)} kept changing while it was listed, the cache holds the changes")
271+
}
267272
}
268273

269274
/**
@@ -1395,7 +1400,7 @@ class DocumentMetadataCache @JvmOverloads constructor(
13951400
events.onChanged(folder)
13961401
}
13971402

1398-
internal fun cloudFolder(folder: VaultPath): CloudFolder {
1403+
private fun cloudFolder(folder: VaultPath): CloudFolder {
13991404
val cloud = cloudRepository.decryptedViewOf(folder.vault)
14001405
//resolve() can't return the root: "/" resolves to a child with an empty name
14011406
return if (folder.isRoot) contentRepository.root(cloud) else contentRepository.resolve(cloud, "/${folder.path}")

‎data/src/main/java/org/cryptomator/data/documents/VaultDocumentRepositoryImpl.kt‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ class VaultDocumentRepositoryImpl @Inject constructor(
8686
return node(parent, metadata)
8787
}
8888

89-
override fun resolve(folder: CloudFolder): CloudFolder = cacheExceptions {
90-
metadataCache.cloudFolder(requireNotNull(pathOf(folder)) { "Not inside a vault" })
91-
}
92-
9389
private fun node(parent: CachedVaultFolder, metadata: DocumentMetadataEntity): CloudNode {
9490
if (metadata.isFolder) {
9591
return CachedVaultFolder(parent.cloud, "${parent.path}/${metadata.name}")

‎data/src/test/java/org/cryptomator/data/documents/DocumentMetadataCacheTest.kt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,25 @@ class DocumentMetadataCacheTest {
21902190
assertTrue(listings.isEmpty())
21912191
}
21922192

2193+
@Test
2194+
fun `a forced listing that keeps being overtaken by changes to the folder is done, the cache holds the changes`() {
2195+
val inTest = cache(CloudTaskScheduler(direct, direct, direct))
2196+
whenever(dao.insert(any())).thenReturn(ID)
2197+
whenever(repository.folder(root, "new")).thenReturn(mock())
2198+
whenever(dao.load(VAULT_ID, "b")).thenReturn(entity("b", Document.MIME_TYPE_DIR))
2199+
var listCalls = 0
2200+
whenever(repository.list(root)).thenAnswer {
2201+
listCalls++
2202+
inTest.create(path("b"), "new", isFolder = true)
2203+
listOf(cryptoFile("b/listed.txt"))
2204+
}
2205+
2206+
inTest.listNow(path("b"))
2207+
2208+
assertEquals(3, listCalls)
2209+
verify(dao, never()).storeChildren(any(), any())
2210+
}
2211+
21932212
@Test
21942213
fun `a new file's upload is based on the version its creation left in the cloud`() {
21952214
givenUpload(remoteModified = 5_400, lastModified = 5_000)

‎domain/src/main/java/org/cryptomator/domain/repository/VaultDocumentRepository.kt‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ interface VaultDocumentRepository {
3636
* The children of [folder] as the cache knows them, as [CachedVaultFile][org.cryptomator.domain.CachedVaultFile]s and
3737
* [CachedVaultFolder][org.cryptomator.domain.CachedVaultFolder]s. A folder whose listing is due is listed from the
3838
* cloud in the background, and the listeners are told once it is done. With [forceRefresh], it is listed right away,
39-
* and a failure is thrown.
39+
* and a failure is thrown, unless the listing only kept being overtaken by changes to the folder, which the cache
40+
* holds already.
4041
*/
4142
@Throws(BackendException::class)
4243
fun list(folder: CloudFolder, forceRefresh: Boolean): VaultListing
4344

44-
/**
45-
* The folder of the vault's decrypted view in the cloud at [folder]'s path, for working with the cloud directly.
46-
*/
47-
@Throws(BackendException::class)
48-
fun resolve(folder: CloudFolder): CloudFolder
49-
5045
@Throws(BackendException::class)
5146
fun createFolder(folder: VaultPath, name: String, userInitiated: Boolean): VaultDocumentChange<CloudFolder>
5247

‎domain/src/main/java/org/cryptomator/domain/usecases/cloud/GetCloudListRecursive.java‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,13 @@ class GetCloudListRecursive {
3131
public CloudNodeRecursiveListing execute() throws BackendException {
3232
CloudNodeRecursiveListing cloudNodeRecursiveListing = new CloudNodeRecursiveListing(folders.size());
3333
for (CloudFolder folder : folders) {
34-
// a folder from a vault's cache is resolved to the vault's folder in the cloud, which the recursive
35-
// listing needs
36-
CloudFolder listedFolder = vaultDocumentRepository.pathOf(folder) != null ? vaultDocumentRepository.resolve(folder) : folder;
37-
cloudNodeRecursiveListing.addFolderContent(recursiveListing(new CloudFolderRecursiveListing(listedFolder), listedFolder));
34+
cloudNodeRecursiveListing.addFolderContent(recursiveListing(new CloudFolderRecursiveListing(folder), folder));
3835
}
3936
return cloudNodeRecursiveListing;
4037
}
4138

4239
private CloudFolderRecursiveListing recursiveListing(CloudFolderRecursiveListing cloudFolderRecursiveListing, CloudFolder folder) throws BackendException {
43-
List<CloudNode> children = cloudContentRepository.list(folder);
40+
List<CloudNode> children = list(folder);
4441
for (CloudNode child : children) {
4542
if (child instanceof CloudFolder) {
4643
cloudFolderRecursiveListing.addFolders(//
@@ -53,4 +50,15 @@ private CloudFolderRecursiveListing recursiveListing(CloudFolderRecursiveListing
5350
return cloudFolderRecursiveListing;
5451
}
5552

53+
/**
54+
* A folder of a vault is listed through the vault's cache, so that files whose upload is still pending or has failed
55+
* are included, as the file browser shows them.
56+
*/
57+
private List<CloudNode> list(CloudFolder folder) throws BackendException {
58+
if (vaultDocumentRepository.pathOf(folder) != null) {
59+
return vaultDocumentRepository.list(folder, true).getNodes();
60+
}
61+
return cloudContentRepository.list(folder);
62+
}
63+
5664
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.cryptomator.domain.usecases.cloud
2+
3+
import org.cryptomator.domain.CachedVaultFile
4+
import org.cryptomator.domain.CachedVaultFolder
5+
import org.cryptomator.domain.Cloud
6+
import org.cryptomator.domain.CloudFile
7+
import org.cryptomator.domain.CloudFolder
8+
import org.cryptomator.domain.CloudNode
9+
import org.cryptomator.domain.CloudType
10+
import org.cryptomator.domain.Vault
11+
import org.cryptomator.domain.VaultPath
12+
import org.cryptomator.domain.exception.NetworkConnectionException
13+
import org.cryptomator.domain.repository.CloudContentRepository
14+
import org.cryptomator.domain.repository.VaultDocumentRepository
15+
import org.cryptomator.domain.repository.VaultListing
16+
import org.junit.jupiter.api.Assertions.assertEquals
17+
import org.junit.jupiter.api.Assertions.assertSame
18+
import org.junit.jupiter.api.Test
19+
import org.junit.jupiter.api.assertThrows
20+
import org.mockito.kotlin.any
21+
import org.mockito.kotlin.mock
22+
import org.mockito.kotlin.never
23+
import org.mockito.kotlin.verify
24+
import org.mockito.kotlin.verifyNoInteractions
25+
import org.mockito.kotlin.whenever
26+
27+
class GetCloudListRecursiveTest {
28+
29+
private val vault = Vault.aVault().withId(1L).withName("vault").withPath("/vault").withCloudType(CloudType.LOCAL).withPosition(0).build()
30+
private val cloudContentRepository: CloudContentRepository<Cloud, CloudNode, CloudFolder, CloudFile> = mock()
31+
private val vaultDocumentRepository: VaultDocumentRepository = mock()
32+
33+
@Test
34+
fun `a vault folder is listed through the vault's cache, so that pending uploads are included`() {
35+
val folder = CachedVaultFolder(null, "/a")
36+
val subfolder = CachedVaultFolder(null, "/a/b")
37+
val pending = CachedVaultFile(folder, "pending.txt", 3, null, CachedVaultFile.Upload.PENDING, null)
38+
val uploaded = CachedVaultFile(subfolder, "uploaded.txt", 5, null, CachedVaultFile.Upload.NONE, null)
39+
whenever(vaultDocumentRepository.pathOf(folder)).thenReturn(VaultPath(vault, "a"))
40+
whenever(vaultDocumentRepository.pathOf(subfolder)).thenReturn(VaultPath(vault, "a/b"))
41+
whenever(vaultDocumentRepository.list(folder, true)).thenReturn(VaultListing(listOf(subfolder, pending), false, null))
42+
whenever(vaultDocumentRepository.list(subfolder, true)).thenReturn(VaultListing(listOf(uploaded), false, null))
43+
44+
val listing = GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute()
45+
46+
val content = listing.foldersContent.single()
47+
assertSame(folder, content.parent)
48+
assertEquals(listOf<CloudFile>(pending), content.files)
49+
assertSame(subfolder, content.folders.single().parent)
50+
assertEquals(listOf<CloudFile>(uploaded), content.folders.single().files)
51+
verifyNoInteractions(cloudContentRepository)
52+
}
53+
54+
@Test
55+
fun `a folder outside a vault is listed from the cloud`() {
56+
val folder: CloudFolder = mock()
57+
val file: CloudFile = mock()
58+
whenever(cloudContentRepository.list(folder)).thenReturn(listOf(file))
59+
60+
val listing = GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute()
61+
62+
assertEquals(listOf(file), listing.foldersContent.single().files)
63+
verify(vaultDocumentRepository, never()).list(any(), any())
64+
}
65+
66+
@Test
67+
fun `a vault folder that can't be listed fails the listing`() {
68+
val folder = CachedVaultFolder(null, "/a")
69+
whenever(vaultDocumentRepository.pathOf(folder)).thenReturn(VaultPath(vault, "a"))
70+
whenever(vaultDocumentRepository.list(folder, true)).thenThrow(NetworkConnectionException())
71+
72+
assertThrows<NetworkConnectionException> { GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute() }
73+
}
74+
}

‎presentation/src/main/java/org/cryptomator/presentation/service/AutolockTimeout.java‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,33 @@ public void setLockTimeout(LockTimeout lockTimeout) {
4141
this.lockTimeout = lockTimeout;
4242
}
4343

44+
// the fields are read once per call: the autolock worker polls while the app changes them, and asking a disabled
45+
// timeout for its duration throws
46+
4447
public boolean expired() {
45-
return !isDisabled() && timeRemaining() <= 0;
48+
LockTimeout timeout = lockTimeout;
49+
return !isDisabled(timeout) && remaining(timeout) <= 0;
4650
}
4751

4852
public int timeRemaining() {
49-
if (isDisabled()) {
50-
return 0;
51-
}
52-
return (int) (lastUsed + configuredTimeout() - currentTimeMillis.getAsLong());
53+
LockTimeout timeout = lockTimeout;
54+
return isDisabled(timeout) ? 0 : remaining(timeout);
5355
}
5456

5557
public long configuredTimeout() {
56-
return isDisabled() ? 0 : lockTimeout.getDurationMillis();
58+
LockTimeout timeout = lockTimeout;
59+
return isDisabled(timeout) ? 0 : timeout.getDurationMillis();
5760
}
5861

5962
public boolean isDisabled() {
60-
return appIsActive || lockTimeout.isDisabled();
63+
return isDisabled(lockTimeout);
64+
}
65+
66+
private boolean isDisabled(LockTimeout timeout) {
67+
return appIsActive || timeout.isDisabled();
68+
}
69+
70+
private int remaining(LockTimeout timeout) {
71+
return (int) (lastUsed + timeout.getDurationMillis() - currentTimeMillis.getAsLong());
6172
}
6273
}

‎presentation/src/test/java/org/cryptomator/presentation/service/AutolockTimeoutTest.kt‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import org.junit.jupiter.api.Assertions.assertEquals
55
import org.junit.jupiter.api.Assertions.assertFalse
66
import org.junit.jupiter.api.Assertions.assertTrue
77
import org.junit.jupiter.api.Test
8+
import org.mockito.kotlin.mock
9+
import org.mockito.kotlin.whenever
810

911
class AutolockTimeoutTest {
1012

@@ -19,6 +21,8 @@ class AutolockTimeoutTest {
1921

2022
assertTrue(inTest.isDisabled)
2123
assertFalse(inTest.expired())
24+
assertEquals(0, inTest.timeRemaining())
25+
assertEquals(0L, inTest.configuredTimeout())
2226
}
2327

2428
@Test
@@ -52,6 +56,43 @@ class AutolockTimeoutTest {
5256
now += 3_600_000
5357

5458
assertFalse(inTest.expired())
59+
assertEquals(0, inTest.timeRemaining())
60+
assertEquals(0L, inTest.configuredTimeout())
61+
}
62+
63+
// the autolock worker checks the timeout while the app changes it, so each check works on what it read first
64+
65+
@Test
66+
fun `the app coming to the foreground during a check doesn't expire the timeout`() {
67+
val timeout = timeout { inTest.setAppIsActive(true) }
68+
inTest.setAppIsActive(false)
69+
inTest.setLockTimeout(timeout)
70+
now += 120_000
71+
72+
assertFalse(inTest.expired())
73+
}
74+
75+
@Test
76+
fun `the timeout turned off during a check doesn't fail the check`() {
77+
val timeout = timeout { inTest.setLockTimeout(LockTimeout.NEVER) }
78+
inTest.setAppIsActive(false)
79+
inTest.setLockTimeout(timeout)
80+
now += 120_000
81+
82+
assertEquals(60_000L, inTest.configuredTimeout())
5583
assertEquals(0L, inTest.configuredTimeout())
5684
}
85+
86+
/**
87+
* A one-minute timeout whose first use in a check runs [duringCheck], like another thread changing the settings.
88+
*/
89+
private fun timeout(duringCheck: () -> Unit): LockTimeout {
90+
val timeout: LockTimeout = mock()
91+
whenever(timeout.durationMillis).thenReturn(60_000L)
92+
whenever(timeout.isDisabled).thenAnswer {
93+
duringCheck()
94+
false
95+
}
96+
return timeout
97+
}
5798
}

‎presentation/src/test/java/org/cryptomator/presentation/ui/adapter/NodeRowsTest.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class NodeRowsTest {
5353

5454
@Test
5555
fun `a changed upload state or thumbnail changes the row`() {
56-
assertNotSame(file(upload = CachedVaultFile.Upload.PENDING), rowNode(file(upload = CachedVaultFile.Upload.PENDING), file(upload = CachedVaultFile.Upload.FAILED)))
56+
val pending = file(upload = CachedVaultFile.Upload.PENDING)
57+
assertNotSame(pending, rowNode(pending, file(upload = CachedVaultFile.Upload.FAILED)))
5758
val old = file()
5859
assertNotSame(old, rowNode(old, file(thumbnail = File("thumbnail.jpg"))))
5960
}

0 commit comments

Comments
 (0)