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