Path: blob/master/test/jdk/java/nio/channels/FileChannel/LoopingTruncate.java
41154 views
/*1* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8137121 813723026* @summary (fc) Infinite loop FileChannel.truncate27* @library /test/lib28* @build jdk.test.lib.Utils29* @run main/othervm/timeout=300 LoopingTruncate30*/3132import java.nio.ByteBuffer;33import java.nio.channels.FileChannel;34import java.nio.channels.ClosedByInterruptException;35import java.nio.file.Files;36import java.nio.file.Path;37import static java.nio.file.StandardOpenOption.*;38import java.util.concurrent.TimeUnit;39import static jdk.test.lib.Utils.adjustTimeout;4041public class LoopingTruncate {4243// (int)FATEFUL_SIZE == -3 == IOStatus.INTERRUPTED44static long FATEFUL_SIZE = 0x1FFFFFFFDL;4546// At least 20 seconds47static long TIMEOUT = adjustTimeout(20_000);4849public static void main(String[] args) throws Throwable {50Path path = Files.createTempFile("LoopingTruncate.tmp", null);51try (FileChannel fc = FileChannel.open(path, CREATE, WRITE)) {52fc.position(FATEFUL_SIZE + 1L);53System.out.println(" Writing large file...");54long t0 = System.nanoTime();55fc.write(ByteBuffer.wrap(new byte[] {0}));56long t1 = System.nanoTime();57System.out.printf(" Wrote large file in %d ns (%d ms) %n",58t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));5960Thread th = new Thread(() -> {61try {62System.out.println(" Truncating large file...");63long t2 = System.nanoTime();64fc.truncate(FATEFUL_SIZE);65long t3 = System.nanoTime();66System.out.printf(" Truncated large file in %d ns (%d ms) %n",67t3 - t2, TimeUnit.NANOSECONDS.toMillis(t3 - t2));68} catch (ClosedByInterruptException ignore) {69} catch (Exception e) {70throw new RuntimeException(e);71}});72th.start();73th.join(TIMEOUT);7475if (th.isAlive()) {76System.err.println("=== Stack trace of the guilty thread:");77for (StackTraceElement el : th.getStackTrace()) {78System.err.println("\t" + el);79}80System.err.println("===");8182th.interrupt();83th.join();84throw new RuntimeException("Failed to complete on time");85}86} finally {87Files.deleteIfExists(path);88}8990System.out.println("Test succeeded.");91System.out.flush();92}93}949596