test/jdk/java/nio/channels/AsynchronousFileChannel/LotsOfWrites.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4668
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     4
 *
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     8
 *
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    13
 * accompanied this code).
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    14
 *
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4668
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4668
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4668
diff changeset
    21
 * questions.
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    22
 */
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    23
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    24
/* @test
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    25
 * @bug 6913877
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    26
 * @summary Stress AsynchronousFileChannel.write
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 5970
diff changeset
    27
 * @key randomness
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    28
 */
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    29
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    30
import java.io.*;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    31
import java.nio.ByteBuffer;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    32
import static java.nio.file.StandardOpenOption.*;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    33
import java.nio.channels.*;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    34
import java.util.Random;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    35
import java.util.concurrent.CountDownLatch;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    36
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    37
public class LotsOfWrites {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    38
    static final Random rand = new Random();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    39
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    40
    /**
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    41
     * Asynchronously writes a known pattern to a file up to a given size,
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    42
     * counting down a latch to release waiters when done.
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    43
     */
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    44
    static class Writer implements CompletionHandler<Integer,ByteBuffer> {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    45
        private final File file;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    46
        private final long size;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    47
        private final CountDownLatch latch;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    48
        private final AsynchronousFileChannel channel;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    49
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    50
        private volatile long position;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    51
        private volatile byte nextByte;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    52
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    53
        private long updatePosition(long nwrote) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    54
            position += nwrote;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    55
            return position;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    56
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    57
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    58
        private ByteBuffer genNextBuffer() {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    59
            int n = Math.min(8192 + rand.nextInt(8192), (int)(size - position));
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    60
            ByteBuffer buf = ByteBuffer.allocate(n);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    61
            for (int i=0; i<n; i++) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    62
                buf.put(nextByte++);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    63
            }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    64
            buf.flip();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    65
            return buf;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    66
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    67
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    68
        // close channel and release any waiters
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    69
        private void done() {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    70
            try {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    71
                channel.close();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    72
            } catch (IOException ignore) { }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    73
            latch.countDown();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    74
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    75
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    76
        Writer(File file, long size, CountDownLatch latch) throws IOException {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    77
            this.file = file;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    78
            this.size = size;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    79
            this.latch = latch;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    80
            this.channel = AsynchronousFileChannel.open(file.toPath(), WRITE);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    81
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    82
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    83
        File file() {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    84
            return file;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    85
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    86
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    87
        long size() {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    88
            return size;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    89
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    90
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    91
        // initiate first write
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    92
        void start() {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    93
            ByteBuffer buf = genNextBuffer();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    94
            channel.write(buf, 0L, buf, this);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    95
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    96
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    97
        @Override
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    98
        public void completed(Integer nwrote, ByteBuffer buf) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
    99
            long pos = updatePosition(nwrote);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   100
            if (!buf.hasRemaining()) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   101
                // buffer has been completely written; decide if we need to
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   102
                // write more
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   103
                if (position >= size) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   104
                    done();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   105
                    return;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   106
                }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   107
                buf = genNextBuffer();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   108
            }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   109
            channel.write(buf, pos, buf, this);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   110
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   111
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   112
        @Override
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   113
        public void failed(Throwable exc, ByteBuffer buf) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   114
            exc.printStackTrace();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   115
            done();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   116
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   117
    }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   118
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   119
    public static void main(String[] args) throws Exception {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   120
        // random number of writers
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   121
        int count = 20 + rand.nextInt(16);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   122
        Writer[] writers = new Writer[count];
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   123
        CountDownLatch latch = new CountDownLatch(count);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   124
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   125
        // initiate writing to each file
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   126
        for (int i=0; i<count; i++) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   127
            long size = 512*1024 + rand.nextInt(512*1024);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   128
            File blah = File.createTempFile("blah", null);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   129
            blah.deleteOnExit();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   130
            Writer writer = new Writer(blah, size, latch);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   131
            writers[i] = writer;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   132
            writer.start();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   133
        }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   134
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   135
        // wait for writing to complete
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   136
        latch.await();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   137
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   138
        // verify content of each file
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   139
        boolean failed = false;
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   140
        byte[] buf = new byte[8192];
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   141
        for (int i=0; i<count ;i++) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   142
            Writer writer = writers[i];
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   143
            FileInputStream in = new FileInputStream(writer.file());
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   144
            try {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   145
                long size = 0L;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   146
                byte expected = 0;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   147
                int nread = in.read(buf);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   148
                while (nread > 0) {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   149
                    for (int j=0; j<nread; j++) {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   150
                        if (buf[j] != expected) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   151
                            System.err.println("Unexpected contents");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   152
                            failed = true;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   153
                            break;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   154
                        }
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   155
                        expected++;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   156
                    }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   157
                    if (failed)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   158
                        break;
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   159
                    size += nread;
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   160
                    nread = in.read(buf);
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   161
                }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   162
                if (!failed && size != writer.size()) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   163
                    System.err.println("Unexpected size");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   164
                    failed = true;
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   165
                }
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   166
                if (failed)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   167
                    break;
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   168
            } finally {
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   169
                in.close();
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   170
            }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   171
        }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   172
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   173
        // clean-up
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   174
        for (int i=0; i<count; i++) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   175
            writers[i].file().delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   176
        }
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   177
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   178
        if (failed)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   179
            throw new RuntimeException("Test failed");
4668
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   180
    }
53e1c92056fc 6913877: (fs) AsynchronousFileChannel.write can return wrong result under load [win]
alanb
parents:
diff changeset
   181
}