/
ShouldNotStarveMemoryOnLargeStackTraceInvocationsTest.java
62 lines (48 loc) · 1.75 KB
/
ShouldNotStarveMemoryOnLargeStackTraceInvocationsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.memorytest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.Test;
@Ignore("https://github.com/mockito/mockito/issues/2478")
public class ShouldNotStarveMemoryOnLargeStackTraceInvocationsTest {
private static final int STACK_TRACE_DEPTH = 1000;
private static final int INVOCATIONS_ON_STACK_TRACE_LEVEL = 100;
private static boolean supported = false;
static {
try {
Class.forName("sun.misc.SharedSecrets")
.getMethod("getJavaLangAccess")
.invoke(null);
Class.forName("sun.misc.JavaLangAccess")
.getMethod("getStackTraceElement", Throwable.class, int.class);
supported = true;
} catch (Exception ignored) {
}
}
@Test
public void large_stack_trace_invocations_should_not_starve_memory() {
Assume.assumeTrue(supported);
Dummy mock = mock(Dummy.class);
when(mock.calculate(anyInt())).thenReturn(42);
assertThat(performComputationForDepth(mock, STACK_TRACE_DEPTH)).isEqualTo(42);
}
private int performComputationForDepth(Dummy mock, int i) {
if (i > 0) {
for (int j = 0; j < INVOCATIONS_ON_STACK_TRACE_LEVEL; j++) {
mock.calculate(j);
}
return mock.calculate(performComputationForDepth(mock, i - 1));
}
return 1;
}
interface Dummy {
int calculate(int fib);
}
}