Skip to content

Commit dd3a6af

Browse files
authored
Clear SDK cache properly (#4396)
* Use close() instead of destroy, because close() is synchronized. * Use new method to clear the SDK cache. * Format file. * Remove the legacy way to clear the SDK cache. * Remove unused import * revert name change
1 parent 811ba95 commit dd3a6af

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package io.element.android.libraries.matrix.impl
99

1010
import io.element.android.libraries.androidutils.file.getSizeOfFiles
11-
import io.element.android.libraries.androidutils.file.safeDelete
1211
import io.element.android.libraries.core.bool.orFalse
1312
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
1413
import io.element.android.libraries.core.coroutine.childScope
@@ -488,20 +487,20 @@ class RustMatrixClient(
488487
verificationService.destroy()
489488

490489
sessionDelegate.clearCurrentClient()
491-
innerRoomListService.destroy()
492-
notificationService.destroy()
490+
innerRoomListService.close()
491+
notificationService.close()
493492
notificationProcessSetup.destroy()
494-
encryptionService.destroy()
495-
innerClient.destroy()
493+
encryptionService.close()
494+
innerClient.close()
496495
}
497496

498497
override suspend fun getCacheSize(): Long {
499498
return baseDirectory.getCacheSize()
500499
}
501500

502501
override suspend fun clearCache() {
502+
innerClient.clearCaches()
503503
close()
504-
deleteSessionDirectory(deleteCryptoDb = false)
505504
}
506505

507506
override suspend fun logout(userInitiated: Boolean, ignoreSdkError: Boolean) {
@@ -526,7 +525,7 @@ class RustMatrixClient(
526525
}
527526
close()
528527

529-
deleteSessionDirectory(deleteCryptoDb = true)
528+
deleteSessionDirectory()
530529
if (userInitiated) {
531530
sessionStore.removeSession(sessionId.value)
532531
}
@@ -575,7 +574,7 @@ class RustMatrixClient(
575574
}
576575
}
577576
close()
578-
deleteSessionDirectory(deleteCryptoDb = true)
577+
deleteSessionDirectory()
579578
sessionStore.removeSession(sessionId.value)
580579
}.onFailure {
581580
Timber.e(it, "Failed to deactivate account")
@@ -663,25 +662,9 @@ class RustMatrixClient(
663662
}
664663
}
665664

666-
private suspend fun deleteSessionDirectory(
667-
deleteCryptoDb: Boolean = false,
668-
): Boolean = withContext(sessionDispatcher) {
669-
val sessionPaths = sessionPathsProvider.provides(sessionId) ?: return@withContext false
670-
// Always delete the cache directory
671-
sessionPaths.cacheDirectory.deleteRecursively()
672-
if (deleteCryptoDb) {
673-
// Delete the folder and all its content
674-
sessionPaths.fileDirectory.deleteRecursively()
675-
} else {
676-
// Do not delete the crypto database files.
677-
sessionPaths.fileDirectory.listFiles().orEmpty()
678-
.filterNot { it.name.contains("matrix-sdk-crypto") }
679-
.forEach { file ->
680-
Timber.w("Deleting file ${file.name}...")
681-
file.safeDelete()
682-
}
683-
true
684-
}
665+
private suspend fun deleteSessionDirectory() = withContext(sessionDispatcher) {
666+
// Delete all the files for this session
667+
sessionPathsProvider.provides(sessionId)?.deleteRecursively()
685668
}
686669
}
687670

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/encryption/RustEncryptionService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ internal class RustEncryptionService(
232232
) ?: error("User identity not found")
233233
}
234234

235-
fun destroy() {
236-
service.destroy()
235+
fun close() {
236+
service.close()
237237
}
238238
}

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/notification/RustNotificationService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RustNotificationService(
3636
}
3737
}
3838

39-
fun destroy() {
40-
notificationClient.destroy()
39+
fun close() {
40+
notificationClient.close()
4141
}
4242
}

libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientTest.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import io.element.android.libraries.matrix.test.A_USER_ID
1717
import io.element.android.libraries.sessionstorage.api.SessionStore
1818
import io.element.android.libraries.sessionstorage.impl.memory.InMemorySessionStore
1919
import io.element.android.services.toolbox.test.systemclock.FakeSystemClock
20+
import io.element.android.tests.testutils.lambda.lambdaRecorder
2021
import io.element.android.tests.testutils.testCoroutineDispatchers
2122
import kotlinx.coroutines.test.TestScope
2223
import kotlinx.coroutines.test.runTest
2324
import org.junit.Test
25+
import org.matrix.rustcomponents.sdk.Client
2426
import java.io.File
2527

2628
class RustMatrixClientTest {
@@ -32,10 +34,27 @@ class RustMatrixClientTest {
3234
}
3335
}
3436

37+
@Test
38+
fun `clear cache invokes the method clearCaches from the client and close it`() = runTest {
39+
val clearCachesResult = lambdaRecorder<Unit> { }
40+
val closeResult = lambdaRecorder<Unit> { }
41+
createRustMatrixClient(
42+
client = FakeRustClient(
43+
clearCachesResult = clearCachesResult,
44+
closeResult = closeResult,
45+
)
46+
).use { sut ->
47+
sut.clearCache()
48+
clearCachesResult.assertions().isCalledOnce()
49+
closeResult.assertions().isCalledOnce()
50+
}
51+
}
52+
3553
private fun TestScope.createRustMatrixClient(
54+
client: Client = FakeRustClient(),
3655
sessionStore: SessionStore = InMemorySessionStore(),
3756
) = RustMatrixClient(
38-
innerClient = FakeRustClient(),
57+
innerClient = client,
3958
baseDirectory = File(""),
4059
sessionStore = sessionStore,
4160
appCoroutineScope = backgroundScope,

libraries/matrix/impl/src/test/kotlin/io/element/android/libraries/matrix/impl/fixtures/fakes/FakeRustClient.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package io.element.android.libraries.matrix.impl.fixtures.fakes
1010
import io.element.android.libraries.matrix.impl.fixtures.factories.aRustSession
1111
import io.element.android.libraries.matrix.test.A_DEVICE_ID
1212
import io.element.android.libraries.matrix.test.A_USER_ID
13+
import io.element.android.tests.testutils.lambda.lambdaError
14+
import io.element.android.tests.testutils.simulateLongTask
1315
import org.matrix.rustcomponents.sdk.Client
1416
import org.matrix.rustcomponents.sdk.ClientDelegate
1517
import org.matrix.rustcomponents.sdk.Encryption
@@ -31,6 +33,8 @@ class FakeRustClient(
3133
private val notificationSettings: NotificationSettings = FakeRustNotificationSettings(),
3234
private val encryption: Encryption = FakeRustEncryption(),
3335
private val session: Session = aRustSession(),
36+
private val clearCachesResult: () -> Unit = { lambdaError() },
37+
private val closeResult: () -> Unit = {},
3438
) : Client(NoPointer) {
3539
override fun userId(): String = userId
3640
override fun deviceId(): String = deviceId
@@ -53,4 +57,6 @@ class FakeRustClient(
5357
) = Unit
5458

5559
override suspend fun deletePusher(identifiers: PusherIdentifiers) = Unit
60+
override suspend fun clearCaches() = simulateLongTask { clearCachesResult() }
61+
override fun close() = closeResult()
5662
}

libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/FakeMatrixClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class FakeMatrixClient(
167167
return 0
168168
}
169169

170-
override suspend fun clearCache() {
170+
override suspend fun clearCache() = simulateLongTask {
171171
clearCacheLambda()
172172
}
173173

0 commit comments

Comments
 (0)