jdk/test/java/nio/channels/FileChannel/LoopingTruncate.java
author igerasim
Sat, 26 Sep 2015 00:23:15 +0300
changeset 32774 6a38398a04f7
child 32836 68073065a22f
permissions -rw-r--r--
8137121: (fc) Infinite loop FileChannel.truncate Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     1
/*
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     4
 *
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     8
 *
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    13
 * accompanied this code).
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    14
 *
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    18
 *
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    21
 * questions.
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    22
 */
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    23
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    24
/**
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    25
 * @test
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    26
 * @bug 8137121
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    27
 * @summary (fc) Infinite loop FileChannel.truncate
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    28
 * @run main/othervm LoopingTruncate
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    29
 */
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    30
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    31
import java.nio.ByteBuffer;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    32
import java.nio.channels.FileChannel;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    33
import java.nio.file.Files;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    34
import java.nio.file.Path;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    35
import static java.nio.file.StandardOpenOption.*;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    36
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    37
public class LoopingTruncate {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    38
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    39
    // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    40
    static long FATEFUL_SIZE = 0x1FFFFFFFDL;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    41
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    42
    static long TIMEOUT = 10_000; // 10 seconds
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    43
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    44
    public static void main(String[] args) throws Throwable {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    45
        Path path = Files.createTempFile("LoopingTruncate.tmp", null);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    46
        try {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    47
            Thread th = new Thread(() -> {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    48
                try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    49
                    fc.position(FATEFUL_SIZE + 1L);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    50
                    fc.write(ByteBuffer.wrap(new byte[] {0}));
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    51
                    fc.truncate(FATEFUL_SIZE);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    52
                } catch (Exception e) {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    53
                    throw new RuntimeException(e);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    54
                }});
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    55
            th.start();
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    56
            th.join(TIMEOUT);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    57
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    58
            if (th.isAlive()) {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    59
                th.interrupt();
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    60
                throw new RuntimeException("Failed to complete on time");
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    61
            }
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    62
        } finally {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    63
            Files.deleteIfExists(path);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    64
        }
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    65
    }
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    66
}