test/jdk/java/nio/channels/AsynchronousSocketChannel/CompletionHandlerRelease.java
author bpb
Tue, 03 Jul 2018 15:02:46 -0700
changeset 50933 76b5ee99ffc0
permissions -rw-r--r--
8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive Summary: Clear handler instance variable after use Reviewed-by: rriggs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50933
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     1
/*
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     4
 *
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     8
 *
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    13
 * accompanied this code).
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    14
 *
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    18
 *
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    21
 * questions.
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    22
 */
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    23
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    24
/* @test
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    25
 * @bug 8202252
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    26
 * @run testng CompletionHandlerRelease
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    27
 * @summary Verify that reference to CompletionHandler is cleared after use
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    28
 */
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    29
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    30
import java.io.Closeable;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    31
import java.io.IOException;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    32
import java.lang.ref.Reference;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    33
import java.lang.ref.ReferenceQueue;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    34
import java.lang.ref.WeakReference;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    35
import java.net.InetAddress;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    36
import java.net.InetSocketAddress;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    37
import static java.net.StandardSocketOptions.*;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    38
import java.nio.ByteBuffer;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    39
import java.nio.channels.AsynchronousChannelGroup;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    40
import java.nio.channels.AsynchronousServerSocketChannel;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    41
import java.nio.channels.AsynchronousSocketChannel;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    42
import java.nio.channels.CompletionHandler;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    43
import java.util.concurrent.CountDownLatch;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    44
import java.util.concurrent.Executors;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    45
import java.util.concurrent.Future;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    46
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    47
import org.testng.annotations.AfterTest;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    48
import org.testng.annotations.BeforeTest;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    49
import org.testng.annotations.Test;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    50
import static org.testng.Assert.*;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    51
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    52
public class CompletionHandlerRelease {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    53
    @Test
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    54
    public void testConnect() throws Exception {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    55
        try (Server server = new Server()) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    56
            try (AsynchronousSocketChannel ch =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    57
                 AsynchronousSocketChannel.open(GROUP)) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    58
                CountDownLatch latch = new CountDownLatch(1);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    59
                Handler<Void,Object> handler =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    60
                    new Handler<Void,Object>("connect", latch);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    61
                ReferenceQueue queue = new ReferenceQueue<WeakReference>();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    62
                WeakReference<Object> ref =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    63
                    new WeakReference<Object>(handler, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    64
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    65
                ch.connect(server.address(), null, handler);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    66
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    67
                try { latch.await(); } catch (InterruptedException ignore) { }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    68
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    69
                handler = null;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    70
                waitForRefToClear(ref, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    71
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    72
                server.accept().get().close();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    73
            }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    74
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    75
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    76
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    77
    @Test
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    78
    public void testWrite() throws Exception {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    79
        try (Server server = new Server();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    80
             AsynchronousSocketChannel ch =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    81
                 AsynchronousSocketChannel.open(GROUP)) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    82
            ch.connect(server.address()).get();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    83
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    84
            try (AsynchronousSocketChannel sc = server.accept().get()) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    85
                ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    86
                sc.setOption(SO_SNDBUF, src.remaining());
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    87
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    88
                CountDownLatch latch = new CountDownLatch(1);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    89
                Handler<Integer,Object> handler =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    90
                    new Handler<Integer,Object>("write", latch);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    91
                ReferenceQueue queue = new ReferenceQueue<WeakReference>();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    92
                WeakReference<Object> ref =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    93
                    new WeakReference<Object>(handler, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    94
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    95
                sc.write(src, null, handler);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    96
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    97
                try { latch.await(); } catch (InterruptedException ignore) { }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    98
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
    99
                handler = null;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   100
                waitForRefToClear(ref, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   101
            }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   102
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   103
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   104
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   105
    @Test
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   106
    public void testRead() throws Exception {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   107
        try (Server server = new Server();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   108
             AsynchronousSocketChannel ch =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   109
                 AsynchronousSocketChannel.open(GROUP)) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   110
            ch.connect(server.address()).get();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   111
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   112
            try (AsynchronousSocketChannel sc = server.accept().get()) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   113
                ByteBuffer src = ByteBuffer.wrap("hello".getBytes("UTF-8"));
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   114
                sc.setOption(SO_SNDBUF, src.remaining());
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   115
                sc.write(src).get();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   116
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   117
                CountDownLatch latch = new CountDownLatch(1);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   118
                Handler<Integer,Object> handler =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   119
                    new Handler<Integer,Object>("read", latch);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   120
                ReferenceQueue queue = new ReferenceQueue<WeakReference>();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   121
                WeakReference<Object> ref =
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   122
                    new WeakReference<Object>(handler, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   123
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   124
                ByteBuffer dst = ByteBuffer.allocate(64);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   125
                ch.read(dst, null, handler);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   126
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   127
                try { latch.await(); } catch (InterruptedException ignore) { }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   128
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   129
                handler = null;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   130
                waitForRefToClear(ref, queue);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   131
            }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   132
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   133
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   134
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   135
    private AsynchronousChannelGroup GROUP;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   136
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   137
    @BeforeTest
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   138
    void setup() throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   139
        GROUP = AsynchronousChannelGroup.withFixedThreadPool(2,
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   140
            Executors.defaultThreadFactory());
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   141
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   142
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   143
    @AfterTest
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   144
    void cleanup() throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   145
        GROUP.shutdownNow();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   146
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   147
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   148
    class Server implements Closeable {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   149
        private final AsynchronousServerSocketChannel ssc;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   150
        private final InetSocketAddress address;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   151
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   152
        Server() throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   153
            this(0);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   154
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   155
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   156
        Server(int recvBufSize) throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   157
            ssc = AsynchronousServerSocketChannel.open(GROUP);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   158
            if (recvBufSize > 0) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   159
                ssc.setOption(SO_RCVBUF, recvBufSize);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   160
            }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   161
            ssc.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(),
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   162
                0));
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   163
            address = (InetSocketAddress)ssc.getLocalAddress();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   164
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   165
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   166
        InetSocketAddress address() {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   167
            return address;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   168
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   169
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   170
        Future<AsynchronousSocketChannel> accept() throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   171
            return ssc.accept();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   172
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   173
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   174
        public void close() throws IOException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   175
            ssc.close();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   176
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   177
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   178
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   179
    static class Handler<V,A> implements CompletionHandler<V,A> {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   180
        private final String name;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   181
        private final CountDownLatch latch;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   182
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   183
        Handler(String name, CountDownLatch latch) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   184
            this.name = name;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   185
            this.latch = latch;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   186
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   187
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   188
        public void completed(V result, A attachment) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   189
            System.out.format("%s completed(%s, %s)%n",
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   190
                name, result, attachment);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   191
            latch.countDown();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   192
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   193
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   194
        public void failed(Throwable exc, A attachment) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   195
            System.out.format("%s failed(%s, %s)%n",
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   196
                name, exc, attachment);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   197
            exc.printStackTrace();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   198
            latch.countDown();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   199
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   200
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   201
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   202
    private void waitForRefToClear(Reference ref, ReferenceQueue queue)
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   203
        throws InterruptedException {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   204
        Reference r;
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   205
        while ((r = queue.remove(20)) == null) {
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   206
            System.gc();
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   207
        }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   208
        assertEquals(r, ref);
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   209
        assertNull(r.get());
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   210
    }
76b5ee99ffc0 8202252: (aio) Closed AsynchronousSocketChannel keeps completion handler alive
bpb
parents:
diff changeset
   211
}