Skip to content

Commit

Permalink
AddonCollectionProvider: Delete unused cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
csadilek authored and mergify[bot] committed Sep 24, 2020
1 parent 57225e0 commit f988e49
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal const val API_VERSION = "api/v4"
internal const val DEFAULT_SERVER_URL = "https://addons.mozilla.org"
internal const val DEFAULT_COLLECTION_USER = "mozilla"
internal const val DEFAULT_COLLECTION_NAME = "7e8d6dc651b54ab385fb8791bf9dac"
internal const val COLLECTION_FILE_NAME = "mozilla_components_addon_collection_%s.json"
internal const val COLLECTION_FILE_NAME_PREFIX = "mozilla_components_addon_collection"
internal const val COLLECTION_FILE_NAME = "${COLLECTION_FILE_NAME_PREFIX}_%s.json"
internal const val MINUTE_IN_MS = 60 * 1000
internal const val DEFAULT_READ_TIMEOUT_IN_SECONDS = 20L

Expand Down Expand Up @@ -132,6 +133,7 @@ class AddonCollectionProvider(
if (maxCacheAgeInMinutes > 0) {
writeToDiskCache(responseBody)
}
deleteUnusedCacheFiles()
}
} catch (e: JSONException) {
throw IOException(e)
Expand Down Expand Up @@ -183,6 +185,22 @@ class AddonCollectionProvider(
}
}

/**
* Deletes cache files from previous (now unused) collections.
*/
@VisibleForTesting
internal fun deleteUnusedCacheFiles() {
val currentCacheFileName = getCacheFileName()
context.filesDir
.listFiles {
_, s -> s.startsWith(COLLECTION_FILE_NAME_PREFIX) && s != currentCacheFileName
}
?.forEach {
logger.debug("Deleting unused collection cache: " + it.name)
it.delete()
}
}

@VisibleForTesting
internal fun cacheExpired(context: Context): Boolean {
return getCacheLastUpdated(context) < Date().time - maxCacheAgeInMinutes * MINUTE_IN_MS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.junit.runner.RunWith
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import java.io.File
import java.io.IOException
import java.util.Date
import java.io.InputStream
Expand Down Expand Up @@ -219,6 +220,38 @@ class AddonCollectionProviderTest {
verify(cachingProvider).writeToDiskCache(jsonResponse)
}

@Test
fun `getAvailableAddons - deletes unused cache files`() = runBlocking {
val jsonResponse = loadResourceAsString("/collection.json")
val mockedClient = prepareClient(jsonResponse)

val provider = spy(AddonCollectionProvider(testContext, client = mockedClient, maxCacheAgeInMinutes = 1))

provider.getAvailableAddons()
verify(provider).deleteUnusedCacheFiles()
}

@Test
fun `deleteUnusedCacheFiles - only deletes collection cache files`() {
val regularFile = File(testContext.filesDir, "test.json")
regularFile.createNewFile()
assertTrue(regularFile.exists())

val regularDir = File(testContext.filesDir, "testDir")
regularDir.mkdir()
assertTrue(regularDir.exists())

val collectionFile = File(testContext.filesDir, COLLECTION_FILE_NAME.format("testCollection"))
collectionFile.createNewFile()
assertTrue(collectionFile.exists())

val provider = AddonCollectionProvider(testContext, client = prepareClient(), maxCacheAgeInMinutes = 1)
provider.deleteUnusedCacheFiles()
assertTrue(regularFile.exists())
assertTrue(regularDir.exists())
assertFalse(collectionFile.exists())
}

@Test
fun `getAvailableAddons - cache expiration check`() {
var provider = spy(AddonCollectionProvider(testContext, client = mock(), maxCacheAgeInMinutes = -1))
Expand Down

0 comments on commit f988e49

Please sign in to comment.