Skip to content

DiskCache.maxSize calculates incorrectly #1619

@Samuel-Unknown

Description

@Samuel-Unknown

Describe the bug
I found a problem with DiskCache. It calculates maxSize incorrectly.

To Reproduce
If you create a DiskCache by setting maxSizePercent and a directory that doesn't exist (e.g. the first run before putting anything in the cache), maxSize will be equal to minimumMaxSizeBytes.

You can't catch this with Robolectric tests, but you can with InstrumentedTests:

@Test
fun `checkInitSize`() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val minimumMaxSizeBytes = 100L
val maximumMaxSizeBytes = 200L
val maxSizePercent = 0.5

val diskCache = DiskCache.Builder().directory(appContext.filesDir.resolve("test_dir"))
   .maxSizePercent(maxSizePercent)
   .minimumMaxSizeBytes(minimumMaxSizeBytes)
   .maximumMaxSizeBytes(maximumMaxSizeBytes)
   .build()

    assertNotEquals(minimumMaxSizeBytes, diskCache.maxSize)
}

Version
2.2.2

P.S. Workaround:

DiskCache.Builder()
   .directory(context.filesDir.resolve(CACHE_DIR_NAME))
   .maxSizePercent(0.5)
   .also {
      if (context.filesDir.resolve(CACHE_DIR_NAME).exists().not()) {
         context.filesDir.resolve(CACHE_DIR_NAME).mkdir()
      }
   }
   .build()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions