Path: blob/master/test/jdk/java/nio/Buffer/ReachabilityTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 820836225* @summary Tests reachability from source to dependent direct byte buffers26* @run testng ReachabilityTest27*/2829import org.testng.Assert;30import org.testng.annotations.Test;3132import java.lang.ref.Reference;33import java.lang.ref.ReferenceQueue;34import java.lang.ref.WeakReference;35import java.nio.Buffer;36import java.nio.ByteBuffer;37import java.util.ArrayList;38import java.util.List;39import java.util.Objects;40import java.util.concurrent.CountDownLatch;41import java.util.function.UnaryOperator;4243import static java.util.concurrent.TimeUnit.MILLISECONDS;4445public class ReachabilityTest {4647@Test48public void testDuplicate() {49testReachability(ByteBuffer.allocateDirect(16384),50ByteBuffer::duplicate);51}5253@Test54public void testSlice() {55testReachability(ByteBuffer.allocateDirect(16384),56ByteBuffer::slice);57}5859@Test60public void testViewDuplicate() {61testReachability(ByteBuffer.allocateDirect(16384),62(Buffer b) -> b instanceof ByteBuffer63? ((ByteBuffer) b).asIntBuffer()64: b.duplicate()65);66}6768@Test69public void testViewSlice() {70testReachability(ByteBuffer.allocateDirect(16384),71(Buffer b) -> b instanceof ByteBuffer72? ((ByteBuffer) b).asIntBuffer()73: b.slice()74);75}7677<T> void testReachability(T t, UnaryOperator<T> b) {78WeakReference<T> root = new WeakReference<>(t);7980ReferenceQueue<Object> queue = new ReferenceQueue<>();81List<WeakReference<T>> refs = new ArrayList<>();82for (int i = 0; i < 1000; i++) {83t = b.apply(t);84refs.add(new WeakReference<>(t, queue));85}86t = b.apply(t);8788boolean collected = false;89long timeoutMillis = 100L;90try {91for (int tries = 0; tries < 3 && !collected; tries++) {92System.gc();93collected = refs.stream().map(Reference::get).anyMatch(Objects::isNull);94if (!collected) {95collected = queue.remove(timeoutMillis) != null;96timeoutMillis *= 4;97}98}99} catch (InterruptedException unexpected) {100throw new AssertionError("unexpected InterruptedException");101}102103// Some or all of the intermediate values must be GC'ed104Assert.assertTrue(collected);105// The root should never be GC'ed106Assert.assertNotNull(root.get());107108Reference.reachabilityFence(t);109}110}111112113