test/jdk/java/net/DatagramSocket/InterruptibleDatagramSocket.java
author pconcannon
Wed, 27 Nov 2019 16:01:34 +0000
changeset 59299 17d242844fc9
permissions -rw-r--r--
8233018: Add a new test to verify that DatagramSocket is not interruptible Summary: Test added to check the interruptability of DatagramSocket, MulticastSocket and DatagramSocketAdaptor. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59299
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     1
/*
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     2
 * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     4
 *
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     7
 * published by the Free Software Foundation.
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     8
 *
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    13
 * accompanied this code).
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    14
 *
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    18
 *
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    21
 * questions.
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    22
 */
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    23
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    24
import java.net.DatagramPacket;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    25
import java.net.DatagramSocket;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    26
import java.net.MulticastSocket;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    27
import java.net.SocketException;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    28
import java.net.SocketTimeoutException;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    29
import java.nio.channels.ClosedByInterruptException;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    30
import java.nio.channels.DatagramChannel;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    31
import java.util.concurrent.CountDownLatch;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    32
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    33
import static java.lang.Thread.sleep;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    34
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    35
/*
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    36
 * @test
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    37
 * @summary Check interrupt mechanism for DatagramSocket,
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    38
 *      MulticastSocket, and DatagramSocketAdaptor
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    39
 */
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    40
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    41
public class InterruptibleDatagramSocket {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    42
    private static void test0(DatagramSocket s) throws Exception {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    43
        CountDownLatch latch = new CountDownLatch(1);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    44
        Thread testThread = Thread.currentThread();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    45
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    46
        Thread coordinator = new Thread(() -> {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    47
            try {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    48
                latch.await();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    49
                sleep(500);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    50
                testThread.interrupt();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    51
            } catch (InterruptedException e) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    52
            }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    53
        });
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    54
        byte[] data = {0, 1, 2};
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    55
        DatagramPacket p = new DatagramPacket(data, data.length,
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    56
                s.getLocalAddress(), s.getLocalPort());
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    57
        s.setSoTimeout(2000);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    58
        coordinator.start();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    59
        latch.countDown();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    60
        try {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    61
            s.receive(p);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    62
        } finally {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    63
            try {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    64
                coordinator.join();
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    65
            } catch (InterruptedException e) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    66
            }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    67
        }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    68
    }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    69
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    70
    static void test(DatagramSocket s, boolean interruptible) throws Exception {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    71
        try {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    72
            test0(s);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    73
            throw new RuntimeException("Receive shouldn't have succeeded");
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    74
        } catch (SocketTimeoutException e) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    75
            if (interruptible)
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    76
                throw e;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    77
            System.out.println("Got expected SocketTimeoutException: " + e);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    78
        } catch (SocketException e) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    79
            if ((e.getCause() instanceof ClosedByInterruptException) && interruptible) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    80
                System.out.println("Got expected ClosedByInterruptException: " + e);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    81
            } else {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    82
                throw e;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    83
            }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    84
        } catch (ClosedByInterruptException e) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    85
            if (!interruptible)
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    86
                throw e;
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    87
            System.out.println("Got expected ClosedByInterruptException: " + e);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    88
        }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    89
        if (s.isClosed() && !interruptible)
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    90
            throw new RuntimeException("DatagramSocket should not be closed");
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    91
        else if (!s.isClosed() && interruptible)
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    92
            throw new RuntimeException("DatagramSocket should be closed");
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    93
    }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    94
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    95
    public static void main(String[] args) throws Exception {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    96
        try (DatagramSocket s = new DatagramSocket()) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    97
            test(s, false);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    98
        }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
    99
        try (DatagramSocket s = new MulticastSocket()) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   100
            test(s, false);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   101
        }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   102
        try (DatagramSocket s = DatagramChannel.open().socket()) {
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   103
            test(s, true);
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   104
        }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   105
    }
17d242844fc9 8233018: Add a new test to verify that DatagramSocket is not interruptible
pconcannon
parents:
diff changeset
   106
}