Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1619 from ttanxu/release/2.x
New API to clean up all inline mocks after test [ci maven-central-release]
- Loading branch information
Showing
10 changed files
with
319 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package org.mockito.plugins; | ||
|
||
import org.mockito.Incubating; | ||
import org.mockito.MockitoFramework; | ||
|
||
/** | ||
* Extension to {@link MockMaker} for mock makers that changes inline method implementations | ||
* and need keep track of created mock objects. | ||
* <p> | ||
* Mockito's default inline mock maker keeps track of created mock objects via weak reference map. | ||
* This poses a risk of memory leaks in certain scenarios | ||
* (issue <a href="https://github.com/mockito/mockito/pull/1619">#1619</a>). | ||
* There is no clean way to tackle those problems at the moment. | ||
* Hence, {@code InlineMockMaker} interface exposes methods to explicitly clear mock references. | ||
* Those methods are called by {@link MockitoFramework#clearInlineMocks()}. | ||
* When the user encounters a leak, he can mitigate the problem with {@link MockitoFramework#clearInlineMocks()}. | ||
* <p> | ||
* {@code InlineMockMaker} is for expert users and framework integrators, when custom inline mock maker is in use. | ||
* If you have a custom {@link MockMaker} that keeps track of mock objects, | ||
* please have your mock maker implement {@code InlineMockMaker} interface. | ||
* This way, it can participate in {@link MockitoFramework#clearInlineMocks()} API. | ||
* | ||
* @since 2.25.0 | ||
*/ | ||
@Incubating | ||
public interface InlineMockMaker extends MockMaker { | ||
|
||
/** | ||
* Clean up internal state for specified {@code mock}. You may assume there won't be any interaction to the specific | ||
* mock after this is called. | ||
* | ||
* @param mock the mock instance whose internal state is to be cleaned. | ||
* @since 2.25.0 | ||
*/ | ||
@Incubating | ||
void clearMock(Object mock); | ||
|
||
/** | ||
* Cleans up internal state for all existing mocks. You may assume there won't be any interaction to mocks created | ||
* previously after this is called. | ||
* | ||
* @since 2.25.0 | ||
*/ | ||
@Incubating | ||
void clearAllMocks(); | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
...s/inline/src/test/java/org/mockitoinline/bugs/CyclicMockMethodArgumentMemoryLeakTest.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,39 @@ | ||
/* | ||
* Copyright (c) 2019 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package org.mockitoinline.bugs; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.mockito.Mockito.framework; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class CyclicMockMethodArgumentMemoryLeakTest { | ||
private static final int ARRAY_LENGTH = 1 << 20; // 4 MB | ||
|
||
@Test | ||
public void no_memory_leak_when_cyclically_calling_method_with_mocks() { | ||
for (int i = 0; i < 100; ++i) { | ||
final A a = mock(A.class); | ||
a.largeArray = new int[ARRAY_LENGTH]; | ||
final B b = mock(B.class); | ||
|
||
a.accept(b); | ||
b.accept(a); | ||
|
||
framework().clearInlineMocks(); | ||
} | ||
} | ||
|
||
private static class A { | ||
private int[] largeArray; | ||
|
||
void accept(B b) {} | ||
} | ||
|
||
private static class B { | ||
void accept(A a) {} | ||
} | ||
} |
Oops, something went wrong.