test/jdk/java/nio/channels/FileChannel/AtomicAppend.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
child 50269 d8327f838b88
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     1
/*
49930
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     4
 *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     8
 *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    13
 * accompanied this code).
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    14
 *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    18
 *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    21
 * questions.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    22
 */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    23
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    24
/*
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    25
 * @test
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    26
 * @summary Check that appends are atomic
49930
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    27
 * @library /test/lib
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    28
 * @build jdk.test.lib.Platform
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    29
 * @run main AtomicAppend
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 9035
diff changeset
    30
 * @key randomness
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    31
 */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    32
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    33
import java.io.File;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    34
import java.io.FileOutputStream;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    35
import java.io.OutputStream;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    36
import java.io.IOException;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    37
import java.util.Random;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    38
import java.util.concurrent.Executors;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    39
import java.util.concurrent.ExecutorService;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    40
import java.util.concurrent.TimeUnit;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    41
import java.nio.ByteBuffer;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    42
import java.nio.channels.FileChannel;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7515
diff changeset
    43
import java.nio.file.Files;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    44
import static java.nio.file.StandardOpenOption.*;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    45
49930
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    46
import jdk.test.lib.Platform;
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    47
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    48
public class AtomicAppend {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    49
    static final Random rand = new Random();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    50
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    51
    // Open file for appending, returning FileChannel
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    52
    static FileChannel newFileChannel(File file) throws IOException {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    53
        if (rand.nextBoolean()) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    54
            return new FileOutputStream(file, true).getChannel();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    55
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    56
            return FileChannel.open(file.toPath(), APPEND);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    57
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    58
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    59
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    60
    // Open file for append, returning OutputStream
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    61
    static OutputStream newOutputStream(File file) throws IOException {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    62
        if (rand.nextBoolean()) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    63
            return new FileOutputStream(file, true);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    64
        } else {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7515
diff changeset
    65
            return Files.newOutputStream(file.toPath(), APPEND);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    66
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    67
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    68
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    69
    // write a byte to the given channel
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    70
    static void write(FileChannel fc, int b) throws IOException {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    71
        ByteBuffer buf = ByteBuffer.allocate(1);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    72
        buf.put((byte)b);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    73
        buf.flip();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    74
        if (rand.nextBoolean()) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    75
            ByteBuffer[] bufs = new ByteBuffer[1];
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    76
            bufs[0] = buf;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    77
            fc.write(bufs);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    78
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    79
            fc.write(buf);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    80
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    81
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    82
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    83
    public static void main(String[] args) throws Throwable {
49930
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    84
        if (Platform.isOSX()) {
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    85
            final String version = "10.13";
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    86
            int ineq = Platform.compareOsVersion(version);
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    87
            if (ineq >= 0) {
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    88
                System.out.format("Skipping test for macOS version %s >= %s%n",
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    89
                    Platform.getOsVersion(), version);
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    90
                return;
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    91
            }
3aaaa5370999 8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13
bpb
parents: 47216
diff changeset
    92
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    93
        final int nThreads = 16;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    94
        final int writes = 1000;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    95
        final File file = File.createTempFile("foo", null);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    96
        try {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    97
            ExecutorService pool = Executors.newFixedThreadPool(nThreads);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    98
            for (int i = 0; i < nThreads; i++)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
    99
                pool.execute(new Runnable() { public void run() {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   100
                    try {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   101
                        // randomly choose FileChannel or OutputStream
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   102
                        if (rand.nextBoolean()) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   103
                            try (FileChannel fc = newFileChannel(file)) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   104
                                for (int j=0; j<writes; j++) write(fc, 'x');
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   105
                            }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   106
                        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   107
                            try (OutputStream out = newOutputStream(file)) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   108
                                for (int j = 0; j<writes; j++) out.write('x');
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   109
                            }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   110
                        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   111
                    } catch (IOException ioe) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   112
                        ioe.printStackTrace();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   113
                    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   114
                }});
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   115
            pool.shutdown();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   116
            pool.awaitTermination(1L, TimeUnit.MINUTES);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   117
            if (file.length() != (long) (nThreads * writes))
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   118
                throw new RuntimeException("File not expected length");
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   119
        } finally {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   120
            file.delete();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   121
        }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   122
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents:
diff changeset
   123
}