Basic.java revision 3261
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4607272 6822643 6830721 6842687
* @summary Unit test for AsynchronousFileChannel
*/
public class Basic {
// create temporary file
blah.deleteOnExit();
try {
// run tests
} finally {
}
// run test that expects channel to be closed
// these tests open the file themselves
// eagerly clean-up
}
/*
* Generate buffer with random contents
* Writes buffer to file using a CompletionHandler to consume the result
* of each write operation
* Reads file to EOF to a new buffer using a CompletionHandler to consume
* the result of each read operation
* Compares buffer contents
*/
throws IOException
{
// generate buffer with random elements and write it to file
// read to EOF or buffer is full
// check buffers are the same
throw new RuntimeException("Contents differ");
}
}
/*
* Generate buffer with random contents
* Writes buffer to file, invoking the Future's get method to wait for
* each write operation to complete
* Reads file to EOF to a new buffer, invoking the Future's get method to
* wait for each write operation to complete
* Compares buffer contents
*/
throws IOException
{
// generate buffer
// write buffer completely to file
long position = 0L;
while (src.hasRemaining()) {
try {
// update position
position += n;
} catch (ExecutionException x) {
throw new RuntimeException(x.getCause());
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
}
// read file into new buffer
position = 0L;
int n;
do {
try {
// update position
if (n > 0) position += n;
} catch (ExecutionException x) {
throw new RuntimeException(x.getCause());
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
} while (n > 0);
// check buffers are the same
throw new RuntimeException("Contents differ");
}
}
// exercise lock methods
try {
// test 1 - acquire lock and check that tryLock throws
// OverlappingFileLockException
try {
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException("Should not be interrupted");
}
throw new RuntimeException("FileLock#acquiredBy returned incorrect channel");
try {
throw new RuntimeException("OverlappingFileLockException expected");
} catch (OverlappingFileLockException x) {
}
// test 2 - acquire try and check that lock throws OverlappingFileLockException
throw new RuntimeException("Unable to acquire lock");
try {
}
}
});
throw new RuntimeException("OverlappingFileLockException expected");
} catch (OverlappingFileLockException x) {
}
} finally {
}
// test 3 - channel is closed so FileLock should no longer be valid
throw new RuntimeException("FileLock expected to be invalid");
}
// interrupt should not close channel
try {
} catch (IOException x) {
x.printStackTrace();
}
}
}
});
// wait for handler to complete
}
// invoke method on closed channel
throw new RuntimeException("Channel should be closed");
// check read fails with ClosedChannelException
try {
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
// check write fails with ClosedChannelException
try {
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
// check lock fails with ClosedChannelException
try {
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
} catch (InterruptedException x) {
}
}
// exercise custom thread pool
// records threads that are created
t.setDaemon(true);
synchronized (threads) {
}
return t;
}
};
// exercise tests with varied number of threads
synchronized (threads) {
}
try {
for (int i=0; i<10; i++) {
// do I/O operation to see which thread invokes the completion handler
}
}
});
// check invoker
boolean found = false;
synchronized (threads) {
found = true;
break;
}
}
}
if (!found)
throw new RuntimeException("Invoker thread not found");
}
} finally {
}
}
// test sharing a thread pool between many channels
try {
for (int i=0; i<n; i++) {
}
}
});
// close ~half the channels
if (rand.nextBoolean())
}
} finally {
// close remaining channels
for (int i=0; i<n; i++) {
}
}
}
// exercise asynchronous close
// create file
long size = 0L;
do {
size += n;
// randomize number of writers, buffer size, and positions
for (int i=0; i<nwriters; i++) {
}
// initiate I/O
for (int i=0; i<nwriters; i++) {
}
// close file
// write operations should complete or fail with AsynchronousCloseException
for (int i=0; i<nwriters; i++) {
try {
} catch (ExecutionException x) {
if (!(cause instanceof AsynchronousCloseException))
throw new RuntimeException(cause);
} catch (CancellationException x) {
throw new RuntimeException(x); // should not happen
} catch (InterruptedException x) {
throw new RuntimeException(x); // should not happen
}
}
}
// exercise cancel method
for (int i=0; i<2; i++) {
boolean mayInterruptIfRunning = (i == 0) ? false : true;
// open with SYNC option to improve chances that write will not
// complete immediately
// start write operation
// cancel operation
// check post-conditions
throw new RuntimeException("isDone should return true");
throw new RuntimeException("isCancelled not consistent");
try {
if (cancelled)
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
if (!cancelled)
throw new RuntimeException("CancellationException not expected");
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
try {
if (cancelled)
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
if (!cancelled)
throw new RuntimeException("CancellationException not expected");
} catch (ExecutionException x) {
throw new RuntimeException(x);
} catch (TimeoutException x) {
throw new RuntimeException(x);
} catch (InterruptedException x) {
throw new RuntimeException(x);
}
}
}
// exercise truncate method
// basic tests
try {
// attempt to truncate to a size greater than the current size
throw new RuntimeException("Unexpected size after truncation");
// truncate file
throw new RuntimeException("Unexpected size after truncation");
// invalid size
try {
throw new RuntimeException("IllegalArgumentException expected");
} catch (IllegalArgumentException e) { }
} finally {
}
// channel is closed
try {
throw new RuntimeException("ClosedChannelException expected");
} catch (ClosedChannelException e) { }
// channel is read-only
try {
try {
throw new RuntimeException("NonWritableChannelException expected");
} catch (NonWritableChannelException e) { }
} finally {
}
}
// returns ByteBuffer with random bytes
static ByteBuffer genBuffer() {
if (useDirect) {
return bb;
} else {
}
}
// writes all remaining bytes in the buffer to the given channel at the
// given position
final ByteBuffer src,
long position)
{
// use position as attachment
int n = result;
if (src.hasRemaining()) {
long p = position + n;
} else {
}
}
}
});
// wait for writes to complete
}
final ByteBuffer dst,
long position)
{
// use position as attachment
int n = result;
if (n > 0) {
long p = position + n;
} else {
}
}
}
});
// wait for reads to complete
}
// wait until done
boolean done = false;
while (!done) {
try {
done = true;
} catch (InterruptedException x) { }
}
}
}