Path: blob/master/test/jdk/java/nio/file/Files/InterruptCopy.java
41153 views
/*1* Copyright (c) 2008, 2016, 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.nio.file.*;31import java.io.*;32import java.util.concurrent.*;33import com.sun.nio.file.ExtendedCopyOption;3435public class InterruptCopy {3637private static final long FILE_SIZE_TO_COPY = 512L * 1024L * 1024L;38private static final int DELAY_IN_MS = 500;39private static final int DURATION_MAX_IN_MS = 5000;4041public static void main(String[] args) throws Exception {42Path dir = TestUtil.createTemporaryDirectory();43try {44FileStore store = Files.getFileStore(dir);45System.out.format("Checking space (%s)\n", store);46long usableSpace = store.getUsableSpace();47if (usableSpace < 2*FILE_SIZE_TO_COPY) {48System.out.println("Insufficient disk space to run test.");49return;50}51doTest(dir);52} finally {53TestUtil.removeAll(dir);54}55}5657static void doTest(Path dir) throws Exception {58final Path source = dir.resolve("foo");59final Path target = dir.resolve("bar");6061// create source file (don't create it as sparse file because we62// require the copy to take a long time)63System.out.println("Creating source file...");64byte[] buf = new byte[32*1024];65long total = 0;66try (OutputStream out = Files.newOutputStream(source)) {67do {68out.write(buf);69total += buf.length;70} while (total < FILE_SIZE_TO_COPY);71}72System.out.println("Source file created.");7374ScheduledExecutorService pool =75Executors.newSingleThreadScheduledExecutor();76try {77// copy source to target in main thread, interrupting it after a delay78final Thread me = Thread.currentThread();79Future<?> wakeup = pool.schedule(new Runnable() {80public void run() {81me.interrupt();82}}, DELAY_IN_MS, TimeUnit.MILLISECONDS);83System.out.println("Copying file...");84try {85long start = System.currentTimeMillis();86Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE);87long duration = System.currentTimeMillis() - start;88if (duration > DURATION_MAX_IN_MS)89throw new RuntimeException("Copy was not interrupted");90} catch (IOException e) {91boolean interrupted = Thread.interrupted();92if (!interrupted)93throw new RuntimeException("Interrupt status was not set");94System.out.println("Copy failed (this is expected)");95}96try {97wakeup.get();98} catch (InterruptedException ignore) { }99Thread.interrupted();100101// copy source to target via task in thread pool, interrupting it after102// a delay using cancel(true)103Future<Void> result = pool.submit(new Callable<Void>() {104public Void call() throws IOException {105System.out.println("Copying file...");106Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,107StandardCopyOption.REPLACE_EXISTING);108return null;109}110});111Thread.sleep(DELAY_IN_MS);112boolean cancelled = result.cancel(true);113if (!cancelled)114result.get();115System.out.println("Copy cancelled.");116} finally {117pool.shutdown();118}119}120}121122123