jdk/test/java/nio/file/Path/InterruptCopy.java
changeset 2057 3acf8e5e2ca0
child 5506 202f599c92aa
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 4313887
       
    26  * @summary Unit test for Sun-specific ExtendedCopyOption.INTERRUPTIBLE option
       
    27  * @library ..
       
    28  * @run main/othervm -XX:-UseVMInterruptibleIO InterruptCopy
       
    29  */
       
    30 
       
    31 import java.nio.file.*;
       
    32 import java.nio.file.attribute.Attributes;
       
    33 import java.io.*;
       
    34 import java.util.concurrent.*;
       
    35 import com.sun.nio.file.ExtendedCopyOption;
       
    36 
       
    37 public class InterruptCopy {
       
    38 
       
    39     private static final long FILE_SIZE_TO_COPY = 512 * 1024 * 1024;
       
    40     private static final int DELAY_IN_MS = 500;
       
    41 
       
    42     public static void main(String[] args) throws Exception {
       
    43         Path dir = TestUtil.createTemporaryDirectory();
       
    44         try {
       
    45             FileStore store = dir.getFileStore();
       
    46             System.out.format("Checking space (%s)\n", store);
       
    47             long usableSpace = Attributes
       
    48                 .readFileStoreSpaceAttributes(store).usableSpace();
       
    49             if (usableSpace < 2*FILE_SIZE_TO_COPY) {
       
    50                 System.out.println("Insufficient disk space to run test.");
       
    51                 return;
       
    52             }
       
    53             doTest(dir);
       
    54         } finally {
       
    55             TestUtil.removeAll(dir);
       
    56         }
       
    57     }
       
    58 
       
    59     static void doTest(Path dir) throws Exception {
       
    60         final Path source = dir.resolve("foo");
       
    61         final Path target = dir.resolve("bar");
       
    62 
       
    63         // create source file (don't create it as sparse file because we
       
    64         // require the copy to take a long time)
       
    65         System.out.println("Creating source file...");
       
    66         byte[] buf = new byte[32*1024];
       
    67         long total = 0;
       
    68         OutputStream out = source.newOutputStream();
       
    69         try {
       
    70             do {
       
    71                 out.write(buf);
       
    72                 total += buf.length;
       
    73             } while (total < FILE_SIZE_TO_COPY);
       
    74         } finally {
       
    75             out.close();
       
    76         }
       
    77         System.out.println("Source file created.");
       
    78 
       
    79         ScheduledExecutorService pool =
       
    80             Executors.newSingleThreadScheduledExecutor();
       
    81         try {
       
    82             // copy source to target in main thread, interrupting it after a delay
       
    83             final Thread me = Thread.currentThread();
       
    84             pool.schedule(new Runnable() {
       
    85                 public void run() {
       
    86                     me.interrupt();
       
    87                 }}, DELAY_IN_MS, TimeUnit.MILLISECONDS);
       
    88             System.out.println("Copying file...");
       
    89             try {
       
    90                 source.copyTo(target, ExtendedCopyOption.INTERRUPTIBLE);
       
    91                 throw new RuntimeException("Copy completed (this is not expected)");
       
    92             } catch (IOException e) {
       
    93                 boolean interrupted = Thread.interrupted();
       
    94                 if (!interrupted)
       
    95                     throw new RuntimeException("Interrupt status was not set");
       
    96                 System.out.println("Copy failed (this is expected)");
       
    97             }
       
    98 
       
    99             // copy source to target via task in thread pool, interrupting it after
       
   100             // a delay using cancel(true)
       
   101             Future<Void> result = pool.submit(new Callable<Void>() {
       
   102                 public Void call() throws IOException {
       
   103                     System.out.println("Copying file...");
       
   104                     source.copyTo(target, ExtendedCopyOption.INTERRUPTIBLE,
       
   105                         StandardCopyOption.REPLACE_EXISTING);
       
   106                     return null;
       
   107                 }
       
   108             });
       
   109             Thread.sleep(DELAY_IN_MS);
       
   110             boolean cancelled = result.cancel(true);
       
   111             if (!cancelled)
       
   112                 result.get();
       
   113             System.out.println("Copy cancelled.");
       
   114         } finally {
       
   115             pool.shutdown();
       
   116             pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
       
   117         }
       
   118     }
       
   119 }