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
79 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
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
/* | ||
* Copyright (c) 2020 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
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); | ||
} | ||
} |