1
1
/*
2
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
* @summary Attempt to provoke error 316 on OS X in NativeSignal.signal()
27
27
*/
28
28
29
- import java .io .*;
30
- import java .net .*;
29
+ import java .io .IOException ;
30
+ import java .net .InetSocketAddress ;
31
+ import java .net .ServerSocket ;
32
+ import java .net .Socket ;
33
+ import java .net .StandardSocketOptions ;
31
34
import java .nio .ByteBuffer ;
32
35
import java .nio .channels .DatagramChannel ;
36
+ import java .util .concurrent .CountDownLatch ;
33
37
34
38
public class StressNativeSignal {
35
39
private UDPThread udpThread ;
36
40
private ServerSocketThread serverSocketThread ;
37
41
38
42
StressNativeSignal () {
39
- try {
40
- serverSocketThread = new ServerSocketThread ();
43
+ serverSocketThread = initServerSocketThread ();
44
+ if ( serverSocketThread != null ) {
41
45
serverSocketThread .start ();
46
+ }
42
47
43
- udpThread = new UDPThread ();
48
+ udpThread = initUDPThread ();
49
+ if (udpThread != null ) {
44
50
udpThread .start ();
51
+ }
52
+ }
53
+
54
+ private UDPThread initUDPThread () {
55
+ UDPThread aUDPThread = null ;
56
+ try {
57
+ aUDPThread = new UDPThread ();
45
58
} catch (Exception z ) {
59
+ System .err .println ("failed to create and start a UDPThread" );
46
60
z .printStackTrace ();
47
61
}
62
+ return aUDPThread ;
48
63
}
49
64
50
- public static void main ( String [] args ) throws Throwable {
51
- StressNativeSignal test = new StressNativeSignal () ;
65
+ private ServerSocketThread initServerSocketThread () {
66
+ ServerSocketThread aServerSocketThread = null ;
52
67
try {
53
- Thread .sleep (3000 );
68
+ aServerSocketThread = new ServerSocketThread ();
69
+
54
70
} catch (Exception z ) {
55
- z .printStackTrace (System .err );
71
+ System .err .println ("failed to create and start a ServerSocketThread" );
72
+ z .printStackTrace ();
56
73
}
74
+ return aServerSocketThread ;
75
+ }
57
76
77
+ public static void main (String [] args ) throws Throwable {
78
+ StressNativeSignal test = new StressNativeSignal ();
79
+ test .waitForTestThreadsToStart ();
58
80
test .shutdown ();
59
81
}
60
82
61
83
public void shutdown () {
62
- udpThread .terminate ();
63
- try {
64
- udpThread .join ();
65
- } catch (Exception z ) {
66
- z .printStackTrace (System .err );
84
+ if ((udpThread != null ) && udpThread .isAlive ()) {
85
+ udpThread .terminate ();
86
+ try {
87
+ udpThread .join ();
88
+ } catch (Exception z ) {
89
+ z .printStackTrace (System .err );
90
+ }
91
+ } else {
92
+ System .out .println ("UDPThread test scenario was not run" );
67
93
}
68
94
69
- serverSocketThread .terminate ();
70
- try {
71
- serverSocketThread .join ();
72
- } catch (Exception z ) {
73
- z .printStackTrace (System .err );
95
+ if ((serverSocketThread != null ) && (serverSocketThread .isAlive ())) {
96
+ serverSocketThread .terminate ();
97
+ try {
98
+ serverSocketThread .join ();
99
+ } catch (Exception z ) {
100
+ z .printStackTrace (System .err );
101
+ }
102
+ } else {
103
+ System .out .println ("ServerSocketThread test scenario was not run" );
104
+ }
105
+ }
106
+
107
+ public void waitForTestThreadsToStart () {
108
+ if ((udpThread != null ) && udpThread .isAlive ()) {
109
+ udpThread .waitTestThreadStart ();
110
+ }
111
+ if ((serverSocketThread != null ) && (serverSocketThread .isAlive ())) {
112
+ serverSocketThread .waitTestThreadStart ();
74
113
}
75
114
}
76
115
77
116
public class ServerSocketThread extends Thread {
78
117
private volatile boolean shouldTerminate ;
79
118
private ServerSocket socket ;
119
+ private final CountDownLatch threadStarted = new CountDownLatch (1 );
120
+
121
+ public ServerSocketThread () throws Exception {
122
+ socket = new ServerSocket (1122 );
123
+ }
80
124
81
125
public void run () {
126
+
82
127
try {
83
- socket = new ServerSocket ( 1122 );
128
+ threadStarted . countDown ( );
84
129
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
- }
130
+ client .close ();
131
+ throw new RuntimeException ("Unexpected return from accept call" );
90
132
} catch (Exception z ) {
133
+ System .err .println ("ServerSocketThread: caught exception " + z .getClass ().getName ());
91
134
if (!shouldTerminate ) {
92
135
z .printStackTrace (System .err );
93
136
}
@@ -103,40 +146,61 @@ public void terminate() {
103
146
// ignore
104
147
}
105
148
}
149
+
150
+ public void waitTestThreadStart () {
151
+ try {
152
+ threadStarted .await ();
153
+ } catch (Exception z ) {
154
+ z .printStackTrace (System .err );
155
+ // ignore
156
+ }
157
+ }
106
158
}
107
159
108
160
public class UDPThread extends Thread {
109
161
private DatagramChannel channel ;
110
162
private volatile boolean shouldTerminate ;
163
+ private final CountDownLatch threadStarted = new CountDownLatch (1 );
164
+
165
+ public UDPThread () throws Exception {
166
+
167
+ channel = DatagramChannel .open ();
168
+ channel .setOption (StandardSocketOptions .SO_RCVBUF , 6553600 );
169
+ channel .bind (new InetSocketAddress (19870 ));
170
+ }
111
171
112
172
@ Override
113
173
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
174
122
175
ByteBuffer buf = ByteBuffer .allocate (6553600 );
123
- shouldTerminate = false ;
124
- while (! shouldTerminate ) {
176
+ threadStarted . countDown () ;
177
+ do {
125
178
try {
126
179
buf .rewind ();
127
180
channel .receive (buf );
128
181
} catch (IOException z ) {
182
+ System .err .println ("UDPThread: caught exception " + z .getClass ().getName ());
129
183
if (!shouldTerminate ) {
130
184
z .printStackTrace (System .err );
131
185
}
132
186
}
133
- }
187
+ } while (! shouldTerminate );
134
188
}
135
189
136
190
public void terminate () {
137
191
shouldTerminate = true ;
138
192
try {
139
193
channel .close ();
194
+ } catch (Exception z ) {
195
+ System .err .println ("UDPThread: caught exception " + z .getClass ().getName ());
196
+ z .printStackTrace (System .err );
197
+ // ignore
198
+ }
199
+ }
200
+
201
+ public void waitTestThreadStart () {
202
+ try {
203
+ threadStarted .await ();
140
204
} catch (Exception z ) {
141
205
z .printStackTrace (System .err );
142
206
// ignore
0 commit comments