jdk/test/java/nio/channels/FileChannel/LoopingTruncate.java
author bpb
Fri, 28 Apr 2017 16:14:57 -0700
changeset 44857 9b65ef2c7ae9
parent 44266 2b9cc0e5e79f
permissions -rw-r--r--
8179452: Remove intermittent key from some tests which appear no longer to fail Summary: Remove "intermittent" key Reviewed-by: alanb, lancea, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
     1
/*
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
32774
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
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    26
 * @bug 8137121 8137230
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    27
 * @summary (fc) Infinite loop FileChannel.truncate
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    28
 * @library /lib/testlibrary
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    29
 * @build jdk.testlibrary.Utils
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    30
 * @run main/othervm/timeout=300 LoopingTruncate
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    31
 */
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    32
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    33
import java.nio.ByteBuffer;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    34
import java.nio.channels.FileChannel;
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    35
import java.nio.channels.ClosedByInterruptException;
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    36
import java.nio.file.Files;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    37
import java.nio.file.Path;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    38
import static java.nio.file.StandardOpenOption.*;
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    39
import java.util.concurrent.TimeUnit;
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    40
import static jdk.testlibrary.Utils.adjustTimeout;
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    41
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    42
public class LoopingTruncate {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    43
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    44
    // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    45
    static long FATEFUL_SIZE = 0x1FFFFFFFDL;
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    46
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    47
    // At least 20 seconds
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    48
    static long TIMEOUT = adjustTimeout(20_000);
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    49
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    50
    public static void main(String[] args) throws Throwable {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    51
        Path path = Files.createTempFile("LoopingTruncate.tmp", null);
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    52
        try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    53
            fc.position(FATEFUL_SIZE + 1L);
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    54
            System.out.println("  Writing large file...");
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    55
            long t0 = System.nanoTime();
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    56
            fc.write(ByteBuffer.wrap(new byte[] {0}));
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    57
            long t1 = System.nanoTime();
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    58
            System.out.printf("  Wrote large file in %d ns (%d ms) %n",
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    59
                t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    60
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    61
            Thread th = new Thread(() -> {
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    62
                try {
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    63
                    System.out.println("  Truncating large file...");
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    64
                    long t2 = System.nanoTime();
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    65
                    fc.truncate(FATEFUL_SIZE);
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    66
                    long t3 = System.nanoTime();
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    67
                    System.out.printf("  Truncated large file in %d ns (%d ms) %n",
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    68
                        t3 - t2, TimeUnit.NANOSECONDS.toMillis(t3 - t2));
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    69
                } catch (ClosedByInterruptException ignore) {
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    70
                } catch (Exception e) {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    71
                    throw new RuntimeException(e);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    72
                }});
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    73
            th.start();
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    74
            th.join(TIMEOUT);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    75
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    76
            if (th.isAlive()) {
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    77
                System.err.println("=== Stack trace of the guilty thread:");
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    78
                for (StackTraceElement el : th.getStackTrace()) {
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    79
                    System.err.println("\t" + el);
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    80
                }
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    81
                System.err.println("===");
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    82
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    83
                th.interrupt();
32836
68073065a22f 8137230: TEST_BUG: java/nio/channels/FileChannel/LoopingTruncate.java timed out
igerasim
parents: 32774
diff changeset
    84
                th.join();
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    85
                throw new RuntimeException("Failed to complete on time");
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    86
            }
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    87
        } finally {
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    88
            Files.deleteIfExists(path);
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    89
        }
43816
1f49172925e2 8175041: Add success message to java/nio/channels/FileChannel/LoopingTruncate.java
bpb
parents: 32836
diff changeset
    90
1f49172925e2 8175041: Add success message to java/nio/channels/FileChannel/LoopingTruncate.java
bpb
parents: 32836
diff changeset
    91
        System.out.println("Test succeeded.");
1f49172925e2 8175041: Add success message to java/nio/channels/FileChannel/LoopingTruncate.java
bpb
parents: 32836
diff changeset
    92
        System.out.flush();
32774
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    93
    }
6a38398a04f7 8137121: (fc) Infinite loop FileChannel.truncate
igerasim
parents:
diff changeset
    94
}