1 /*
2  * Copyright (c) 2016, 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 8050499
26  * @summary Attempt to provoke error 316 on OS X in NativeSignal.signal()
27  */
28 
29 import java.io.*;
30 import java.net.*;
31 import java.nio.ByteBuffer;
32 import java.nio.channels.DatagramChannel;
33 
34 public class StressNativeSignal {
35     private UDPThread udpThread;
36     private ServerSocketThread serverSocketThread;
37 
StressNativeSignal()38     StressNativeSignal() {
39         try {
40             serverSocketThread = new ServerSocketThread();
41             serverSocketThread.start();
42 
43             udpThread = new UDPThread();
44             udpThread.start();
45         } catch (Exception z) {
46             z.printStackTrace();
47         }
48     }
49 
main(String[] args)50     public static void main(String[] args) throws Throwable {
51         StressNativeSignal test = new StressNativeSignal();
52         try {
53             Thread.sleep(3000);
54         } catch (Exception z) {
55             z.printStackTrace(System.err);
56         }
57 
58         test.shutdown();
59     }
60 
shutdown()61     public void shutdown() {
62         udpThread.terminate();
63         try {
64             udpThread.join();
65         } catch (Exception z) {
66             z.printStackTrace(System.err);
67         }
68 
69         serverSocketThread.terminate();
70         try {
71             serverSocketThread.join();
72         } catch (Exception z) {
73             z.printStackTrace(System.err);
74         }
75     }
76 
77     public class ServerSocketThread extends Thread {
78         private volatile boolean shouldTerminate;
79         private ServerSocket socket;
80 
run()81         public void run() {
82             try {
83                 socket = new ServerSocket(1122);
84                 Socket client = socket.accept();
85                 BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
86                 shouldTerminate = false;
87                 while (!shouldTerminate) {
88                     String msg = reader.readLine();
89                 }
90             } catch (Exception z) {
91                 if (!shouldTerminate) {
92                     z.printStackTrace(System.err);
93                 }
94             }
95         }
96 
terminate()97         public void terminate() {
98             shouldTerminate = true;
99             try {
100                 socket.close();
101             } catch (Exception z) {
102                 z.printStackTrace(System.err);
103                 // ignore
104             }
105         }
106     }
107 
108     public class UDPThread extends Thread {
109         private DatagramChannel channel;
110         private volatile boolean shouldTerminate;
111 
112         @Override
run()113         public void run() {
114             try {
115                 channel = DatagramChannel.open();
116                 channel.setOption(StandardSocketOptions.SO_RCVBUF, 6553600);
117                 channel.bind(new InetSocketAddress(19870));
118             } catch (IOException z) {
119                 z.printStackTrace(System.err);
120             }
121 
122             ByteBuffer buf = ByteBuffer.allocate(6553600);
123             shouldTerminate = false;
124             while (!shouldTerminate) {
125                 try {
126                     buf.rewind();
127                     channel.receive(buf);
128                 } catch (IOException z) {
129                     if (!shouldTerminate) {
130                         z.printStackTrace(System.err);
131                     }
132                 }
133             }
134         }
135 
terminate()136         public void terminate() {
137             shouldTerminate = true;
138             try {
139                 channel.close();
140             } catch (Exception z) {
141                 z.printStackTrace(System.err);
142                 // ignore
143             }
144         }
145     }
146 
147 }
148