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 /* @test
25  * @bug 8232673
26  * @summary Test DatagramChannel socket adaptor with concurrent send/receive
27  */
28 
29 import java.net.DatagramPacket;
30 import java.net.DatagramSocket;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.nio.channels.DatagramChannel;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.Future;
37 
38 public class AdaptorConcurrentIO {
39 
main(String[] args)40     public static void main(String[] args) throws Exception {
41         testConcurrentSendReceive(0);
42         testConcurrentSendReceive(60_000);
43     }
44 
45     /**
46      * Starts a task that blocks in the adaptor's receive method, then invokes
47      * the adaptor's send method to send a datagram. If the adaptor were using
48      * the channel's blockingLock then send without be blocked waiting for
49      * the receive to complete.
50      */
testConcurrentSendReceive(int timeout)51     static void testConcurrentSendReceive(int timeout) throws Exception {
52         try (DatagramChannel dc = DatagramChannel.open()) {
53             InetAddress lb = InetAddress.getLoopbackAddress();
54             dc.bind(new InetSocketAddress(lb, 0));
55             DatagramSocket s = dc.socket();
56             s.setSoTimeout(timeout);
57 
58             ExecutorService pool = Executors.newSingleThreadExecutor();
59             try {
60                 Future<String> result = pool.submit(() -> {
61                     byte[] data = new byte[100];
62                     DatagramPacket p = new DatagramPacket(data, 0, data.length);
63                     s.receive(p);
64                     return new String(p.getData(), p.getOffset(), p.getLength(), "UTF-8");
65                 });
66 
67                 Thread.sleep(200); // give chance for thread to block
68 
69                 byte[] data = "hello".getBytes("UTF-8");
70                 DatagramPacket p = new DatagramPacket(data, 0, data.length);
71                 p.setSocketAddress(s.getLocalSocketAddress());
72                 s.send(p);
73 
74                 String msg = result.get();
75                 if (!msg.equals("hello"))
76                     throw new RuntimeException("Unexpected message: " + msg);
77             } finally {
78                 pool.shutdown();
79             }
80         }
81     }
82 }
83 
84