|
| 1 | +package test; |
| 2 | + |
| 3 | +import org.junit.runner.Description; |
| 4 | +import org.junit.runner.Result; |
| 5 | +import org.junit.runner.notification.RunListener; |
| 6 | +import org.junit.Assert; |
| 7 | + |
| 8 | +import java.lang.ref.WeakReference; |
| 9 | + |
| 10 | +public class ContextEscapeDetector extends RunListener { |
| 11 | + |
| 12 | + //context can be captured by objects, eg NoDenotation |
| 13 | + public static final int CONTEXTS_ALLOWED = 1; |
| 14 | + |
| 15 | + @Override |
| 16 | + public void testRunFinished(Result result) throws Exception { |
| 17 | + if (contextsAlive() > CONTEXTS_ALLOWED) { |
| 18 | + forceGCHeuristic0(); |
| 19 | + if (contextsAlive() > CONTEXTS_ALLOWED) { |
| 20 | + forceGCHeuristic1(); |
| 21 | + if (contextsAlive() > CONTEXTS_ALLOWED) { |
| 22 | + forceGCHeuristic2(); |
| 23 | + forceGCHeuristic1(); |
| 24 | + int contextAlive = contextsAlive(); |
| 25 | + if (contextAlive > CONTEXTS_ALLOWED) { |
| 26 | + StringBuilder names = new StringBuilder(); |
| 27 | + for (ContextEscapeDetection.TestContext ref : ContextEscapeDetection.contexts) { |
| 28 | + if (ref.context.get() != null) names.append(ref.testName).append(' '); |
| 29 | + } |
| 30 | + Assert.fail("Multiple contexts survived test suite: " + names.toString()); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + super.testRunFinished(result); |
| 36 | + } |
| 37 | + |
| 38 | + private static synchronized int contextsAlive() { |
| 39 | + int count = 0; |
| 40 | + for (ContextEscapeDetection.TestContext ref : ContextEscapeDetection.contexts) { |
| 41 | + if (ref.context.get() != null) count++; |
| 42 | + } |
| 43 | + return count; |
| 44 | + } |
| 45 | + |
| 46 | + private static volatile Object o = null; |
| 47 | + |
| 48 | + private static synchronized void forceGCHeuristic0() { |
| 49 | + System.gc(); |
| 50 | + Runtime.getRuntime().gc(); |
| 51 | + System.gc(); |
| 52 | + Runtime.getRuntime().gc(); |
| 53 | + System.gc(); |
| 54 | + Runtime.getRuntime().gc(); |
| 55 | + System.gc(); |
| 56 | + Runtime.getRuntime().gc(); |
| 57 | + System.gc(); |
| 58 | + } |
| 59 | + |
| 60 | + private static synchronized void forceGCHeuristic1() { |
| 61 | + Object obj = new Object(); |
| 62 | + WeakReference ref = new WeakReference<Object>(obj); |
| 63 | + obj = null; |
| 64 | + while (ref.get() != null) { |
| 65 | + System.gc(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static synchronized void forceGCHeuristic2() { |
| 70 | + try { |
| 71 | + Object[] arr = new Object[1024]; // upto 8 GB |
| 72 | + WeakReference ref = new WeakReference<Object>(arr); |
| 73 | + o = arr; // make sure array isn't optimized away |
| 74 | + |
| 75 | + Runtime runtime = Runtime.getRuntime(); |
| 76 | + // allocate memory until no more that 64MB is left |
| 77 | + for (int i = 0; i < 1024 && |
| 78 | + runtime.totalMemory() != runtime.maxMemory() || |
| 79 | + runtime.freeMemory() < 1024 * 1024 * 64; i++) { |
| 80 | + int[] data = new int[1024 * 1024]; // 8MB |
| 81 | + for (int j = 0; j < 1024 * 1024; j++) { |
| 82 | + data[j] = j; // force actual pages allocation |
| 83 | + } |
| 84 | + arr[i] = data; |
| 85 | + } |
| 86 | + o = null; |
| 87 | + arr = new Object[128]; |
| 88 | + o = arr; |
| 89 | + // allocate 1 more GB |
| 90 | + for (int i = 0; i < 128; i++) { |
| 91 | + int[] data = new int[1024 * 1024]; // 8MB |
| 92 | + for (int j = 0; j < 1024 * 1024; j++) { |
| 93 | + data[j] = j; // force actual pages allocation |
| 94 | + } |
| 95 | + arr[i] = data; |
| 96 | + } |
| 97 | + o = null; |
| 98 | + arr = null; |
| 99 | + |
| 100 | + forceGCHeuristic0(); |
| 101 | + while (ref.get() != null) { |
| 102 | + System.gc(); |
| 103 | + } |
| 104 | + } catch (OutOfMemoryError e) { |
| 105 | + o = null; |
| 106 | + // just swallow |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments