提交 21414b54 编写于 作者: S sherman

6501089: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux

Summary: test/java/nio/channels/SocketChannel/AsyncCloseChannel.java failing (timeout) on Linux
Reviewed-by: alanb
上级 3a6673c6
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 6285901 * @bug 6285901 6501089
* @summary Check no data is written to wrong socket channel during async closing. * @summary Check no data is written to wrong socket channel during async closing.
* @author Xueming Shen * @author Xueming Shen
*/ */
...@@ -33,13 +33,13 @@ import java.nio.channels.*; ...@@ -33,13 +33,13 @@ import java.nio.channels.*;
import java.net.*; import java.net.*;
public class AsyncCloseChannel { public class AsyncCloseChannel {
static boolean failed = false; static volatile boolean failed = false;
static boolean keepGoing = true; static volatile boolean keepGoing = true;
static int maxAcceptCount = 100;
static volatile int acceptCount = 0;
static String host = "127.0.0.1"; static String host = "127.0.0.1";
static int sensorPort = 3010; static int sensorPort;
static int targetPort = 3020; static int targetPort;
static int maxAcceptCount = 1000;
static int acceptCount = 0;
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
if (System.getProperty("os.name").startsWith("Windows")) { if (System.getProperty("os.name").startsWith("Windows")) {
...@@ -48,11 +48,15 @@ public class AsyncCloseChannel { ...@@ -48,11 +48,15 @@ public class AsyncCloseChannel {
} }
Thread ss = new SensorServer(); ss.start(); Thread ss = new SensorServer(); ss.start();
Thread ts = new TargetServer(); ts.start(); Thread ts = new TargetServer(); ts.start();
sensorPort = ((ServerThread)ss).server.getLocalPort();
targetPort = ((ServerThread)ts).server.getLocalPort();
Thread sc = new SensorClient(); sc.start(); Thread sc = new SensorClient(); sc.start();
Thread tc = new TargetClient(); tc.start(); Thread tc = new TargetClient(); tc.start();
while(acceptCount < maxAcceptCount && !failed) { while(acceptCount < maxAcceptCount && !failed) {
Thread.yield(); Thread.sleep(10);
} }
keepGoing = false; keepGoing = false;
try { try {
...@@ -66,11 +70,8 @@ public class AsyncCloseChannel { ...@@ -66,11 +70,8 @@ public class AsyncCloseChannel {
+ acceptCount + "> times of accept!"); + acceptCount + "> times of accept!");
} }
static class SensorServer extends ServerThread {
static class SensorServer extends ThreadEx {
public void runEx() throws Exception { public void runEx() throws Exception {
ServerSocket server;
server = new ServerSocket(sensorPort);
while(keepGoing) { while(keepGoing) {
try { try {
final Socket s = server.accept(); final Socket s = server.accept();
...@@ -80,7 +81,7 @@ public class AsyncCloseChannel { ...@@ -80,7 +81,7 @@ public class AsyncCloseChannel {
int c = s.getInputStream().read(); int c = s.getInputStream().read();
if(c != -1) { if(c != -1) {
// No data is ever written to the peer's socket! // No data is ever written to the peer's socket!
System.out.println("Oops: read a character: " System.err.println("Oops: read a character: "
+ (char) c); + (char) c);
failed = true; failed = true;
} }
...@@ -92,17 +93,14 @@ public class AsyncCloseChannel { ...@@ -92,17 +93,14 @@ public class AsyncCloseChannel {
} }
}.start(); }.start();
} catch (IOException ex) { } catch (IOException ex) {
//ex.printStackTrace(); System.err.println("Exception on sensor server " + ex.getMessage());
} }
} }
} }
} }
static class TargetServer extends ThreadEx { static class TargetServer extends ServerThread {
public void runEx() throws Exception { public void runEx() throws Exception {
ServerSocket server;
server = new ServerSocket(targetPort);
while (keepGoing) { while (keepGoing) {
try { try {
final Socket s = server.accept(); final Socket s = server.accept();
...@@ -127,7 +125,7 @@ public class AsyncCloseChannel { ...@@ -127,7 +125,7 @@ public class AsyncCloseChannel {
} }
}.start(); }.start();
} catch (IOException ex) { } catch (IOException ex) {
//ex.printStackTrace(); System.err.println("Exception on target server " + ex.getMessage());
} }
} }
} }
...@@ -142,19 +140,19 @@ public class AsyncCloseChannel { ...@@ -142,19 +140,19 @@ public class AsyncCloseChannel {
try { try {
s = new Socket(); s = new Socket();
synchronized(this) { synchronized(this) {
while(!wake) { while(!wake && keepGoing) {
try { try {
wait(); wait();
} catch (InterruptedException ex) { } } catch (InterruptedException ex) { }
} }
}
wake = false; wake = false;
}
s.connect(new InetSocketAddress(host, sensorPort)); s.connect(new InetSocketAddress(host, sensorPort));
try { try {
Thread.sleep(10); Thread.sleep(10);
} catch (InterruptedException ex) { } } catch (InterruptedException ex) { }
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("Exception on sensor client " + ex.getMessage()); System.err.println("Exception on sensor client " + ex.getMessage());
} finally { } finally {
if(s != null) { if(s != null) {
try { try {
...@@ -200,26 +198,49 @@ public class AsyncCloseChannel { ...@@ -200,26 +198,49 @@ public class AsyncCloseChannel {
} }
} catch (IOException ex) { } catch (IOException ex) {
if(!(ex instanceof ClosedChannelException)) if(!(ex instanceof ClosedChannelException))
System.out.println("Exception in target client child " System.err.println("Exception in target client child "
+ ex.toString()); + ex.toString());
} }
} }
}; };
t.start(); t.start();
while(!ready) while(!ready && keepGoing) {
Thread.yield(); try {
Thread.sleep(10);
} catch (InterruptedException ex) {}
}
s.close(); s.close();
SensorClient.wakeMe(); SensorClient.wakeMe();
t.join(); t.join();
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("Exception in target client parent " System.err.println("Exception in target client parent "
+ ex.getMessage()); + ex.getMessage());
} catch (InterruptedException ex) {} } catch (InterruptedException ex) {}
} }
} }
} }
static abstract class ThreadEx extends Thread { static abstract class ServerThread extends Thread {
ServerSocket server;
public ServerThread() {
super();
try {
server = new ServerSocket(0);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void interrupt() {
super.interrupt();
if (server != null) {
try {
server.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void run() { public void run() {
try { try {
runEx(); runEx();
...@@ -231,7 +252,6 @@ public class AsyncCloseChannel { ...@@ -231,7 +252,6 @@ public class AsyncCloseChannel {
abstract void runEx() throws Exception; abstract void runEx() throws Exception;
} }
public static void closeIt(Socket s) { public static void closeIt(Socket s) {
try { try {
if(s != null) if(s != null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册