1 /*
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.
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  * @summary (fc) Infinite loop FileChannel.truncate
28  * @library /lib/testlibrary
29  * @build jdk.testlibrary.Utils
30  * @run main/othervm/timeout=300 LoopingTruncate
31  */
32 
33 import java.nio.ByteBuffer;
34 import java.nio.channels.FileChannel;
35 import java.nio.channels.ClosedByInterruptException;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import static java.nio.file.StandardOpenOption.*;
39 import java.util.concurrent.TimeUnit;
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 
main(String[] args)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             System.out.println("  Writing large file...");
55             long t0 = System.nanoTime();
56             fc.write(ByteBuffer.wrap(new byte[] {0}));
57             long t1 = System.nanoTime();
58             System.out.printf("  Wrote large file in %d ns (%d ms) %n",
59                 t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
60 
61             Thread th = new Thread(() -> {
62                 try {
63                     System.out.println("  Truncating large file...");
64                     long t2 = System.nanoTime();
65                     fc.truncate(FATEFUL_SIZE);
66                     long t3 = System.nanoTime();
67                     System.out.printf("  Truncated large file in %d ns (%d ms) %n",
68                         t3 - t2, TimeUnit.NANOSECONDS.toMillis(t3 - t2));
69                 } catch (ClosedByInterruptException ignore) {
70                 } catch (Exception e) {
71                     throw new RuntimeException(e);
72                 }});
73             th.start();
74             th.join(TIMEOUT);
75 
76             if (th.isAlive()) {
77                 System.err.println("=== Stack trace of the guilty thread:");
78                 for (StackTraceElement el : th.getStackTrace()) {
79                     System.err.println("\t" + el);
80                 }
81                 System.err.println("===");
82 
83                 th.interrupt();
84                 th.join();
85                 throw new RuntimeException("Failed to complete on time");
86             }
87         } finally {
88             Files.deleteIfExists(path);
89         }
90 
91         System.out.println("Test succeeded.");
92         System.out.flush();
93     }
94 }
95