test/jdk/java/net/MulticastSocket/UnreferencedMulticastSockets.java
author dfuchs
Wed, 10 Jul 2019 22:33:23 +0200
changeset 55649 ad8e3b295615
parent 55047 3131927311ee
child 58423 54de0c861d32
permissions -rw-r--r--
8227539: Replace wildcard address with loopback or local host in tests - part 20 Summary: Update some tests to stop using the wildcard address. Reviewed-by: michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     1
/*
55047
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     4
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     8
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    13
 * accompanied this code).
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    14
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    18
 *
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    21
 * questions.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    22
 */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    23
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    24
/**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    25
 * @test
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 51830
diff changeset
    26
 * @library /test/lib
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    27
 * @modules java.management java.base/java.io:+open java.base/java.net:+open
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    28
 * @run main/othervm -Djava.net.preferIPv4Stack=true UnreferencedMulticastSockets
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    29
 * @run main/othervm UnreferencedMulticastSockets
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    30
 * @summary Check that unreferenced multicast sockets are closed
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    31
 */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    32
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    33
import java.io.FileDescriptor;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    34
import java.lang.management.ManagementFactory;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    35
import java.lang.management.OperatingSystemMXBean;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    36
import java.lang.ref.ReferenceQueue;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    37
import java.lang.ref.WeakReference;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    38
import java.lang.reflect.Field;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    39
import java.io.IOException;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    40
import java.net.DatagramPacket;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    41
import java.net.DatagramSocket;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    42
import java.net.DatagramSocketImpl;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    43
import java.net.InetAddress;
55047
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
    44
import java.net.InetSocketAddress;
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    45
import java.net.MulticastSocket;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    46
import java.net.UnknownHostException;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    47
import java.nio.file.Files;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    48
import java.nio.file.Path;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    49
import java.nio.file.Paths;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    50
import java.util.ArrayDeque;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    51
import java.util.List;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    52
import java.util.Optional;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    53
import java.util.concurrent.TimeUnit;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    54
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 51830
diff changeset
    55
