jdk/test/java/nio/channels/AsynchronousSocketChannel/StressLoopback.java
author alanb
Mon, 27 Jul 2009 18:46:50 +0100
changeset 3327 82e069ae54ab
parent 2705 27b7f2d5e949
child 3632 399359a027de
permissions -rw-r--r--
6863667: (ch) Several tests in java/nio/channels/* need to be updated after 6638712 Reviewed-by: mcimadamore
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
/*
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
     2
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    21
 * have any questions.
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
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    25
 * @bug 6834246
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
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    27
 */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    28
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    29
import java.nio.ByteBuffer;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    30
import java.net.*;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    31
import java.nio.channels.*;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    32
import java.util.Random;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    33
import java.io.IOException;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    34
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    35
public class StressLoopback {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    36
    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
    37
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    38
    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
    39
        // setup listener
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    40
        AsynchronousServerSocketChannel listener =
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    41
            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
    42
        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
    43
        InetAddress lh = InetAddress.getLocalHost();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    44
        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
    45
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    46
        // create sources and sinks
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    47
        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
    48
        Source[] source = new Source[count];
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    49
        Sink[] sink = new Sink[count];
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    50
        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
    51
            AsynchronousSocketChannel ch = AsynchronousSocketChannel.open();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    52
            ch.connect(remote).get();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    53
            source[i] = new Source(ch);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    54
            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
    55
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    56
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    57
        // start the sinks and sources
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    58
        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
    59
            sink[i].start();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    60
            source[i].start();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    61
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    62
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    63
        // 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
    64
        Thread.sleep(20*1000);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    65
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    66
        // wait until everyone is done
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    67
        boolean failed = false;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    68
        long total = 0L;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    69
        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
    70
            long nwrote = source[i].finish();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    71
            long nread = sink[i].finish();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    72
            if (nread != nwrote)
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    73
                failed = true;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    74
            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
    75
                nwrote, nread, (failed) ? "FAIL" : "PASS");
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    76
            total += nwrote;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    77
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    78
        if (failed)
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    79
            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
    80
        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
    81
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    82
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
     * 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
    85
     */
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    86
    static class Source {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    87
        private final AsynchronousByteChannel channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    88
        private final ByteBuffer sentBuffer;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    89
        private volatile long bytesSent;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    90
        private volatile boolean finished;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    91
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    92
        Source(AsynchronousByteChannel channel) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    93
            this.channel = channel;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    94
            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
    95
            this.sentBuffer = (rand.nextBoolean()) ?
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    96
                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
    97
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    98
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
    99
        void start() {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   100
            sentBuffer.position(0);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   101
            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
   102
            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
   103
                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
   104
                    bytesSent += nwrote;
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   105
                    if (finished) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   106
                        closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   107
                    } else {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   108
                        sentBuffer.position(0);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   109
                        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
   110
                        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
   111
                    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   112
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   113
                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
   114
                    exc.printStackTrace();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   115
                    closeUnchecked(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   116
                }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   117
                public void cancelled(Void att) {
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
                public void cancelled(Void att) {
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
        }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   163
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   164
        long finish() {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   165
            waitUntilClosed(channel);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   166
            return bytesRead;
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
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   169
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   170
    static void waitUntilClosed(Channel c) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   171
        while (c.isOpen()) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   172
            try {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   173
                Thread.sleep(100);
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   174
            } catch (InterruptedException ignore) { }
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
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   177
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   178
    static void closeUnchecked(Channel c) {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   179
        try {
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   180
            c.close();
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   181
        } catch (IOException ignore) { }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   182
    }
27b7f2d5e949 6834246: (ch) AsynchronousSocketChannel#write completes with wrong number of bytes written under load (win)
alanb
parents:
diff changeset
   183
}