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  */
  28 
  29 import java.io.*;
  30 import java.nio.ByteBuffer;
  31 import static java.nio.file.StandardOpenOption.*;
  32 import java.nio.channels.*;
  33 import java.util.Random;
  34 import java.util.concurrent.CountDownLatch;
  35 
  36 public class LotsOfWrites {
  37     static final Random rand = new Random();
  38 
  39     /**
  40      * Asynchronously writes a known pattern to a file up to a given size,
  41      * counting down a latch to release waiters when done.
  42      */
  43     static class Writer implements CompletionHandler<Integer,ByteBuffer> {
  44         private final File file;
  45         private final long size;
  46         private final CountDownLatch latch;
  47         private final AsynchronousFileChannel channel;
  48 
  49         private volatile long position;
  50         private volatile byte nextByte;
  51 
  52         private long updatePosition(long nwrote) {
  53             position += nwrote;
  54             return position;
  55         }
  56 
  57         private ByteBuffer genNextBuffer() {
  58             int n = Math.min(8192 + rand.nextInt(8192), (int)(size - position));
  59             ByteBuffer buf = ByteBuffer.allocate(n);
  60             for (int i=0; i<n; i++) {
  61                 buf.put(nextByte++);
  62             }
  63             buf.flip();
  64             return buf;
  65         }
  66 
  67         // close channel and release any waiters
  68         private void done() {
  69             try {
  70                 channel.close();
  71             } catch (IOException ignore) { }
  72             latch.countDown();
  73         }
  74 
  75         Writer(File file, long size, CountDownLatch latch) throws IOException {
  76             this.file = file;
  77             this.size = size;
  78             this.latch = latch;
  79             this.channel = AsynchronousFileChannel.open(file.toPath(), WRITE);
  80         }
  81 
  82         File file() {
  83             return file;
  84         }
  85 
  86         long size() {
  87             return size;
  88         }
  89 
  90         // initiate first write
  91         void start() {
  92             ByteBuffer buf = genNextBuffer();
  93             channel.write(buf, 0L, buf, this);
  94         }
  95 
  96         @Override
  97         public void completed(Integer nwrote, ByteBuffer buf) {
  98             long pos = updatePosition(nwrote);
  99             if (!buf.hasRemaining()) {
 100                 // buffer has been completely written; decide if we need to
 101                 // write more
 102                 if (position >= size) {
 103                     done();
 104                     return;
 105                 }
 106                 buf = genNextBuffer();
 107             }
 108             channel.write(buf, pos, buf, this);
 109         }
 110 
 111         @Override
 112         public void failed(Throwable exc, ByteBuffer buf) {
 113             exc.printStackTrace();
 114             done();
 115         }
 116     }
 117 
 118     public static void main(String[] args) throws Exception {
 119         // random number of writers
 120         int count = 20 + rand.nextInt(16);
 121         Writer[] writers = new Writer[count];
 122         CountDownLatch latch = new CountDownLatch(count);
 123 
 124         // initiate writing to each file
 125         for (int i=0; i<count; i++) {
 126             long size = 512*1024 + rand.nextInt(512*1024);
 127             File blah = File.createTempFile("blah", null);
 128             blah.deleteOnExit();
 129             Writer writer = new Writer(blah, size, latch);
 130             writers[i] = writer;
 131             writer.start();
 132         }
 133 
 134         // wait for writing to complete
 135         latch.await();
 136 
 137         // verify content of each file
 138         boolean failed = false;
 139         byte[] buf = new byte[8192];
 140         for (int i=0; i<count ;i++) {
 141             Writer writer = writers[i];
 142             FileInputStream in = new FileInputStream(writer.file());
 143             try {
 144                 long size = 0L;
 145                 byte expected = 0;
 146                 int nread = in.read(buf);
 147                 while (nread > 0) {
 148                     for (int j=0; j<nread; j++) {
 149                         if (buf[j] != expected) {
 150                             System.err.println("Unexpected contents");
 151                             failed = true;
 152                             break;
 153                         }
 154                         expected++;
 155                     }
 156                     if (failed)
 157                         break;
 158                     size += nread;
 159                     nread = in.read(buf);
 160                 }
 161                 if (!failed && size != writer.size()) {
 162                     System.err.println("Unexpected size");
 163                     failed = true;
 164                 }
 165                 if (failed)
 166                     break;
 167             } finally {
 168                 in.close();
 169             }
 170         }
 171 
 172         // clean-up
 173         for (int i=0; i<count; i++) {
 174             writers[i].file().delete();
 175         }
 176 
 177         if (failed)
 178             throw new RuntimeException("Test failed");
 179     }
 180 }