1 /*
   2  * Copyright (c) 2015, 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  * @bug 8137121 8137230
  27  * @key intermittent
  28  * @summary (fc) Infinite loop FileChannel.truncate
  29  * @library /lib/testlibrary
  30  * @build jdk.testlibrary.Utils
  31  * @run main/othervm LoopingTruncate
  32  */
  33 
  34 import java.nio.ByteBuffer;
  35 import java.nio.channels.FileChannel;
  36 import java.nio.channels.ClosedByInterruptException;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import static java.nio.file.StandardOpenOption.*;
  40 import static jdk.testlibrary.Utils.adjustTimeout;
  41 
  42 public class LoopingTruncate {
  43 
  44     // (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED
  45     static long FATEFUL_SIZE = 0x1FFFFFFFDL;
  46 
  47     // At least 20 seconds
  48     static long TIMEOUT = adjustTimeout(20_000);
  49 
  50     public static void main(String[] args) throws Throwable {
  51         Path path = Files.createTempFile("LoopingTruncate.tmp", null);
  52         try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {
  53             fc.position(FATEFUL_SIZE + 1L);
  54             fc.write(ByteBuffer.wrap(new byte[] {0}));
  55 
  56             Thread th = new Thread(() -> {
  57                 try {
  58                     fc.truncate(FATEFUL_SIZE);
  59                 } catch (ClosedByInterruptException ignore) {
  60                 } catch (Exception e) {
  61                     throw new RuntimeException(e);
  62                 }});
  63             th.start();
  64             th.join(TIMEOUT);
  65 
  66             if (th.isAlive()) {
  67                 System.err.println("=== Stack trace of the guilty thread:");
  68                 for (StackTraceElement el : th.getStackTrace()) {
  69                     System.err.println("\t" + el);
  70                 }
  71                 System.err.println("===");
  72 
  73                 th.interrupt();
  74                 th.join();
  75                 throw new RuntimeException("Failed to complete on time");
  76             }
  77         } finally {
  78             Files.deleteIfExists(path);
  79         }
  80 
  81         System.out.println("Test succeeded.");
  82         System.out.flush();
  83     }
  84 }