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