Skip to content

Commit 9ee1571

Browse files
author
SendaoYan
committed
8327650: Test java/nio/channels/DatagramChannel/StressNativeSignal.java timed out
Backport-of: 7acfba288ff4d1f43cc36506b2bd2d32107b00c2
1 parent d158b8b commit 9ee1571

File tree

1 file changed

+100
-36
lines changed

1 file changed

+100
-36
lines changed

test/jdk/java/nio/channels/DatagramChannel/StressNativeSignal.java

Lines changed: 100 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,68 +26,111 @@
2626
* @summary Attempt to provoke error 316 on OS X in NativeSignal.signal()
2727
*/
2828

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;
3134
import java.nio.ByteBuffer;
3235
import java.nio.channels.DatagramChannel;
36+
import java.util.concurrent.CountDownLatch;
3337

3438
public class StressNativeSignal {
3539
private UDPThread udpThread;
3640
private ServerSocketThread serverSocketThread;
3741

3842
StressNativeSignal() {
39-
try {
40-
serverSocketThread = new ServerSocketThread();
43+
serverSocketThread = initServerSocketThread();
44+
if (serverSocketThread != null) {
4145
serverSocketThread.start();
46+
}
4247

43-
udpThread = new UDPThread();
48+
udpThread = initUDPThread();
49+
if (udpThread != null) {
4450
udpThread.start();
51+
}
52+
}
53+
54+
private UDPThread initUDPThread() {
55+
UDPThread aUDPThread = null;
56+
try {
57+
aUDPThread = new UDPThread();
4558
} catch (Exception z) {
59+
System.err.println("failed to create and start a UDPThread");
4660
z.printStackTrace();
4761
}
62+
return aUDPThread;
4863
}
4964

50-
public static void main(String[] args) throws Throwable {
51-
StressNativeSignal test = new StressNativeSignal();
65+
private ServerSocketThread initServerSocketThread() {
66+
ServerSocketThread aServerSocketThread = null;
5267
try {
53-
Thread.sleep(3000);
68+
aServerSocketThread = new ServerSocketThread();
69+
5470
} catch (Exception z) {
55-
z.printStackTrace(System.err);
71+
System.err.println("failed to create and start a ServerSocketThread");
72+
z.printStackTrace();
5673
}
74+
return aServerSocketThread;
75+
}
5776

77+
public static void main(String[] args) throws Throwable {
78+
StressNativeSignal test = new StressNativeSignal();
79+
test.waitForTestThreadsToStart();
5880
test.shutdown();
5981
}
6082

6183
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");
6793
}
6894

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();
74113
}
75114
}
76115

77116
public class ServerSocketThread extends Thread {
78117
private volatile boolean shouldTerminate;
79118
private ServerSocket socket;
119+
private final CountDownLatch threadStarted = new CountDownLatch(1);
120+
121+
public ServerSocketThread () throws Exception {
122+
socket = new ServerSocket(1122);
123+
}
80124

81125
public void run() {
126+
82127
try {
83-
socket = new ServerSocket(1122);
128+
threadStarted.countDown();
84129
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");
90132
} catch (Exception z) {
133+
System.err.println("ServerSocketThread: caught exception " + z.getClass().getName());
91134
if (!shouldTerminate) {
92135
z.printStackTrace(System.err);
93136
}
@@ -103,40 +146,61 @@ public void terminate() {
103146
// ignore
104147
}
105148
}
149+
150+
public void waitTestThreadStart() {
151+
try {
152+
threadStarted.await();
153+
} catch (Exception z) {
154+
z.printStackTrace(System.err);
155+
// ignore
156+
}
157+
}
106158
}
107159

108160
public class UDPThread extends Thread {
109161
private DatagramChannel channel;
110162
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+
}
111171

112172
@Override
113173
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-
}
121174

122175
ByteBuffer buf = ByteBuffer.allocate(6553600);
123-
shouldTerminate = false;
124-
while (!shouldTerminate) {
176+
threadStarted.countDown();
177+
do {
125178
try {
126179
buf.rewind();
127180
channel.receive(buf);
128181
} catch (IOException z) {
182+
System.err.println("UDPThread: caught exception " + z.getClass().getName());
129183
if (!shouldTerminate) {
130184
z.printStackTrace(System.err);
131185
}
132186
}
133-
}
187+
} while (!shouldTerminate);
134188
}
135189

136190
public void terminate() {
137191
shouldTerminate = true;
138192
try {
139193
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();
140204
} catch (Exception z) {
141205
z.printStackTrace(System.err);
142206
// ignore

0 commit comments

Comments
 (0)