mirror of
https://github.com/openjdk/jdk.git
synced 2026-05-11 05:59:52 +00:00
8278166: java/nio/channels/Channels/TransferTo.java timed out
Reviewed-by: lancea, alanb
This commit is contained in:
parent
543d1a8cf4
commit
7ea4b19f54
@ -23,9 +23,10 @@
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.IllegalBlockingModeException;
|
||||
@ -51,6 +52,7 @@ import org.testng.annotations.Test;
|
||||
import jdk.test.lib.RandomFactory;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static java.nio.file.StandardOpenOption.*;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertThrows;
|
||||
@ -62,8 +64,8 @@ import static org.testng.Assert.assertTrue;
|
||||
* @build jdk.test.lib.RandomFactory
|
||||
* @run testng/othervm/timeout=180 TransferTo
|
||||
* @bug 8265891
|
||||
* @summary tests whether sun.nio.ChannelInputStream.transferTo conforms to the
|
||||
* InputStream.transferTo contract defined in the javadoc
|
||||
* @summary Tests whether sun.nio.ChannelInputStream.transferTo conforms to the
|
||||
* InputStream.transferTo specification
|
||||
* @key randomness
|
||||
*/
|
||||
public class TransferTo {
|
||||
@ -72,16 +74,17 @@ public class TransferTo {
|
||||
|
||||
private static final int ITERATIONS = 10;
|
||||
|
||||
private static final int NUM_WRITES = 3 * 1024;
|
||||
private static final int BYTES_PER_WRITE = 1024 * 1024;
|
||||
private static final long BYTES_WRITTEN = (long) NUM_WRITES * BYTES_PER_WRITE;
|
||||
private static final int NUM_WRITES = 3*1024;
|
||||
private static final int BYTES_PER_WRITE = 1024*1024;
|
||||
private static final long BYTES_WRITTEN = (long) NUM_WRITES*BYTES_PER_WRITE;
|
||||
|
||||
private static final Random RND = RandomFactory.getRandom();
|
||||
|
||||
private static final Path CWD = Path.of(".");
|
||||
|
||||
/*
|
||||
* Provides test scenarios, i. e. combinations of input and output streams to be tested.
|
||||
* Provides test scenarios, i.e., combinations of input and output streams
|
||||
* to be tested.
|
||||
*/
|
||||
@DataProvider
|
||||
public static Object[][] streamCombinations() throws Exception {
|
||||
@ -89,10 +92,12 @@ public class TransferTo {
|
||||
// tests FileChannel.transferTo(FileChannel) optimized case
|
||||
{ fileChannelInput(), fileChannelOutput() },
|
||||
|
||||
// tests FileChannel.transferTo(SelectableChannelOutput) optimized case
|
||||
// tests FileChannel.transferTo(SelectableChannelOutput)
|
||||
// optimized case
|
||||
{ fileChannelInput(), selectableChannelOutput() },
|
||||
|
||||
// tests FileChannel.transferTo(WritableChannelOutput) optimized case
|
||||
// tests FileChannel.transferTo(WritableChannelOutput)
|
||||
// optimized case
|
||||
{ fileChannelInput(), writableByteChannelOutput() },
|
||||
|
||||
// tests InputStream.transferTo(OutputStream) default case
|
||||
@ -101,7 +106,8 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Testing API compliance: Input stream must throw NullPointerException when parameter "out" is null.
|
||||
* Testing API compliance: input stream must throw NullPointerException
|
||||
* when parameter "out" is null.
|
||||
*/
|
||||
@Test(dataProvider = "streamCombinations")
|
||||
public void testNullPointerException(InputStreamProvider inputStreamProvider,
|
||||
@ -117,7 +123,8 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Testing API compliance: Complete content of input stream must be transferred to output stream.
|
||||
* Testing API compliance: complete content of input stream must be
|
||||
* transferred to output stream.
|
||||
*/
|
||||
@Test(dataProvider = "streamCombinations")
|
||||
public void testStreamContents(InputStreamProvider inputStreamProvider,
|
||||
@ -128,10 +135,12 @@ public class TransferTo {
|
||||
// tests input stream with a length between 1k and 4k
|
||||
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(1024, 4096));
|
||||
|
||||
// tests input stream with several data chunks, as 16k is more than a single chunk can hold
|
||||
// tests input stream with several data chunks, as 16k is more than a
|
||||
// single chunk can hold
|
||||
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(16384, 16384));
|
||||
|
||||
// tests randomly chosen starting positions within source and target stream
|
||||
// tests randomly chosen starting positions within source and
|
||||
// target stream
|
||||
for (int i = 0; i < ITERATIONS; i++) {
|
||||
byte[] inBytes = createRandomBytes(MIN_SIZE, MAX_SIZE_INCR);
|
||||
int posIn = RND.nextInt(inBytes.length);
|
||||
@ -147,34 +156,60 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Special test for file-to-file transfer of more than two GB.
|
||||
* This test covers multiple iterations of FileChannel.transerTo(FileChannel),
|
||||
* which ChannelInputStream.transferTo() only applies in this particular case,
|
||||
* and cannot get tested using a single byte[] due to size limitation of arrays.
|
||||
* Special test for file-to-file transfer of more than 2 GB. This test
|
||||
* covers multiple iterations of FileChannel.transerTo(FileChannel),
|
||||
* which ChannelInputStream.transferTo() only applies in this particular
|
||||
* case, and cannot get tested using a single byte[] due to size limitation
|
||||
* of arrays.
|
||||
*/
|
||||
@Test
|
||||
public void testMoreThanTwoGB() throws IOException {
|
||||
Path sourceFile = Files.createTempFile(CWD, "test2GBSource", null);
|
||||
try {
|
||||
// preparing two temporary files which will be compared at the end of the test
|
||||
Path targetFile = Files.createTempFile(CWD, "test2GBtarget", null);
|
||||
try {
|
||||
// writing 3 GB of random bytes into source file
|
||||
for (int i = 0; i < NUM_WRITES; i++)
|
||||
Files.write(sourceFile, createRandomBytes(BYTES_PER_WRITE, 0), StandardOpenOption.APPEND);
|
||||
// prepare two temporary files to be compared at the end of the test
|
||||
// set the source file name
|
||||
String sourceName = String.format("test3GBSource%s.tmp",
|
||||
String.valueOf(RND.nextInt(Integer.MAX_VALUE)));
|
||||
Path sourceFile = CWD.resolve(sourceName);
|
||||
|
||||
// performing actual transfer, effectively by multiple invocations of Filechannel.transferTo(FileChannel)
|
||||
long count;
|
||||
try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
|
||||
OutputStream outputStream = Channels
|
||||
.newOutputStream(FileChannel.open(targetFile, StandardOpenOption.WRITE))) {
|
||||
count = inputStream.transferTo(outputStream);
|
||||
try {
|
||||
// set the target file name
|
||||
String targetName = String.format("test3GBTarget%s.tmp",
|
||||
String.valueOf(RND.nextInt(Integer.MAX_VALUE)));
|
||||
Path targetFile = CWD.resolve(targetName);
|
||||
|
||||
try {
|
||||
// calculate initial position to be just short of 2GB
|
||||
final long initPos = 2047*BYTES_PER_WRITE;
|
||||
|
||||
// create the source file with a hint to be sparse
|
||||
try (FileChannel fc = FileChannel.open(sourceFile, CREATE_NEW, SPARSE, WRITE, APPEND);) {
|
||||
// set initial position to avoid writing nearly 2GB
|
||||
fc.position(initPos);
|
||||
|
||||
// fill the remainder of the file with random bytes
|
||||
int nw = (int)(NUM_WRITES - initPos/BYTES_PER_WRITE);
|
||||
for (int i = 0; i < nw; i++) {
|
||||
byte[] rndBytes = createRandomBytes(BYTES_PER_WRITE, 0);
|
||||
ByteBuffer src = ByteBuffer.wrap(rndBytes);
|
||||
fc.write(src);
|
||||
}
|
||||
}
|
||||
|
||||
// comparing reported transferred bytes, must be 3 GB
|
||||
assertEquals(count, BYTES_WRITTEN);
|
||||
// create the target file with a hint to be sparse
|
||||
try (FileChannel fc = FileChannel.open(targetFile, CREATE_NEW, WRITE, SPARSE);) {
|
||||
}
|
||||
|
||||
// comparing content of both files, failing in case of any difference
|
||||
// perform actual transfer, effectively by multiple invocations
|
||||
// of Filechannel.transferTo(FileChannel)
|
||||
try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
|
||||
OutputStream outputStream = Channels.newOutputStream(FileChannel.open(targetFile, WRITE))) {
|
||||
long count = inputStream.transferTo(outputStream);
|
||||
|
||||
// compare reported transferred bytes, must be 3 GB
|
||||
// less the value of the initial position
|
||||
assertEquals(count, BYTES_WRITTEN - initPos);
|
||||
}
|
||||
|
||||
// compare content of both files, failing if different
|
||||
assertEquals(Files.mismatch(sourceFile, targetFile), -1);
|
||||
|
||||
} finally {
|
||||
@ -186,28 +221,33 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Special test whether selectable channel based transfer throws blocking mode exception.
|
||||
* Special test of whether selectable channel based transfer throws blocking
|
||||
* mode exception.
|
||||
*/
|
||||
@Test
|
||||
public void testIllegalBlockingMode() throws IOException {
|
||||
Pipe pipe = Pipe.open();
|
||||
try {
|
||||
// testing arbitrary input (here: empty file) to non-blocking selectable output
|
||||
// testing arbitrary input (here: empty file) to non-blocking
|
||||
// selectable output
|
||||
try (FileChannel fc = FileChannel.open(Files.createTempFile(CWD, "testIllegalBlockingMode", null));
|
||||
InputStream is = Channels.newInputStream(fc);
|
||||
SelectableChannel sc = pipe.sink().configureBlocking(false);
|
||||
OutputStream os = Channels.newOutputStream((WritableByteChannel) sc)) {
|
||||
InputStream is = Channels.newInputStream(fc);
|
||||
SelectableChannel sc = pipe.sink().configureBlocking(false);
|
||||
OutputStream os = Channels.newOutputStream((WritableByteChannel) sc)) {
|
||||
|
||||
// IllegalBlockingMode must be thrown when trying to perform a transfer
|
||||
// IllegalBlockingMode must be thrown when trying to perform
|
||||
// a transfer
|
||||
assertThrows(IllegalBlockingModeException.class, () -> is.transferTo(os));
|
||||
}
|
||||
|
||||
// testing non-blocking selectable input to arbitrary output (here: byte array)
|
||||
// testing non-blocking selectable input to arbitrary output
|
||||
// (here: byte array)
|
||||
try (SelectableChannel sc = pipe.source().configureBlocking(false);
|
||||
InputStream is = Channels.newInputStream((ReadableByteChannel) sc);
|
||||
OutputStream os = new ByteArrayOutputStream()) {
|
||||
InputStream is = Channels.newInputStream((ReadableByteChannel) sc);
|
||||
OutputStream os = new ByteArrayOutputStream()) {
|
||||
|
||||
// IllegalBlockingMode must be thrown when trying to perform a transfer
|
||||
// IllegalBlockingMode must be thrown when trying to perform
|
||||
// a transfer
|
||||
assertThrows(IllegalBlockingModeException.class, () -> is.transferTo(os));
|
||||
}
|
||||
} finally {
|
||||
@ -217,9 +257,9 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Asserts that the transferred content is correct, i. e. compares the actually transferred bytes
|
||||
* to the expected assumption. The position of the input and output stream before the transfer is
|
||||
* the start of stream (BOF).
|
||||
* Asserts that the transferred content is correct, i.e., compares the bytes
|
||||
* actually transferred to those expected. The position of the input and
|
||||
* output streams before the transfer are zero (BOF).
|
||||
*/
|
||||
private static void checkTransferredContents(InputStreamProvider inputStreamProvider,
|
||||
OutputStreamProvider outputStreamProvider, byte[] inBytes) throws Exception {
|
||||
@ -227,16 +267,16 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Asserts that the transferred content is correct, i. e. compares the actually transferred bytes
|
||||
* to the expected assumption. The position of the input and output stream before the transfer is
|
||||
* provided by the caller.
|
||||
* Asserts that the transferred content is correct, i. e. compares the bytes
|
||||
* actually transferred to those expected. The positions of the input and
|
||||
* output streams before the transfer are provided by the caller.
|
||||
*/
|
||||
private static void checkTransferredContents(InputStreamProvider inputStreamProvider,
|
||||
OutputStreamProvider outputStreamProvider, byte[] inBytes, int posIn, int posOut) throws Exception {
|
||||
AtomicReference<Supplier<byte[]>> recorder = new AtomicReference<>();
|
||||
try (InputStream in = inputStreamProvider.input(inBytes);
|
||||
OutputStream out = outputStreamProvider.output(recorder::set)) {
|
||||
// skip bytes till starting position
|
||||
OutputStream out = outputStreamProvider.output(recorder::set)) {
|
||||
// skip bytes until starting position
|
||||
in.skipNBytes(posIn);
|
||||
out.write(new byte[posOut]);
|
||||
|
||||
@ -252,7 +292,8 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates an array of random size (between min and min + maxRandomAdditive) filled with random bytes
|
||||
* Creates an array of random size (between min and min + maxRandomAdditive)
|
||||
* filled with random bytes
|
||||
*/
|
||||
private static byte[] createRandomBytes(int min, int maxRandomAdditive) {
|
||||
byte[] bytes = new byte[min + (maxRandomAdditive == 0 ? 0 : RND.nextInt(maxRandomAdditive))];
|
||||
@ -298,7 +339,8 @@ public class TransferTo {
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates a provider for an input stream which wraps a readable byte channel but is not a file channel
|
||||
* Creates a provider for an input stream which wraps a readable byte
|
||||
* channel but is not a file channel
|
||||
*/
|
||||
private static InputStreamProvider readableByteChannelInput() {
|
||||
return new InputStreamProvider() {
|
||||
@ -316,7 +358,7 @@ public class TransferTo {
|
||||
return new OutputStreamProvider() {
|
||||
public OutputStream output(Consumer<Supplier<byte[]>> spy) throws Exception {
|
||||
Path path = Files.createTempFile(CWD, "fileChannelOutput", null);
|
||||
FileChannel fileChannel = FileChannel.open(path, StandardOpenOption.WRITE);
|
||||
FileChannel fileChannel = FileChannel.open(path, WRITE);
|
||||
spy.accept(() -> {
|
||||
try {
|
||||
return Files.readAllBytes(path);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user