Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape mock during method dispatch on mock to avoid premature garbage…
… collection. Fixes #1802.
- Loading branch information
Showing
2 changed files
with
64 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
39 changes: 39 additions & 0 deletions
39
src/test/java/org/mockito/PrematureGarbageCollectionTest.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 @@ | ||
package org.mockito; | ||
|
||
import org.junit.Test; | ||
|
||
public class PrematureGarbageCollectionTest { | ||
|
||
@Test | ||
public void provoke_premature_garbage_collection() { | ||
for (int i = 0; i < 500; i++) { | ||
populateNodeList(); | ||
} | ||
} | ||
|
||
private static void populateNodeList() { | ||
Node node = nodes(); | ||
while (node != null) { | ||
Node next = node.next; | ||
node.object.run(); | ||
node = next; | ||
} | ||
} | ||
|
||
private static Node nodes() { | ||
Node node = null; | ||
for (int i = 0; i < 1_000; ++i) { | ||
Node next = new Node(); | ||
next.next = node; | ||
node = next; | ||
} | ||
return node; | ||
} | ||
|
||
private static class Node { | ||
|
||
private Node next; | ||
|
||
private final Runnable object = Mockito.mock(Runnable.class); | ||
} | ||
} |