/*
* 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 6842687 6878369 6944810 7023403
* @summary Unit test for AsynchronousSocketChannel
* @run main Basic -skipSlowConnectTest
*/
public class Basic {
static boolean skipSlowConnectTest = false;
switch (arg) {
case "-skipSlowConnectTest" :
skipSlowConnectTest = true;
break;
default:
}
}
testBind();
testConnect();
testCancel();
testRead1();
testRead2();
testRead3();
testWrite1();
testWrite2();
testTimeout();
testShutdown();
}
}
return address;
}
}
}
}
throw new RuntimeException("Local address should be 'null'");
// check local address after binding
throw new RuntimeException("Unexpected port");
throw new RuntimeException("Not bound to a wildcard address");
// try to re-bind
try {
throw new RuntimeException("AlreadyBoundException expected");
} catch (AlreadyBoundException x) {
}
}
// check ClosedChannelException
try {
throw new RuntimeException("ClosedChannelException expected");
} catch (ClosedChannelException x) {
}
}
.setOption(SO_REUSEADDR, true);
throw new RuntimeException("setOption caused SO_SNDBUF to decrease");
throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
// default values
throw new RuntimeException("Default of SO_KEEPALIVE should be 'false'");
throw new RuntimeException("Default of TCP_NODELAY should be 'false'");
// set and check
throw new RuntimeException("SO_KEEPALIVE did not change");
throw new RuntimeException("SO_KEEPALIVE did not change");
// read others (can't check as actual value is implementation dependent)
}
}
// check local address
throw new RuntimeException("Not bound to local address");
// check remote address
throw new RuntimeException("Connected to unexpected port");
throw new RuntimeException("Connected to unexpected address");
// try to connect again
try {
throw new RuntimeException("AlreadyConnectedException expected");
} catch (AlreadyConnectedException x) {
}
// clean-up
}
// check that connect fails with ClosedChannelException
try {
throw new RuntimeException("ExecutionException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("Cause of ClosedChannelException expected");
}
}
}
});
}
throw new RuntimeException("ClosedChannelException expected");
}
// test that failure to connect closes the channel
try {
} catch (ExecutionException x) {
// failed to establish connection
throw new RuntimeException("Channel should be closed");
}
}
// repeat test by connecting to a (probably) non-existent host. This
// improves the chance that the connect will not fail immediately.
if (!skipSlowConnectTest) {
try {
} catch (ExecutionException x) {
// failed to establish connection
throw new RuntimeException("Channel should be closed");
}
}
}
}
// asynchronous close while connecting
// give time to initiate the connect (SYN)
// close
// check that exception is thrown in timely manner
try {
} catch (TimeoutException x) {
throw new RuntimeException("AsynchronousCloseException not thrown");
} catch (ExecutionException x) {
// expected
}
// attempt a second read - should fail with ReadPendingException
try {
throw new RuntimeException("ReadPendingException expected");
} catch (ReadPendingException x) {
}
// close channel (should cause initial read to complete)
// check that AsynchronousCloseException is thrown
try {
throw new RuntimeException("Should not read");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof AsynchronousCloseException))
throw new RuntimeException(x);
}
new AtomicReference<Throwable>();
// write bytes to fill socket buffer
}
writeException.set(x);
}
});
// give time for socket buffer to fill up.
// attempt a concurrent write - should fail with WritePendingException
try {
throw new RuntimeException("WritePendingException expected");
} catch (WritePendingException x) {
}
// close channel - should cause initial write to complete
// wait for exception
}
throw new RuntimeException("AsynchronousCloseException expected");
}
}
for (int i=0; i<2; i++) {
boolean mayInterruptIfRunning = (i == 0) ? false : true;
// establish loopback connection
// start read operation
// cancel operation
// check post-conditions
throw new RuntimeException("isDone should return true");
throw new RuntimeException("isCancelled not consistent");
try {
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
}
try {
throw new RuntimeException("CancellationException expected");
} catch (CancellationException x) {
}
// check that the cancel doesn't impact writing to the channel
if (!mayInterruptIfRunning) {
}
}
}
}
// read with 0 bytes remaining should complete immediately
if (n != 0)
throw new RuntimeException("0 expected");
// write bytes and close connection
while (src.hasRemaining())
}
// reads should complete immediately
int n = result;
if (n > 0) {
} else {
}
}
}
});
// check buffers
throw new RuntimeException("Contents differ");
}
// close channel
// 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");
}
}
}
// read until the buffer is full
if (dst.hasRemaining()) {
} else {
}
}
}
});
// trickle the writing
do {
for (int i=0; i<size; i++)
while (buf.hasRemaining())
} while (src.hasRemaining());
// wait until ascynrhonous reading has completed
// check buffers
throw new RuntimeException("Contents differ");
}
}
}
// exercise scattering read
}
// scattering read that completes ascynhronously
long n = result;
if (n <= 0)
throw new RuntimeException("No bytes read");
}
}
});
// write some bytes
// read should now complete
// write more bytes
// read should complete immediately
}
long n = result;
if (n <= 0)
throw new RuntimeException("No bytes read");
}
}
});
}
}
// write with 0 bytes remaining should complete immediately
if (n != 0)
throw new RuntimeException("0 expected");
// write all bytes and close connection when done
if (src.hasRemaining()) {
} else {
try {
} catch (IOException ignore) { }
}
}
}
});
// read to EOF or buffer full
do {
} while (n > 0);
// check buffers
throw new RuntimeException("Contents differ");
}
// 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");
}
}
}
// exercise gathering write
// number of bytes written
// write buffers (should complete immediately)
long n = result;
if (n <= 0)
throw new RuntimeException("No bytes read");
}
}
});
// set to true to signal that no more buffers should be written
// write until socket buffer is full so as to create the conditions
// for when a write does not complete immediately
long n = result;
if (n <= 0)
throw new RuntimeException("No bytes written");
if (continueWriting.get()) {
}
}
}
});
// give time for socket buffer to fill up.
// signal handler to stop further writing
continueWriting.set(false);
// read until done
long total = 0L;
do {
if (n <= 0)
throw new RuntimeException("No bytes read");
total += n;
}
}
{
int n;
// check read
ch.shutdownInput();
if (n != -1)
throw new RuntimeException("-1 expected");
// check full with full buffer
if (n != -1)
throw new RuntimeException("-1 expected");
// check write
ch.shutdownOutput();
try {
throw new RuntimeException("ClosedChannelException expected");
} catch (ExecutionException x) {
if (!(x.getCause() instanceof ClosedChannelException))
throw new RuntimeException("ClosedChannelException expected");
}
}
}
}
}
// this read should timeout if value is > 0
}
}
});
if (timeout > 0L) {
// wait for exception
}
throw new RuntimeException("InterruptedByTimeoutException expected");
// after a timeout then further reading should throw unspecified runtime exception
boolean exceptionThrown = false;
try {
} catch (RuntimeException x) {
exceptionThrown = true;
}
if (!exceptionThrown)
throw new RuntimeException("RuntimeException expected after timeout.");
} else {
throw new RuntimeException(exc);
}
// write bytes to fill socket buffer
{
}
}
});
if (timeout > 0) {
// wait for exception
}
throw new RuntimeException("InterruptedByTimeoutException expected");
// after a timeout then further writing should throw unspecified runtime exception
boolean exceptionThrown = false;
try {
} catch (RuntimeException x) {
exceptionThrown = true;
}
if (!exceptionThrown)
throw new RuntimeException("RuntimeException expected after timeout.");
} else {
throw new RuntimeException(exc);
}
// clean-up
}
}
// returns ByteBuffer with random bytes
if (useDirect) {
return bb;
} else {
}
}
// return ByteBuffer[] with random bytes
int len = 1;
if (max > 1)
for (int i=0; i<len; i++)
return bufs;
}
// return random SocketAddress
try {
} catch (UnknownHostException x) {
throw new InternalError("Should not happen");
}
}
}