Path: blob/master/test/jdk/java/nio/file/Files/InterruptCopy.java
66646 views
/*1* Copyright (c) 2008, 2021, 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/* @test24* @bug 4313887 699326725* @summary Unit test for Sun-specific ExtendedCopyOption.INTERRUPTIBLE option26* @modules jdk.unsupported27* @library ..28*/2930import java.io.IOException;31import java.io.OutputStream;32import java.nio.file.Files;33import java.nio.file.FileStore;34import java.nio.file.Path;35import java.nio.file.StandardCopyOption;36import java.util.concurrent.Callable;37import java.util.concurrent.CountDownLatch;38import java.util.concurrent.Executors;39import java.util.concurrent.Future;40import java.util.concurrent.ScheduledExecutorService;41import com.sun.nio.file.ExtendedCopyOption;4243public class InterruptCopy {4445private static final long FILE_SIZE_TO_COPY = 1024L * 1024L * 1024L;46private static final int INTERRUPT_DELAY_IN_MS = 50;47private static final int CANCEL_DELAY_IN_MS = 10;4849public static void main(String[] args) throws Exception {50Path dir = TestUtil.createTemporaryDirectory();51try {52FileStore store = Files.getFileStore(dir);53System.out.format("Checking space (%s)\n", store);54long usableSpace = store.getUsableSpace();55if (usableSpace < 2*FILE_SIZE_TO_COPY) {56System.out.println("Insufficient disk space to run test.");57return;58}59doTest(dir);60} finally {61TestUtil.removeAll(dir);62}63}6465static void doTest(Path dir) throws Exception {66final Path source = dir.resolve("foo");67final Path target = dir.resolve("bar");6869// create source file (don't create it as sparse file because we70// require the copy to take a long time)71System.out.println("Creating source file...");72byte[] buf = new byte[32*1024];73long total = 0;74try (OutputStream out = Files.newOutputStream(source)) {75do {76out.write(buf);77total += buf.length;78} while (total < FILE_SIZE_TO_COPY);79}80System.out.println("Source file created.");8182ScheduledExecutorService pool =83Executors.newSingleThreadScheduledExecutor();8485try {86// copy source to target in main thread, interrupting it87// after a delay88final Thread me = Thread.currentThread();89final CountDownLatch interruptLatch = new CountDownLatch(1);90Future<?> wakeup = pool.submit(new Runnable() {91public void run() {92try {93interruptLatch.await();94Thread.sleep(INTERRUPT_DELAY_IN_MS);95} catch (InterruptedException ignore) {96}97System.out.printf("Interrupting at %d ms...%n",98System.currentTimeMillis());99me.interrupt();100}101});102try {103interruptLatch.countDown();104long theBeginning = System.currentTimeMillis();105System.out.printf("Copying file at %d ms...%n", theBeginning);106Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE);107long theEnd = System.currentTimeMillis();108System.out.printf("Done copying at %d ms...%n", theEnd);109long duration = theEnd - theBeginning;110111// If the copy was interrupted the target file should have been112// deleted, so if the file does not exist, then the copy must113// have been interrupted without throwing an exception; if the114// file exists, then the copy finished before being interrupted115// so not throwing an exception is not considered a failure116if (Files.notExists(target))117throw new RuntimeException("Copy was not interrupted in " +118duration + " ms");119} catch (IOException e) {120boolean interrupted = Thread.interrupted();121if (!interrupted)122throw new RuntimeException("Interrupt status was not set");123System.out.println("Copy failed (this is expected).");124}125try {126wakeup.get();127} catch (InterruptedException ignore) { }128Thread.interrupted();129130// copy source to target via task in thread pool, interrupting it131// after a delay using cancel(true)132CountDownLatch cancelLatch = new CountDownLatch(1);133Future<Void> result = pool.submit(new Callable<Void>() {134public Void call() throws IOException {135cancelLatch.countDown();136System.out.printf("Copying file at %d ms...%n",137System.currentTimeMillis());138Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,139StandardCopyOption.REPLACE_EXISTING);140System.out.printf("Done copying at %d ms...%n",141System.currentTimeMillis());142return null;143}144});145try {146cancelLatch.await();147Thread.sleep(CANCEL_DELAY_IN_MS);148} catch (InterruptedException ignore) {149}150if (result.isDone())151throw new RuntimeException("Copy finished before cancellation");152System.out.printf("Cancelling at %d ms...%n",153System.currentTimeMillis());154boolean cancelled = result.cancel(true);155if (cancelled)156System.out.println("Copy cancelled.");157else {158result.get();159throw new RuntimeException("Copy was not cancelled");160}161} finally {162pool.shutdown();163}164}165}166167168