8176237: (fs) java/nio/file/FileStore/Basic.java should conditionally check FileStores
authorbpb
Thu, 09 Mar 2017 08:58:58 -0800
changeset 44121 3ed4e4e86d3f
parent 44120 f569cf365ae3
child 44122 65a14579a2ae
8176237: (fs) java/nio/file/FileStore/Basic.java should conditionally check FileStores Summary: On Unix platforms, spawn a 'df' process and skip FileStore check if it hangs Reviewed-by: alanb, chegar
jdk/test/java/nio/file/FileStore/Basic.java
jdk/test/java/nio/file/FileSystem/Basic.java
jdk/test/lib/testlibrary/jdk/testlibrary/FileUtils.java
--- a/jdk/test/java/nio/file/FileStore/Basic.java	Thu Mar 09 08:55:59 2017 -0800
+++ b/jdk/test/java/nio/file/FileStore/Basic.java	Thu Mar 09 08:58:58 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -24,8 +24,11 @@
 /* @test
  * @bug 4313887 6873621 6979526 7006126 7020517
  * @summary Unit test for java.nio.file.FileStore
+ * @key intermittent
  * @library ..
- * @key intermittent
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main Basic
  */
 
 import java.nio.file.*;
@@ -33,6 +36,8 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
+import jdk.testlibrary.FileUtils;
+
 
 public class Basic {
 
@@ -110,31 +115,36 @@
         /**
          * Test: Enumerate all FileStores
          */
-        FileStore prev = null;
-        for (FileStore store: FileSystems.getDefault().getFileStores()) {
-            System.out.format("%s (name=%s type=%s)\n", store, store.name(),
-                store.type());
+        if (FileUtils.areFileSystemsAccessible()) {
+            FileStore prev = null;
+            for (FileStore store: FileSystems.getDefault().getFileStores()) {
+                System.out.format("%s (name=%s type=%s)\n", store, store.name(),
+                    store.type());
 
-            // check space attributes are accessible
-            try {
-                store.getTotalSpace();
-                store.getUnallocatedSpace();
-                store.getUsableSpace();
-            } catch (NoSuchFileException nsfe) {
-                // ignore exception as the store could have been
-                // deleted since the iterator was instantiated
-                System.err.format("%s was not found\n", store);
-            } catch (AccessDeniedException ade) {
-                // ignore exception as the lack of ability to access the
-                // store due to lack of file permission or similar does not
-                // reflect whether the space attributes would be accessible
-                // were access to be permitted
-                System.err.format("%s is inaccessible\n", store);
+                // check space attributes are accessible
+                try {
+                    store.getTotalSpace();
+                    store.getUnallocatedSpace();
+                    store.getUsableSpace();
+                } catch (NoSuchFileException nsfe) {
+                    // ignore exception as the store could have been
+                    // deleted since the iterator was instantiated
+                    System.err.format("%s was not found\n", store);
+                } catch (AccessDeniedException ade) {
+                    // ignore exception as the lack of ability to access the
+                    // store due to lack of file permission or similar does not
+                    // reflect whether the space attributes would be accessible
+                    // were access to be permitted
+                    System.err.format("%s is inaccessible\n", store);
+                }
+
+                // two distinct FileStores should not be equal
+                assertTrue(!store.equals(prev));
+                prev = store;
             }
-
-            // two distinct FileStores should not be equal
-            assertTrue(!store.equals(prev));
-            prev = store;
+        } else {
+            System.err.println
+                ("Skipping FileStore check due to file system access failure");
         }
     }
 }
--- a/jdk/test/java/nio/file/FileSystem/Basic.java	Thu Mar 09 08:55:59 2017 -0800
+++ b/jdk/test/java/nio/file/FileSystem/Basic.java	Thu Mar 09 08:58:58 2017 -0800
@@ -41,7 +41,6 @@
 import java.nio.file.Paths;
 import java.nio.file.ProviderNotFoundException;
 import java.util.HashMap;
-import java.util.concurrent.TimeUnit;
 import jdk.testlibrary.FileUtils;
 
 /**
@@ -54,41 +53,17 @@
             throw new RuntimeException(msg);
     }
 
-    static void checkFileStores(String os, FileSystem fs) throws IOException {
-        boolean checkFileStores = true;
-        if (!os.equals("Windows")) {
-            // try to check whether 'df' hangs
-            System.out.println("\n--- Begin df output ---");
-            System.out.flush();
-            Process proc = new ProcessBuilder("df").inheritIO().start();
-            try {
-                proc.waitFor(90, TimeUnit.SECONDS);
-            } catch (InterruptedException ignored) {
-            }
-            System.out.println("--- End df output ---\n");
-            System.out.flush();
-            try {
-                int exitValue = proc.exitValue();
-                if (exitValue != 0) {
-                    System.err.printf("df process exited with %d != 0%n",
-                        exitValue);
-                    checkFileStores = false;
-                }
-            } catch (IllegalThreadStateException ignored) {
-                System.err.println("df command apparently hung");
-                checkFileStores = false;
-            }
-        }
-
+    static void checkFileStores(FileSystem fs) throws IOException {
         // sanity check method
-        if (checkFileStores) {
+        if (FileUtils.areFileSystemsAccessible()) {
             System.out.println("\n--- Begin FileStores ---");
             for (FileStore store: fs.getFileStores()) {
                 System.out.println(store);
             }
             System.out.println("--- EndFileStores ---\n");
         } else {
-            System.err.println("Skipping FileStore check due to df failure");
+            System.err.println
+                ("Skipping FileStore check due to file system access failure");
         }
     }
 
@@ -133,7 +108,7 @@
             "should use 'file' scheme");
 
         // sanity check FileStores
-        checkFileStores(os, fs);
+        checkFileStores(fs);
 
         // sanity check supportedFileAttributeViews
         checkSupported(fs, "basic");
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Thu Mar 09 08:55:59 2017 -0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Thu Mar 09 08:58:58 2017 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,6 +33,7 @@
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 
 /**
@@ -190,4 +191,40 @@
         }
         return excs;
     }
+
+    /**
+     * Checks whether all file systems are accessible. This is performed
+     * by checking free disk space on all mounted file systems via a
+     * separate, spawned process. File systems are considered to be
+     * accessible if this process completes successfully before a given
+     * fixed duration has elapsed.
+     *
+     * @implNote On Unix this executes the {@code df} command in a separate
+     * process and on Windows always returns {@code true}.
+     */
+    public static boolean areFileSystemsAccessible() throws IOException {
+        boolean areFileSystemsAccessible = true;
+        if (!isWindows) {
+            // try to check whether 'df' hangs
+            System.out.println("\n--- df output ---");
+            System.out.flush();
+            Process proc = new ProcessBuilder("df").inheritIO().start();
+            try {
+                proc.waitFor(90, TimeUnit.SECONDS);
+            } catch (InterruptedException ignored) {
+            }
+            try {
+                int exitValue = proc.exitValue();
+                if (exitValue != 0) {
+                    System.err.printf("df process exited with %d != 0%n",
+                        exitValue);
+                    areFileSystemsAccessible = false;
+                }
+            } catch (IllegalThreadStateException ignored) {
+                System.err.println("df command apparently hung");
+                areFileSystemsAccessible = false;
+            }
+        }
+        return areFileSystemsAccessible;
+    }
 }