Path: blob/master/test/jdk/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java
41153 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 820225225* @run testng CompletionHandlerRelease26* @summary Verify that reference to CompletionHandler is cleared after use27*/2829import java.io.Closeable;30import java.io.IOException;31import java.lang.ref.Reference;32import java.lang.ref.ReferenceQueue;33import java.lang.ref.WeakReference;34import java.net.InetAddress;35import java.net.InetSocketAddress;36import static java.net.StandardSocketOptions.*;37import java.nio.ByteBuffer;38import java.nio.channels.AsynchronousChannelGroup;39import java.nio.channels.AsynchronousServerSocketChannel;40import java.nio.channels.AsynchronousSocketChannel;41import java.nio.channels.CompletionHandler;42import java.util.concurrent.CountDownLatch;43import java.util.concurrent.Executors;44import java.util.concurrent.Future;4546import org.testng.annotations.AfterTest;47import org.testng.annotations.BeforeTest;48import org.testng.annotations.Test;49import static org.testng.Assert.*;5051public class CompletionHandlerRelease {52@Test53public void testConnect() throws Exception {54try (Server server = new Server()) {55try (AsynchronousSocketChannel ch =56AsynchronousSocketChannel.open(GROUP)) {57CountDownLatch latch = new CountDownLatch(1);58Handler<Void,Object> handler =59new Handler<Void,Object>("connect", latch);60ReferenceQueue queue = new ReferenceQueue<WeakReference>();61WeakReference<Object> ref =62new WeakReference<Object>(handler, queue);6364ch.connect(server.address(), null, handler);6566try { latch.await(); } catch (InterruptedException ignore) { }6768handler = null;69waitForRefToClear(ref, queue);7071server.accept().get().close();72}73}74}7576@Test77public void testWrite() throws Exception {78try (Server server = new Server();79AsynchronousSocketChannel ch =80AsynchronousSocketChannel.open(GROUP)) {81ch.connect(server.address()).get();8283try (AsynchronousSocketChannel sc = server.accept().get()) {84ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));85sc.setOption(SO_SNDBUF, src.remaining());8687CountDownLatch latch = new CountDownLatch(1);88Handler<Integer,Object> handler =89new Handler<Integer,Object>("write", latch);90ReferenceQueue queue = new ReferenceQueue<WeakReference>();91WeakReference<Object> ref =92new WeakReference<Object>(handler, queue);9394sc.write(src, null, handler);9596try { latch.await(); } catch (InterruptedException ignore) { }9798handler = null;99waitForRefToClear(ref, queue);100}101}102}103104@Test105public void testRead() throws Exception {106try (Server server = new Server();107AsynchronousSocketChannel ch =108AsynchronousSocketChannel.open(GROUP)) {109ch.connect(server.address()).get();110111try (AsynchronousSocketChannel sc = server.accept().get()) {112ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));113sc.setOption(SO_SNDBUF, src.remaining());114sc.write(src).get();115116CountDownLatch latch = new CountDownLatch(1);117Handler<Integer,Object> handler =118new Handler<Integer,Object>("read", latch);119ReferenceQueue queue = new ReferenceQueue<WeakReference>();120WeakReference<Object> ref =121new WeakReference<Object>(handler, queue);122123ByteBuffer dst = ByteBuffer.allocate(64);124ch.read(dst, null, handler);125126try { latch.await(); } catch (InterruptedException ignore) { }127128handler = null;129waitForRefToClear(ref, queue);130}131}132}133134private AsynchronousChannelGroup GROUP;135136@BeforeTest137void setup() throws IOException {138GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,139Executors.defaultThreadFactory());140}141142@AfterTest143void cleanup() throws IOException {144GROUP.shutdownNow();145}146147class Server implements Closeable {148private final AsynchronousServerSocketChannel ssc;149private final InetSocketAddress address;150151Server() throws IOException {152this(0);153}154155Server(int recvBufSize) throws IOException {156ssc = AsynchronousServerSocketChannel.open(GROUP);157if (recvBufSize > 0) {158ssc.setOption(SO_RCVBUF, recvBufSize);159}160ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(),1610));162address = (InetSocketAddress)ssc.getLocalAddress();163}164165InetSocketAddress address() {166return address;167}168169Future<AsynchronousSocketChannel> accept() throws IOException {170return ssc.accept();171}172173public void close() throws IOException {174ssc.close();175}176}177178static class Handler<V,A> implements CompletionHandler<V,A> {179private final String name;180private final CountDownLatch latch;181182Handler(String name, CountDownLatch latch) {183this.name = name;184this.latch = latch;185}186187public void completed(V result, A attachment) {188System.out.format("%s completed(%s, %s)%n",189name, result, attachment);190latch.countDown();191}192193public void failed(Throwable exc, A attachment) {194System.out.format("%s failed(%s, %s)%n",195name, exc, attachment);196exc.printStackTrace();197latch.countDown();198}199}200201private void waitForRefToClear(Reference ref, ReferenceQueue queue)202throws InterruptedException {203Reference r;204while ((r = queue.remove(20)) == null) {205System.gc();206}207assertEquals(r, ref);208assertNull(r.get());209}210}211212213