jdk/test/java/nio/channels/FileChannel/LoopingTruncate.java
changeset 44266 2b9cc0e5e79f
parent 44107 a3d145e4f03c
child 44857 9b65ef2c7ae9
equal deleted inserted replaced
44265:ac63ae089927 44266:2b9cc0e5e79f
     1 /*
     1 /*
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    26  * @bug 8137121 8137230
    26  * @bug 8137121 8137230
    27  * @key intermittent
    27  * @key intermittent
    28  * @summary (fc) Infinite loop FileChannel.truncate
    28  * @summary (fc) Infinite loop FileChannel.truncate
    29  * @library /lib/testlibrary
    29  * @library /lib/testlibrary
    30  * @build jdk.testlibrary.Utils
    30  * @build jdk.testlibrary.Utils
    31  * @run main/othervm LoopingTruncate
    31  * @run main/othervm/timeout=300 LoopingTruncate
    32  */
    32  */
    33 
    33 
    34 import java.nio.ByteBuffer;
    34 import java.nio.ByteBuffer;
    35 import java.nio.channels.FileChannel;
    35 import java.nio.channels.FileChannel;
    36 import java.nio.channels.ClosedByInterruptException;
    36 import java.nio.channels.ClosedByInterruptException;
    37 import java.nio.file.Files;
    37 import java.nio.file.Files;
    38 import java.nio.file.Path;
    38 import java.nio.file.Path;
    39 import static java.nio.file.StandardOpenOption.*;
    39 import static java.nio.file.StandardOpenOption.*;
       
    40 import java.util.concurrent.TimeUnit;
    40 import static jdk.testlibrary.Utils.adjustTimeout;
    41 import static jdk.testlibrary.Utils.adjustTimeout;
    41 
    42 
    42 public class LoopingTruncate {
    43 public class LoopingTruncate {
    43 
    44 
    44     // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
    45     // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
    49 
    50 
    50     public static void main(String[] args) throws Throwable {
    51     public static void main(String[] args) throws Throwable {
    51         Path path = Files.createTempFile("LoopingTruncate.tmp", null);
    52         Path path = Files.createTempFile("LoopingTruncate.tmp", null);
    52         try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
    53         try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
    53             fc.position(FATEFUL_SIZE + 1L);
    54             fc.position(FATEFUL_SIZE + 1L);
       
    55             System.out.println("  Writing large file...");
       
    56             long t0 = System.nanoTime();
    54             fc.write(ByteBuffer.wrap(new byte[] {0}));
    57             fc.write(ByteBuffer.wrap(new byte[] {0}));
       
    58             long t1 = System.nanoTime();
       
    59             System.out.printf("  Wrote large file in %d ns (%d ms) %n",
       
    60                 t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
    55 
    61 
    56             Thread th = new Thread(() -> {
    62             Thread th = new Thread(() -> {
    57                 try {
    63                 try {
       
    64                     System.out.println("  Truncating large file...");
       
    65                     long t2 = System.nanoTime();
    58                     fc.truncate(FATEFUL_SIZE);
    66                     fc.truncate(FATEFUL_SIZE);
       
    67                     long t3 = System.nanoTime();
       
    68                     System.out.printf("  Truncated large file in %d ns (%d ms) %n",
       
    69                         t3 - t2, TimeUnit.NANOSECONDS.toMillis(t3 - t2));
    59                 } catch (ClosedByInterruptException ignore) {
    70                 } catch (ClosedByInterruptException ignore) {
    60                 } catch (Exception e) {
    71                 } catch (Exception e) {
    61                     throw new RuntimeException(e);
    72                     throw new RuntimeException(e);
    62                 }});
    73                 }});
    63             th.start();
    74             th.start();