jdk/test/java/nio/channels/AsynchronousSocketChannel/StressLoopback.java
author alanb
Sun, 19 Oct 2014 11:52:53 +0100
changeset 27178 885f4428b501
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows Reviewed-by: alanb Contributed-by: kirk.shoop@microsoft.com, v-valkop@microsoft.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
     2
 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     4
 *
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     8
 *
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    13
 * accompanied this code).
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    14
 *
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3632
diff changeset
    21
 * questions.
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    22
 */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    23
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    24
/* @test
3632
399359a027de 6842687: New I/O: Update Asynchronous I/O API to jsr203/nio2-b101
alanb
parents: 3327
diff changeset
    25
 * @bug 6834246 6842687
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    26
 * @summary Stress test connections through the loopback interface
27178
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 5506
diff changeset
    27
 * @run main StressLoopback
885f4428b501 8060170: Support SIO_LOOPBACK_FAST_PATH option on Windows
alanb
parents: 5506
diff changeset
    28
 * @run main/othervm -Djdk.net.useFastTcpLoopback StressLoopback
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    29
 */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    30
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    31
import java.nio.ByteBuffer;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    32
import java.net.*;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    33
import java.nio.channels.*;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    34
import java.util.Random;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    35
import java.io.IOException;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    36
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    37
public class StressLoopback {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    38
    static final Random rand = new Random();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    39
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    41
        // setup listener
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    42
        AsynchronousServerSocketChannel listener =
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    43
            AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    44
        int port =((InetSocketAddress)(listener.getLocalAddress())).getPort();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    45
        InetAddress lh = InetAddress.getLocalHost();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    46
        SocketAddress remote = new InetSocketAddress(lh, port);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    47
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    48
        // create sources and sinks
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    49
        int count = 2 + rand.nextInt(9);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    50
        Source[] source = new Source[count];
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    51
        Sink[] sink = new Sink[count];
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    52
        for (int i=0; i<count; i++) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    53
            AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    54
            ch.connect(remote).get();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    55
            source[i] = new Source(ch);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    56
            sink[i] = new Sink(listener.accept().get());
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    57
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    58
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    59
        // start the sinks and sources
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    60
        for (int i=0; i<count; i++) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    61
            sink[i].start();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    62
            source[i].start();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    63
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    64
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    65
        // let the test run for a while
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    66
        Thread.sleep(20*1000);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    67
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    68
        // wait until everyone is done
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    69
        boolean failed = false;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    70
        long total = 0L;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    71
        for (int i=0; i<count; i++) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    72
            long nwrote = source[i].finish();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    73
            long nread = sink[i].finish();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    74
            if (nread != nwrote)
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    75
                failed = true;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    76
            System.out.format("%d -> %d (%s)\n",
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    77
                nwrote, nread, (failed) ? "FAIL" : "PASS");
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    78
            total += nwrote;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    79
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    80
        if (failed)
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    81
            throw new RuntimeException("Test failed - see log for details");
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    82
        System.out.format("Total sent %d MB\n", total / (1024L * 1024L));
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    83
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    84
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    85
    /**
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    86
     * Writes bytes to a channel until "done". When done the channel is closed.
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    87
     */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    88
    static class Source {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    89
        private final AsynchronousByteChannel channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    90
        private final ByteBuffer sentBuffer;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    91
        private volatile long bytesSent;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    92
        private volatile boolean finished;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    93
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    94
        Source(AsynchronousByteChannel channel) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    95
            this.channel = channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    96
            int size = 1024 + rand.nextInt(10000);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    97
            this.sentBuffer = (rand.nextBoolean()) ?
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    98
                ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    99
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   100
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   101
        void start() {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   102
            sentBuffer.position(0);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   103
            sentBuffer.limit(sentBuffer.capacity());
3327
82e069ae54ab 6863667: (ch) Several tests in java/nio/channels/* need to be updated after 6638712
alanb
parents: 2705
diff changeset
   104
            channel.write(sentBuffer, (Void)null, new CompletionHandler<Integer,Void> () {
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   105
                public void completed(Integer nwrote, Void att) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   106
                    bytesSent += nwrote;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   107
                    if (finished) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   108
                        closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   109
                    } else {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   110
                        sentBuffer.position(0);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   111
                        sentBuffer.limit(sentBuffer.capacity());
3327
82e069ae54ab 6863667: (ch) Several tests in java/nio/channels/* need to be updated after 6638712
alanb
parents: 2705
diff changeset
   112
                        channel.write(sentBuffer, (Void)null, this);
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   113
                    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   114
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   115
                public void failed(Throwable exc, Void att) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   116
                    exc.printStackTrace();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   117
                    closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   118
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   119
            });
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   120
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   121
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   122
        long finish() {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   123
            finished = true;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   124
            waitUntilClosed(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   125
            return bytesSent;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   126
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   127
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   128
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   129
    /**
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   130
     * Read bytes from a channel until EOF is received.
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   131
     */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   132
    static class Sink {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   133
        private final AsynchronousByteChannel channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   134
        private final ByteBuffer readBuffer;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   135
        private volatile long bytesRead;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   136
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   137
        Sink(AsynchronousByteChannel channel) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   138
            this.channel = channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   139
            int size = 1024 + rand.nextInt(10000);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   140
            this.readBuffer = (rand.nextBoolean()) ?
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   141
                ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   142
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   143
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   144
        void start() {
3327
82e069ae54ab 6863667: (ch) Several tests in java/nio/channels/* need to be updated after 6638712
alanb
parents: 2705
diff changeset
   145
            channel.read(readBuffer, (Void)null, new CompletionHandler<Integer,Void> () {
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   146
                public void completed(Integer nread, Void att) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   147
                    if (nread < 0) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   148
                        closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   149
                    } else {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   150
                        bytesRead += nread;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   151
                        readBuffer.clear();
3327
82e069ae54ab 6863667: (ch) Several tests in java/nio/channels/* need to be updated after 6638712
alanb
parents: 2705
diff changeset
   152
                        channel.read(readBuffer, (Void)null, this);
2705
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   153
                    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   154
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   155
                public void failed(Throwable exc, Void att) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   156
                    exc.printStackTrace();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   157
                    closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   158
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   159
            });
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   160
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   161
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   162
        long finish() {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   163
            waitUntilClosed(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   164
            return bytesRead;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   165
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   166
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   167
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   168
    static void waitUntilClosed(Channel c) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   169
        while (c.isOpen()) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   170
            try {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   171
                Thread.sleep(100);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   172
            } catch (InterruptedException ignore) { }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   173
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   174
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   175
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   176
    static void closeUnchecked(Channel c) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   177
        try {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   178
            c.close();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   179
        } catch (IOException ignore) { }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   180
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   181
}