StressLoopback.java revision 1434
0N/A/*
1879N/A * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
1472N/A * CA 95054 USA or visit www.sun.com if you need additional information or
1472N/A * have any questions.
0N/A */
0N/A
0N/A/* @test
1879N/A * @bug 6834246
1879N/A * @summary Stress test connections through the loopback interface
1879N/A */
1879N/A
1879N/Aimport java.nio.ByteBuffer;
1879N/Aimport java.net.*;
1879N/Aimport java.nio.channels.*;
1879N/Aimport java.util.Random;
1879N/Aimport java.io.IOException;
1879N/A
0N/Apublic class StressLoopback {
0N/A static final Random rand = new Random();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A // setup listener
0N/A AsynchronousServerSocketChannel listener =
0N/A AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
0N/A int port =((InetSocketAddress)(listener.getLocalAddress())).getPort();
0N/A InetAddress lh = InetAddress.getLocalHost();
0N/A SocketAddress remote = new InetSocketAddress(lh, port);
0N/A
0N/A // create sources and sinks
0N/A int count = 2 + rand.nextInt(9);
0N/A Source[] source = new Source[count];
0N/A Sink[] sink = new Sink[count];
0N/A for (int i=0; i<count; i++) {
0N/A AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
0N/A ch.connect(remote).get();
0N/A source[i] = new Source(ch);
0N/A sink[i] = new Sink(listener.accept().get());
0N/A }
0N/A
0N/A // start the sinks and sources
0N/A for (int i=0; i<count; i++) {
0N/A sink[i].start();
0N/A source[i].start();
0N/A }
0N/A
0N/A // let the test run for a while
0N/A Thread.sleep(20*1000);
0N/A
0N/A // wait until everyone is done
0N/A boolean failed = false;
0N/A long total = 0L;
0N/A for (int i=0; i<count; i++) {
0N/A long nwrote = source[i].finish();
0N/A long nread = sink[i].finish();
0N/A if (nread != nwrote)
0N/A failed = true;
0N/A System.out.format("%d -> %d (%s)\n",
0N/A nwrote, nread, (failed) ? "FAIL" : "PASS");
0N/A total += nwrote;
0N/A }
0N/A if (failed)
0N/A throw new RuntimeException("Test failed - see log for details");
0N/A System.out.format("Total sent %d MB\n", total / (1024L * 1024L));
0N/A }
0N/A
0N/A /**
0N/A * Writes bytes to a channel until "done". When done the channel is closed.
0N/A */
0N/A static class Source {
0N/A private final AsynchronousByteChannel channel;
0N/A private final ByteBuffer sentBuffer;
0N/A private volatile long bytesSent;
0N/A private volatile boolean finished;
0N/A
0N/A Source(AsynchronousByteChannel channel) {
0N/A this.channel = channel;
0N/A int size = 1024 + rand.nextInt(10000);
0N/A this.sentBuffer = (rand.nextBoolean()) ?
0N/A ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
0N/A }
0N/A
0N/A void start() {
0N/A sentBuffer.position(0);
0N/A sentBuffer.limit(sentBuffer.capacity());
0N/A channel.write(sentBuffer, (Void)null, new CompletionHandler<Integer,Void> () {
0N/A public void completed(Integer nwrote, Void att) {
0N/A bytesSent += nwrote;
0N/A if (finished) {
0N/A closeUnchecked(channel);
0N/A } else {
0N/A sentBuffer.position(0);
0N/A sentBuffer.limit(sentBuffer.capacity());
0N/A channel.write(sentBuffer, (Void)null, this);
0N/A }
0N/A }
0N/A public void failed(Throwable exc, Void att) {
0N/A exc.printStackTrace();
0N/A closeUnchecked(channel);
0N/A }
0N/A public void cancelled(Void att) {
0N/A }
0N/A });
0N/A }
0N/A
0N/A long finish() {
0N/A finished = true;
0N/A waitUntilClosed(channel);
0N/A return bytesSent;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Read bytes from a channel until EOF is received.
0N/A */
0N/A static class Sink {
0N/A private final AsynchronousByteChannel channel;
0N/A private final ByteBuffer readBuffer;
0N/A private volatile long bytesRead;
0N/A
0N/A Sink(AsynchronousByteChannel channel) {
0N/A this.channel = channel;
0N/A int size = 1024 + rand.nextInt(10000);
0N/A this.readBuffer = (rand.nextBoolean()) ?
0N/A ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
0N/A }
0N/A
0N/A void start() {
0N/A channel.read(readBuffer, (Void)null, new CompletionHandler<Integer,Void> () {
0N/A public void completed(Integer nread, Void att) {
0N/A if (nread < 0) {
0N/A closeUnchecked(channel);
0N/A } else {
0N/A bytesRead += nread;
1969N/A readBuffer.clear();
0N/A channel.read(readBuffer, (Void)null, this);
0N/A }
0N/A }
0N/A public void failed(Throwable exc, Void att) {
0N/A exc.printStackTrace();
0N/A closeUnchecked(channel);
0N/A }
0N/A public void cancelled(Void att) {
0N/A }
0N/A });
0N/A }
0N/A
0N/A long finish() {
0N/A waitUntilClosed(channel);
0N/A return bytesRead;
0N/A }
0N/A }
0N/A
0N/A static void waitUntilClosed(Channel c) {
0N/A while (c.isOpen()) {
0N/A try {
0N/A Thread.sleep(100);
0N/A } catch (InterruptedException ignore) { }
0N/A }
0N/A }
0N/A
0N/A static void closeUnchecked(Channel c) {
0N/A try {
0N/A c.close();
0N/A } catch (IOException ignore) { }
0N/A }
0N/A}
0N/A