Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation of cleanup logic when a Sandbox is evicted
Previously, when a Sandbox was evicted from SandboxManager, there was no logic to shut down any of the associated resources. There was an implicit assumption that the Sandbox would be garbage collected, and cleanup would magically occur, but currently there are a lot of things preventing Sandbox GC from occurring, and there are a lot of resources that need to be explicitly cleaned up. Add a 'shutdown' method to Sandbox that closes the classloader and shuts down the main executor thread. Although this is not currently sufficient to make the Sandbox eligible for GC, it does free some resources. Forthcoming CLs will add additional cleanup logic, with the eventual goal of making Sandboxes eligible for GC when they are evicted from SandboxManager. PiperOrigin-RevId: 564790888
- Loading branch information
Showing
6 changed files
with
113 additions
and
8 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
56 changes: 56 additions & 0 deletions
56
robolectric/src/test/java/org/robolectric/RobolectricTestRunnerCleanupTest.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,56 @@ | ||
package org.robolectric; | ||
|
||
import static org.junit.Assert.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.concurrent.RejectedExecutionException; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runner.notification.RunNotifier; | ||
import org.junit.runners.JUnit4; | ||
import org.robolectric.internal.AndroidSandbox; | ||
|
||
@RunWith(JUnit4.class) | ||
public class RobolectricTestRunnerCleanupTest { | ||
|
||
@Test | ||
public void sandboxShutdownShouldCloseClassloader() throws Exception { | ||
SingleSdkRobolectricTestRunner runner = | ||
new SingleSdkRobolectricTestRunner( | ||
TestWithEmptyTest.class, SingleSdkRobolectricTestRunner.defaultInjector().build()); | ||
|
||
runner.run(mock(RunNotifier.class)); | ||
|
||
AndroidSandbox latestSandbox = runner.getLatestSandbox(); | ||
latestSandbox.shutdown(); | ||
// Try to load a class that has not already been loaded. Closing a ClassLoader will only prevent | ||
// loading new classes. | ||
assertThrows( | ||
ClassNotFoundException.class, | ||
() -> | ||
latestSandbox | ||
.getRobolectricClassLoader() | ||
.loadClass("com.android.server.am.ActivityManagerService")); | ||
} | ||
|
||
@Test | ||
public void sandboxShutdownShouldShutDownMainThreadExecutor() throws Exception { | ||
SingleSdkRobolectricTestRunner runner = | ||
new SingleSdkRobolectricTestRunner( | ||
TestWithEmptyTest.class, SingleSdkRobolectricTestRunner.defaultInjector().build()); | ||
|
||
runner.run(mock(RunNotifier.class)); | ||
|
||
AndroidSandbox latestSandbox = runner.getLatestSandbox(); | ||
latestSandbox.shutdown(); | ||
// Try to run something on the main thread executor, which has been shut down. | ||
assertThrows(RejectedExecutionException.class, () -> latestSandbox.runOnMainThread(() -> {})); | ||
} | ||
|
||
@Ignore | ||
public static class TestWithEmptyTest { | ||
@Test | ||
public void emptyTest() throws Exception {} | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
sandbox/src/main/java/org/robolectric/internal/bytecode/ResourceProvider.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
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