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