--- /dev/null 2016-01-24 08:40:46.523299426 +0100 +++ new/test/java/lang/ref/EphemeronTests.java 2016-01-24 11:21:30.578167102 +0100 @@ -0,0 +1,215 @@ +/* + * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @run main/othervm -Xmx16m EphemeronTests + * @summary Basic functional tests for Ephemerons + * @author Peter Levart + */ + +import java.lang.ref.Ephemeron; +import java.lang.ref.Reference; +import java.lang.ref.SoftReference; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; + +public class EphemeronTests { + + public static void main(String[] args) throws Exception { + + testValueToKeyChain(100, true); + testValueToKeyChain(100, false); + + testReachability(SoftReference::new); + testReachability(WeakReference::new); + } + + static class Key { + final int i; + + Key(int i) { + this.i = i; + } + + @Override + public String toString() { + return "k" + i; + } + } + + static class Value { + final Key key; + + Value(Key key) { + this.key = key; + } + + @Override + public String toString() { + return "v(" + key + ")"; + } + } + + static class Eph extends Ephemeron { + public Eph(Key key, Value value) { + super(key, value); + } + + @Override + public String toString() { + return getKey() + "=" + getValue(); + } + } + + static void testValueToKeyChain(int size, boolean forwardChaining) throws Exception { + System.out.println(); + System.out.println((forwardChaining ? "Forward" : "Backward") + + " chaining of value->key ..."); + System.out.println(); + List ephs = new ArrayList<>(size); + + Key k1 = new Key(1); + { + Key kp = k1; + for (int i = 2; i <= size; i++) { + Key ki = new Key(i); + ephs.add( + forwardChaining + ? new Eph(kp, new Value(ki)) + : new Eph(ki, new Value(kp)) + ); + kp = ki; + } + ephs.add( + forwardChaining + ? new Eph(kp, new Value(k1)) + : new Eph(k1, new Value(kp)) + ); + kp = null; + } + + fillHeap(); + System.out.println("1: " + toString(ephs)); + for (Eph eph : ephs) assertAlive(eph); + k1.getClass(); // reachabilityFence + + k1 = null; + fillHeap(); + System.out.println("2: " + toString(ephs)); + for (Eph eph : ephs) assertCleared(eph); + } + + static void testReachability(Function> referenceConstructor) { + Reference kRef, vRef; + Ephemeron eph; + Object k; + { + k = new Object(); + Object v = new Object(); + kRef = referenceConstructor.apply(k); + vRef = referenceConstructor.apply(v); + eph = new Ephemeron<>(k, v); + v = null; + } + + System.out.println(); + System.out.println(kRef.getClass().getSimpleName() + " reachability test"); + System.out.println(); + + while (true) { + fillHeap(); + if ((k = kRef.get()) == null) { + System.out.println("key cleared"); + assertCleared(kRef); + assertCleared(eph); + assertCleared(vRef); + break; + } else { + System.out.println("key alive"); + assertAlive(kRef); + assertAlive(eph); + assertAlive(vRef); + k.getClass(); // reachabilityFence + } + k = null; + } + } + + // utils + + static void fillHeap() { + long t0 = System.nanoTime(); + Object junk = null; + try { + while (true) { + Object[] nj = new Object[16384]; + nj[0] = junk; + junk = nj; + nj = null; + } + } catch (OutOfMemoryError e) { + junk = null; + System.gc(); + } + long t = System.nanoTime() - t0; + System.out.println("Fill Heap (" + (t / 1000000L) + " ms)"); + try { + Thread.sleep(100L); + } catch (InterruptedException e) {} + } + + static void assertAlive(Reference reference) { + if (reference.get() == null) { + throw new AssertionError(reference + " is cleared"); + } + } + + static void assertAlive(Ephemeron ephemeron) { + if (ephemeron.getKey() == null || ephemeron.getValue() == null) { + throw new AssertionError(ephemeron + " is cleared"); + } + } + + static void assertCleared(Reference reference) { + if (reference.get() != null) { + throw new AssertionError(reference + " is not cleared"); + } + } + + static void assertCleared(Ephemeron ephemeron) { + if (ephemeron.getKey() != null || ephemeron.getValue() != null) { + throw new AssertionError(ephemeron + " is not cleared"); + } + } + + static String toString(List list) { + if (list == null) return "null"; + if (list.size() <= 10) return list.toString(); + String prefix = list.subList(0, 5).toString(); + String suffix = list.subList(list.size() - 5, list.size()).toString(); + + return prefix.substring(0, prefix.length() - 1) + ", ..., " + suffix.substring(1); + } +}