-
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.
Wrap recursive initialization booleans in try/finally
Previously any exception in the initialization process could be hidden by one of these recursive checks. The first time the exception occured, it would be thrown normally, but subsequent times the recursive check would be thrown instead. This complicates debugging. Now we always unset the isInitializing boolean using try/finally. While this doesn't fix anything directly, it does make debugging easier when some kind of initialization error occurs. Helps with #4977
- Loading branch information
Showing
4 changed files
with
147 additions
and
9 deletions.
There are no files selected for viewing
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
70 changes: 70 additions & 0 deletions
70
library/test/src/test/java/com/bumptech/glide/InitializeGlideTest.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,70 @@ | ||
package com.bumptech.glide; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
|
||
import android.content.Context; | ||
import androidx.annotation.NonNull; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.bumptech.glide.tests.TearDownGlide; | ||
import java.util.Set; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.function.ThrowingRunnable; | ||
import org.junit.runner.RunWith; | ||
|
||
// This test is about edge cases that might otherwise make debugging more challenging. | ||
@RunWith(AndroidJUnit4.class) | ||
public class InitializeGlideTest { | ||
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private final Context context = ApplicationProvider.getApplicationContext(); | ||
private static final class TestException extends RuntimeException { | ||
private static final long serialVersionUID = 2515021766931124927L; | ||
} | ||
|
||
@Test | ||
public void initialize_whenInternalMethodThrows_throwsException() { | ||
assertThrows( | ||
TestException.class, | ||
new ThrowingRunnable() { | ||
@Override | ||
public void run() { | ||
synchronized (Glide.class) { | ||
Glide.checkAndInitializeGlide( | ||
context, | ||
new GeneratedAppGlideModule() { | ||
@NonNull | ||
@Override | ||
Set<Class<?>> getExcludedModuleClasses() { | ||
throw new TestException(); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void initialize_whenInternalMethodThrows_andCalledTwice_throwsException() { | ||
GeneratedAppGlideModule throwingGeneratedAppGlideModule = | ||
new GeneratedAppGlideModule() { | ||
@NonNull | ||
@Override | ||
Set<Class<?>> getExcludedModuleClasses() { | ||
throw new TestException(); | ||
} | ||
}; | ||
ThrowingRunnable initializeGlide = new ThrowingRunnable() { | ||
@Override | ||
public void run() throws Throwable { | ||
synchronized (Glide.class) { | ||
Glide.checkAndInitializeGlide(context, throwingGeneratedAppGlideModule); | ||
} | ||
} | ||
}; | ||
|
||
assertThrows(TestException.class, initializeGlide); | ||
// Make sure the second exception isn't hidden by some Glide initialization related exception. | ||
assertThrows(TestException.class, initializeGlide); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
library/test/src/test/java/com/bumptech/glide/RegistryFactoryTest.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,63 @@ | ||
package com.bumptech.glide; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
|
||
import android.content.Context; | ||
import androidx.annotation.NonNull; | ||
import androidx.test.core.app.ApplicationProvider; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import com.bumptech.glide.module.AppGlideModule; | ||
import com.bumptech.glide.tests.TearDownGlide; | ||
import com.bumptech.glide.util.GlideSuppliers.GlideSupplier; | ||
import com.google.common.collect.ImmutableList; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.function.ThrowingRunnable; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class RegistryFactoryTest { | ||
@Rule public final TearDownGlide tearDownGlide = new TearDownGlide(); | ||
private final Context context = ApplicationProvider.getApplicationContext(); | ||
|
||
private static final class TestException extends RuntimeException { | ||
private static final long serialVersionUID = 2334956185897161236L; | ||
} | ||
|
||
@Test | ||
public void create_whenCalledTwiceWithThrowingModule_throwsOriginalException() { | ||
AppGlideModule throwingAppGlideModule = | ||
new AppGlideModule() { | ||
@Override | ||
public void registerComponents(@NonNull Context context, @NonNull Glide glide, | ||
@NonNull Registry registry) { | ||
throw new TestException(); | ||
} | ||
}; | ||
|
||
Glide glide = Glide.get(context); | ||
GlideSupplier<Registry> registrySupplier = | ||
RegistryFactory.lazilyCreateAndInitializeRegistry( | ||
glide, | ||
/* manifestModules= */ ImmutableList.of(), | ||
throwingAppGlideModule); | ||
|
||
assertThrows( | ||
TestException.class, | ||
new ThrowingRunnable() { | ||
@Override | ||
public void run() { | ||
registrySupplier.get(); | ||
} | ||
}); | ||
|
||
assertThrows( | ||
TestException.class, | ||
new ThrowingRunnable() { | ||
@Override | ||
public void run() { | ||
registrySupplier.get(); | ||
} | ||
}); | ||
} | ||
} |