import jdk.test.lib.net.IPSupport;
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 51830
diff changeset
    56
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    57
import com.sun.management.UnixOperatingSystemMXBean;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    58
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    59
public class UnreferencedMulticastSockets {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    60
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    61
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    62
     * The set of sockets we have to check up on.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    63
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    64
    final static ArrayDeque<NamedWeak> pendingSockets = new ArrayDeque<>(5);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    65
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    66
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    67
     * Queued objects when they are unreferenced.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    68
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    69
    final static ReferenceQueue<Object> pendingQueue = new ReferenceQueue<>();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    70
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    71
    // Server to echo a datagram packet
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    72
    static class Server implements Runnable {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    73
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    74
        MulticastSocket ss;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    75
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    76
        Server() throws IOException {
55047
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
    77
            InetSocketAddress serverAddress =
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
    78
                new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
    79
            ss = new MulticastSocket(serverAddress);
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    80
            System.out.printf("  DatagramServer addr: %s: %d%n",
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    81
                    this.getHost(), this.getPort());
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    82
            pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverMulticastSocket"));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    83
            extractRefs(ss, "serverMulticastSocket");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    84
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    85
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    86
        InetAddress getHost() throws UnknownHostException {
55047
3131927311ee 8224761: Replace wildcard address with loopback or local host in tests - part 12
dfuchs
parents: 54811
diff changeset
    87
            InetAddress localhost = InetAddress.getLoopbackAddress();
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    88
            return localhost;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    89
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    90
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    91
        int getPort() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    92
            return ss.getLocalPort();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    93
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    94
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    95
        // Receive a byte and send back a byte
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    96
        public void run() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    97
            try {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    98
                byte[] buffer = new byte[50];
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
    99
                DatagramPacket p = new DatagramPacket(buffer, buffer.length);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   100
                ss.receive(p);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   101
                buffer[0] += 1;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   102
                ss.send(p);         // send back +1
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   103
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   104
                // do NOT close but 'forget' the socket reference
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   105
                ss = null;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   106
            } catch (Exception ioe) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   107
                ioe.printStackTrace();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   108
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   109
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   110
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   111
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   112
    public static void main(String args[]) throws Exception {
54811
9db7c0f561a6 8223652: Rename IPSupport.skipIfCurrentConfigurationIsInvalid() to IPSupport.throwSkippedExceptionIfNonOperational()
aeubanks
parents: 54770
diff changeset
   113
        IPSupport.throwSkippedExceptionIfNonOperational();
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   114
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   115
        // Create and close a MulticastSocket to warm up the FD count for side effects.
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   116
        try (MulticastSocket s = new MulticastSocket(0)) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   117
            // no-op; close immediately
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   118
            s.getLocalPort();   // no-op
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   119
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   120
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   121
        long fdCount0 = getFdCount();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   122
        listProcFD();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   123
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   124
        // start a server
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   125
        Server svr = new Server();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   126
        Thread thr = new Thread(svr);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   127
        thr.start();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   128
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   129
        MulticastSocket client = new MulticastSocket(0);
51830
d2c72de3cf83 8199931: java/net/MulticastSocket/UnreferencedMulticastSockets.java fails with "incorrect data received"
xyin
parents: 48737
diff changeset
   130
        System.out.printf("  client bound port: %d%n", client.getLocalPort());
48737
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   131
        client.connect(svr.getHost(), svr.getPort());
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   132
        pendingSockets.add(new NamedWeak(client, pendingQueue, "clientMulticastSocket"));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   133
        extractRefs(client, "clientMulticastSocket");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   134
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   135
        byte[] msg = new byte[1];
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   136
        msg[0] = 1;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   137
        DatagramPacket p = new DatagramPacket(msg, msg.length, svr.getHost(), svr.getPort());
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   138
        client.send(p);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   139
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   140
        p = new DatagramPacket(msg, msg.length);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   141
        client.receive(p);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   142
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   143
        System.out.printf("echo received from: %s%n", p.getSocketAddress());
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   144
        if (msg[0] != 2) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   145
            throw new AssertionError("incorrect data received: expected: 2, actual: " + msg[0]);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   146
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   147
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   148
        // Do NOT close the MulticastSocket; forget it
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   149
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   150
        Object ref;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   151
        int loops = 20;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   152
        while (!pendingSockets.isEmpty() && loops-- > 0) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   153
            ref = pendingQueue.remove(1000L);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   154
            if (ref != null) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   155
                pendingSockets.remove(ref);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   156
                System.out.printf("  ref freed: %s, remaining: %d%n", ref, pendingSockets.size());
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   157
            } else {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   158
                client = null;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   159
                p = null;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   160
                msg = null;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   161
                System.gc();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   162
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   163
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   164
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   165
        thr.join();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   166
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   167
        // List the open file descriptors
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   168
        long fdCount = getFdCount();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   169
        System.out.printf("Initial fdCount: %d, final fdCount: %d%n", fdCount0, fdCount);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   170
        listProcFD();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   171
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   172
        if (loops == 0) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   173
            throw new AssertionError("Not all references reclaimed");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   174
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   175
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   176
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   177
    // Get the count of open file descriptors, or -1 if not available
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   178
    private static long getFdCount() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   179
        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   180
        return (mxBean instanceof UnixOperatingSystemMXBean)
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   181
                ? ((UnixOperatingSystemMXBean) mxBean).getOpenFileDescriptorCount()
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   182
                : -1L;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   183
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   184
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   185
    // Reflect to find references in the socket implementation that will be gc'd
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   186
    private static void extractRefs(MulticastSocket s, String name) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   187
        try {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   188
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   189
            Field socketImplField = DatagramSocket.class.getDeclaredField("impl");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   190
            socketImplField.setAccessible(true);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   191
            Object socketImpl = socketImplField.get(s);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   192
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   193
            Field fileDescriptorField = DatagramSocketImpl.class.getDeclaredField("fd");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   194
            fileDescriptorField.setAccessible(true);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   195
            FileDescriptor fileDescriptor = (FileDescriptor) fileDescriptorField.get(socketImpl);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   196
            extractRefs(fileDescriptor, name);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   197
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   198
            Class<?> socketImplClass = socketImpl.getClass();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   199
            System.out.printf("socketImplClass: %s%n", socketImplClass);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   200
            if (socketImplClass.getName().equals("java.net.TwoStacksPlainDatagramSocketImpl")) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   201
                Field fileDescriptor1Field = socketImplClass.getDeclaredField("fd1");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   202
                fileDescriptor1Field.setAccessible(true);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   203
                FileDescriptor fileDescriptor1 = (FileDescriptor) fileDescriptor1Field.get(socketImpl);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   204
                extractRefs(fileDescriptor1, name + "::twoStacksFd1");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   205
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   206
            } else {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   207
                System.out.printf("socketImpl class name not matched: %s != %s%n",
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   208
                        socketImplClass.getName(), "java.net.TwoStacksPlainDatagramSocketImpl");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   209
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   210
        } catch (NoSuchFieldException | IllegalAccessException ex) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   211
            ex.printStackTrace();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   212
            throw new AssertionError("missing field", ex);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   213
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   214
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   215
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   216
    private static void extractRefs(FileDescriptor fileDescriptor, String name) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   217
        Object cleanup = null;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   218
        int rawfd = -1;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   219
        try {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   220
            if (fileDescriptor != null) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   221
                Field fd1Field = FileDescriptor.class.getDeclaredField("fd");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   222
                fd1Field.setAccessible(true);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   223
                rawfd = fd1Field.getInt(fileDescriptor);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   224
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   225
                Field cleanupfdField = FileDescriptor.class.getDeclaredField("cleanup");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   226
                cleanupfdField.setAccessible(true);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   227
                cleanup = cleanupfdField.get(fileDescriptor);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   228
                pendingSockets.add(new NamedWeak(fileDescriptor, pendingQueue,
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   229
                        name + "::fileDescriptor: " + rawfd));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   230
                pendingSockets.add(new NamedWeak(cleanup, pendingQueue, name + "::fdCleanup: " + rawfd));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   231
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   232
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   233
        } catch (NoSuchFieldException | IllegalAccessException ex) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   234
            ex.printStackTrace();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   235
            throw new AssertionError("missing field", ex);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   236
        } finally {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   237
            System.out.print(String.format("  %s:: fd: %s, fd: %d, cleanup: %s%n",
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   238
                    name, fileDescriptor, rawfd, cleanup));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   239
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   240
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   241
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   242
    /**
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   243
     * Method to list the open file descriptors (if supported by the 'lsof' command).
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   244
     */
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   245
    static void listProcFD() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   246
        List<String> lsofDirs = List.of("/usr/bin", "/usr/sbin");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   247
        Optional<Path> lsof = lsofDirs.stream()
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   248
                .map(s -> Paths.get(s, "lsof"))
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   249
                .filter(f -> Files.isExecutable(f))
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   250
                .findFirst();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   251
        lsof.ifPresent(exe -> {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   252
            try {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   253
                System.out.printf("Open File Descriptors:%n");
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   254
                long pid = ProcessHandle.current().pid();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   255
                ProcessBuilder pb = new ProcessBuilder(exe.toString(), "-p", Integer.toString((int) pid));
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   256
                pb.inheritIO();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   257
                Process p = pb.start();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   258
                p.waitFor(10, TimeUnit.SECONDS);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   259
            } catch (IOException | InterruptedException ie) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   260
                ie.printStackTrace();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   261
            }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   262
        });
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   263
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   264
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   265
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   266
    // Simple class to identify which refs have been queued
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   267
    static class NamedWeak extends WeakReference<Object> {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   268
        private final String name;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   269
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   270
        NamedWeak(Object o, ReferenceQueue<Object> queue, String name) {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   271
            super(o, queue);
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   272
            this.name = name;
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   273
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   274
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   275
        public String toString() {
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   276
            return name + "; " + super.toString();
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   277
        }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   278
    }
7c12219870fd 8195059: Update java.net Socket and DatagramSocket implementations to use Cleaner
rriggs
parents:
diff changeset
   279
}