1 /*
   2  * Copyright (c) 2010, 2011, 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 /*
  25  * @test
  26  * @summary Check that appends are atomic
  27  * @key randomness
  28  */
  29 
  30 import java.io.File;
  31 import java.io.FileOutputStream;
  32 import java.io.OutputStream;
  33 import java.io.IOException;
  34 import java.util.Random;
  35 import java.util.concurrent.Executors;
  36 import java.util.concurrent.ExecutorService;
  37 import java.util.concurrent.TimeUnit;
  38 import java.nio.ByteBuffer;
  39 import java.nio.channels.FileChannel;
  40 import java.nio.file.Files;
  41 import static java.nio.file.StandardOpenOption.*;
  42 
  43 public class AtomicAppend {
  44     static final Random rand = new Random();
  45 
  46     // Open file for appending, returning FileChannel
  47     static FileChannel newFileChannel(File file) throws IOException {
  48         if (rand.nextBoolean()) {
  49             return new FileOutputStream(file, true).getChannel();
  50         } else {
  51             return FileChannel.open(file.toPath(), APPEND);
  52         }
  53     }
  54 
  55     // Open file for append, returning OutputStream
  56     static OutputStream newOutputStream(File file) throws IOException {
  57         if (rand.nextBoolean()) {
  58             return new FileOutputStream(file, true);
  59         } else {
  60             return Files.newOutputStream(file.toPath(), APPEND);
  61         }
  62     }
  63 
  64     // write a byte to the given channel
  65     static void write(FileChannel fc, int b) throws IOException {
  66         ByteBuffer buf = ByteBuffer.allocate(1);
  67         buf.put((byte)b);
  68         buf.flip();
  69         if (rand.nextBoolean()) {
  70             ByteBuffer[] bufs = new ByteBuffer[1];
  71             bufs[0] = buf;
  72             fc.write(bufs);
  73         } else {
  74             fc.write(buf);
  75         }
  76     }
  77 
  78     public static void main(String[] args) throws Throwable {
  79         final int nThreads = 16;
  80         final int writes = 1000;
  81         final File file = File.createTempFile("foo", null);
  82         try {
  83             ExecutorService pool = Executors.newFixedThreadPool(nThreads);
  84             for (int i = 0; i < nThreads; i++)
  85                 pool.execute(new Runnable() { public void run() {
  86                     try {
  87                         // randomly choose FileChannel or OutputStream
  88                         if (rand.nextBoolean()) {
  89                             try (FileChannel fc = newFileChannel(file)) {
  90                                 for (int j=0; j<writes; j++) write(fc, 'x');
  91                             }
  92                         } else {
  93                             try (OutputStream out = newOutputStream(file)) {
  94                                 for (int j = 0; j<writes; j++) out.write('x');
  95                             }
  96                         }
  97                     } catch (IOException ioe) {
  98                         ioe.printStackTrace();
  99                     }
 100                 }});
 101             pool.shutdown();
 102             pool.awaitTermination(1L, TimeUnit.MINUTES);
 103             if (file.length() != (long) (nThreads * writes))
 104                 throw new RuntimeException("File not expected length");
 105         } finally {
 106             file.delete();
 107         }
 108     }
 109 }