893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
893N/A * published by the Free Software Foundation.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A/* @test
2980N/A * @bug 4313887 6993267
893N/A * @summary Unit test for Sun-specific ExtendedCopyOption.INTERRUPTIBLE option
893N/A * @library ..
893N/A * @run main/othervm -XX:-UseVMInterruptibleIO InterruptCopy
893N/A */
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.io.*;
893N/Aimport java.util.concurrent.*;
893N/Aimport com.sun.nio.file.ExtendedCopyOption;
893N/A
893N/Apublic class InterruptCopy {
893N/A
2980N/A private static final long FILE_SIZE_TO_COPY = 512L * 1024L * 1024L;
893N/A private static final int DELAY_IN_MS = 500;
2980N/A private static final int DURATION_MAX_IN_MS = 5000;
893N/A
893N/A public static void main(String[] args) throws Exception {
893N/A Path dir = TestUtil.createTemporaryDirectory();
893N/A try {
3471N/A FileStore store = Files.getFileStore(dir);
893N/A System.out.format("Checking space (%s)\n", store);
3471N/A long usableSpace = store.getUsableSpace();
893N/A if (usableSpace < 2*FILE_SIZE_TO_COPY) {
893N/A System.out.println("Insufficient disk space to run test.");
893N/A return;
893N/A }
893N/A doTest(dir);
893N/A } finally {
893N/A TestUtil.removeAll(dir);
893N/A }
893N/A }
893N/A
893N/A static void doTest(Path dir) throws Exception {
893N/A final Path source = dir.resolve("foo");
893N/A final Path target = dir.resolve("bar");
893N/A
893N/A // create source file (don't create it as sparse file because we
893N/A // require the copy to take a long time)
893N/A System.out.println("Creating source file...");
893N/A byte[] buf = new byte[32*1024];
893N/A long total = 0;
3471N/A try (OutputStream out = Files.newOutputStream(source)) {
893N/A do {
893N/A out.write(buf);
893N/A total += buf.length;
893N/A } while (total < FILE_SIZE_TO_COPY);
893N/A }
893N/A System.out.println("Source file created.");
893N/A
893N/A ScheduledExecutorService pool =
893N/A Executors.newSingleThreadScheduledExecutor();
893N/A try {
893N/A // copy source to target in main thread, interrupting it after a delay
893N/A final Thread me = Thread.currentThread();
2980N/A Future<?> wakeup = pool.schedule(new Runnable() {
893N/A public void run() {
893N/A me.interrupt();
893N/A }}, DELAY_IN_MS, TimeUnit.MILLISECONDS);
893N/A System.out.println("Copying file...");
893N/A try {
2980N/A long start = System.currentTimeMillis();
3471N/A Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE);
2980N/A long duration = System.currentTimeMillis() - start;
2980N/A if (duration > DURATION_MAX_IN_MS)
2980N/A throw new RuntimeException("Copy was not interrupted");
893N/A } catch (IOException e) {
893N/A boolean interrupted = Thread.interrupted();
893N/A if (!interrupted)
893N/A throw new RuntimeException("Interrupt status was not set");
893N/A System.out.println("Copy failed (this is expected)");
893N/A }
2980N/A try {
2980N/A wakeup.get();
2980N/A } catch (InterruptedException ignore) { }
2980N/A Thread.interrupted();
893N/A
893N/A // copy source to target via task in thread pool, interrupting it after
893N/A // a delay using cancel(true)
893N/A Future<Void> result = pool.submit(new Callable<Void>() {
893N/A public Void call() throws IOException {
893N/A System.out.println("Copying file...");
3471N/A Files.copy(source, target, ExtendedCopyOption.INTERRUPTIBLE,
893N/A StandardCopyOption.REPLACE_EXISTING);
893N/A return null;
893N/A }
893N/A });
893N/A Thread.sleep(DELAY_IN_MS);
893N/A boolean cancelled = result.cancel(true);
893N/A if (!cancelled)
893N/A result.get();
893N/A System.out.println("Copy cancelled.");
893N/A } finally {
893N/A pool.shutdown();
893N/A }
893N/A }
893N/A}