-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset DiskLruCachWrapper outside of IOException catch block in clear().
DiskLruCache can close the disk cache but throw anyway in delete(), which previously left the disk cache in an unrecoverable state until the application was restarted. By always nulling out the disk cache variable we allow for future attempts at opening the disk cache even if closing it throws. This particular case was caused by a bug in DiskLruCache where it would throw on an empty directory if the cache were deleted outside the app while the app was still running, but broadly speaking it seems like a bad idea to allow our disk cache to get into a state where the app must be killed for it to function again, even if we might partially ignore a clear disk cache command (which wouldn’t be retried or recovered in the previous implementation anyway). Fixes #2465.
- Loading branch information
Showing
3 changed files
with
161 additions
and
15 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...entation/src/androidTest/java/com/bumptech/glide/test/ExternallyClearedDiskCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package com.bumptech.glide.test; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import android.content.Context; | ||
import android.graphics.drawable.Drawable; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.GlideBuilder; | ||
import com.bumptech.glide.load.Key; | ||
import com.bumptech.glide.load.engine.cache.DiskCache; | ||
import com.bumptech.glide.load.engine.cache.DiskCache.Factory; | ||
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper; | ||
import com.bumptech.glide.test.ResourceIds.raw; | ||
import java.io.File; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
// Tests #2465. | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExternallyClearedDiskCacheTest { | ||
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private final ConcurrencyHelper concurrency = new ConcurrencyHelper(); | ||
private Context context; | ||
private File cacheDir; | ||
|
||
@Before | ||
public void setUp() { | ||
context = InstrumentationRegistry.getTargetContext(); | ||
cacheDir = context.getCacheDir(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
// Force us to wait until Glide's threads shut down. | ||
Glide.tearDown(); | ||
deleteRecursively(cacheDir); | ||
} | ||
|
||
@Test | ||
public void clearDiskCache_afterOpeningDiskCache_andDeleteDirectoryOutsideGlide_doesNotThrow() { | ||
DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); | ||
cache.get(mock(Key.class)); | ||
deleteRecursively(cacheDir); | ||
cache.clear(); | ||
} | ||
|
||
@Test | ||
public void get_afterDeleteDirectoryOutsideGlideAndClose_doesNotThrow() { | ||
DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); | ||
cache.get(mock(Key.class)); | ||
deleteRecursively(cacheDir); | ||
cache.clear(); | ||
|
||
cache.get(mock(Key.class)); | ||
} | ||
|
||
@Test | ||
public void loadFromCache_afterDiskCacheDeletedAndCleared_doesNotFail() { | ||
final DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); | ||
cache.get(mock(Key.class)); | ||
deleteRecursively(cacheDir); | ||
cache.clear(); | ||
|
||
Glide.init( | ||
context, | ||
new GlideBuilder() | ||
.setDiskCache(new Factory() { | ||
@Override | ||
public DiskCache build() { | ||
return cache; | ||
} | ||
})); | ||
|
||
Drawable drawable = | ||
concurrency.get( | ||
Glide.with(context) | ||
.load(ResourceIds.raw.canonical) | ||
.submit()); | ||
assertThat(drawable).isNotNull(); | ||
} | ||
|
||
@Test | ||
public void loadFromCache_afterDiskCacheDeleted_doesNotFail() { | ||
final DiskCache cache = DiskLruCacheWrapper.create(cacheDir, 1024 * 1024); | ||
cache.get(mock(Key.class)); | ||
deleteRecursively(cacheDir); | ||
|
||
Glide.init( | ||
context, | ||
new GlideBuilder() | ||
.setDiskCache(new Factory() { | ||
@Override | ||
public DiskCache build() { | ||
return cache; | ||
} | ||
})); | ||
|
||
Drawable drawable = | ||
concurrency.get(Glide.with(context) | ||
.load(raw.canonical) | ||
.submit()); | ||
assertThat(drawable).isNotNull(); | ||
} | ||
|
||
private static void deleteRecursively(File file) { | ||
if (file.isDirectory()) { | ||
File[] files = file.listFiles(); | ||
if (files != null) { | ||
for (File f : files) { | ||
deleteRecursively(f); | ||
} | ||
} | ||
} | ||
if (!file.delete() && file.exists()) { | ||
throw new RuntimeException("Failed to delete: " + file); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters