Affected test:
api/java_nio/channels/spi/AbstractInterruptibleChannel/index.html#Methods[AbstractInterruptibleChannel0007]
this will be new test in jck8, and it is brought-over from cdc-tck during the test sync
The test passed on jdk6, but failed on jdk7.
Following code also can be used to reproduce the failure:
==============================================================
import java.nio.channels.*;
import java.io.*;
import java.nio.channels.spi.*;
public class Bug {
public static final long WAIT_TIME = 10000L; // 10 sec
public static final long DELAY_TIME = 100L; // 100 ms
public void test() {
TestIChannel channel = new TestIChannel(true);
System.out.println("Start the IO thread");
IOThread t = new IOThread(channel);
t.start();
System.out.println("Waiting for the read start in IO thread");
while (!channel.isReadingStarted()) {
try {
Thread.sleep(DELAY_TIME);
} catch (InterruptedException ie) {
// ignore
System.out.println("Got InterruptedException 1");
}
}
System.out.println("Interrupting the IO thread");
try {
t.interrupt();
System.out.println("after t.interrupted");
t.join();
System.out.println("after join.");
} catch (InterruptedException e) {
// ignore
System.out.println("Got InterruptedException 1");
}
Exception e = t.getException();
if (e != null) {
System.out.println("Caught exception " + e.getClass().getName());
System.out.println("Failed");
} else {
System.out.println("No exception caught");
System.out.println("OK");
}
}
public static void main(String[] args) {
Bug a = new Bug();
a.test();
}
class TestIChannel extends AbstractInterruptibleChannel {
boolean readSuccess = false;
Object lock = new Object();
volatile boolean implCloseChannel = false;
volatile boolean readingStarted = false;
public boolean isReadingStarted() {
return readingStarted;
}
public void resetFlag() {
implCloseChannel = false;
}
public boolean isImplCloseChannel() {
return implCloseChannel;
}
protected void implCloseChannel() throws IOException {
synchronized(lock) {
implCloseChannel = true;
lock.notify();
}
}
public void read() throws IOException {
try {
begin();
readingStarted = true;
while(!implCloseChannel) {
try {
synchronized(lock) {
lock.wait(WAIT_TIME);
}
} catch(InterruptedException e) {
// ignore
}
}
} finally {
end(readSuccess);
}
}
public TestIChannel() {
super();
}
public TestIChannel(boolean complete) {
super();
readSuccess = complete;
}
}
class IOThread extends Thread {
volatile Exception exc = null;
TestIChannel channel = null;
public IOThread(TestIChannel c) {
super();
channel = c;
}
public Exception getException() {
return exc;
}
public void run() {
try {
channel.read();
} catch (Exception e) {
exc = e;
System.out.println("Got exception when call channel.read.");
}
}
}
}
===========================================================
api/java_nio/channels/spi/AbstractInterruptibleChannel/index.html#Methods[AbstractInterruptibleChannel0007]
this will be new test in jck8, and it is brought-over from cdc-tck during the test sync
The test passed on jdk6, but failed on jdk7.
Following code also can be used to reproduce the failure:
==============================================================
import java.nio.channels.*;
import java.io.*;
import java.nio.channels.spi.*;
public class Bug {
public static final long WAIT_TIME = 10000L; // 10 sec
public static final long DELAY_TIME = 100L; // 100 ms
public void test() {
TestIChannel channel = new TestIChannel(true);
System.out.println("Start the IO thread");
IOThread t = new IOThread(channel);
t.start();
System.out.println("Waiting for the read start in IO thread");
while (!channel.isReadingStarted()) {
try {
Thread.sleep(DELAY_TIME);
} catch (InterruptedException ie) {
// ignore
System.out.println("Got InterruptedException 1");
}
}
System.out.println("Interrupting the IO thread");
try {
t.interrupt();
System.out.println("after t.interrupted");
t.join();
System.out.println("after join.");
} catch (InterruptedException e) {
// ignore
System.out.println("Got InterruptedException 1");
}
Exception e = t.getException();
if (e != null) {
System.out.println("Caught exception " + e.getClass().getName());
System.out.println("Failed");
} else {
System.out.println("No exception caught");
System.out.println("OK");
}
}
public static void main(String[] args) {
Bug a = new Bug();
a.test();
}
class TestIChannel extends AbstractInterruptibleChannel {
boolean readSuccess = false;
Object lock = new Object();
volatile boolean implCloseChannel = false;
volatile boolean readingStarted = false;
public boolean isReadingStarted() {
return readingStarted;
}
public void resetFlag() {
implCloseChannel = false;
}
public boolean isImplCloseChannel() {
return implCloseChannel;
}
protected void implCloseChannel() throws IOException {
synchronized(lock) {
implCloseChannel = true;
lock.notify();
}
}
public void read() throws IOException {
try {
begin();
readingStarted = true;
while(!implCloseChannel) {
try {
synchronized(lock) {
lock.wait(WAIT_TIME);
}
} catch(InterruptedException e) {
// ignore
}
}
} finally {
end(readSuccess);
}
}
public TestIChannel() {
super();
}
public TestIChannel(boolean complete) {
super();
readSuccess = complete;
}
}
class IOThread extends Thread {
volatile Exception exc = null;
TestIChannel channel = null;
public IOThread(TestIChannel c) {
super();
channel = c;
}
public Exception getException() {
return exc;
}
public void run() {
try {
channel.read();
} catch (Exception e) {
exc = e;
System.out.println("Got exception when call channel.read.");
}
}
}
}
===========================================================
- relates to
-
JDK-6979009 (fc) FileChannel.read() fails to throw ClosedByInterruptException
- Closed