changeset 13546:d0cccfd32b1e

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
author adinn
date Fri, 24 May 2019 16:15:32 +0100
parents 32c88d12f6ae
children 63b345c88831
files test/java/nio/file/FileStore/Basic.java test/java/nio/file/FileSystem/Basic.java test/lib/testlibrary/jdk/testlibrary/FileUtils.java
diffstat 3 files changed, 80 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/nio/file/FileStore/Basic.java	Fri May 24 15:26:47 2019 +0100
+++ b/test/java/nio/file/FileStore/Basic.java	Fri May 24 16:15:32 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2019, 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,7 +24,10 @@
 /* @test
  * @bug 4313887 6873621 6979526 7006126 7020517
  * @summary Unit test for java.nio.file.FileStore
- * @library ..
+ * @key intermittent
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main Basic
  */
 
 import java.nio.file.*;
@@ -32,6 +35,7 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
+import jdk.testlibrary.FileUtils;
 
 public class Basic {
 
@@ -109,31 +113,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/test/java/nio/file/FileSystem/Basic.java	Fri May 24 15:26:47 2019 +0100
+++ b/test/java/nio/file/FileSystem/Basic.java	Fri May 24 16:15:32 2019 +0100
@@ -24,7 +24,9 @@
 /* @test
  * @bug 4313887 6838333
  * @summary Unit test for java.nio.file.FileSystem
- * @library ..
+ * @library .. /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @run main Basic
  */
 
 import java.io.File;
@@ -40,6 +42,7 @@
 import java.nio.file.ProviderNotFoundException;
 import java.util.HashMap;
 import java.util.concurrent.TimeUnit;
+import jdk.testlibrary.FileUtils;
 
 /**
  * Simple santity checks for java.nio.file.FileSystem
@@ -51,41 +54,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");
         }
     }
 
@@ -114,7 +93,7 @@
             "should use 'file' scheme");
 
         // sanity check FileStores
-        checkFileStores(os, fs);
+        checkFileStores(fs);
 
         // sanity check supportedFileAttributeViews
         checkSupported(fs, "basic");
--- a/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Fri May 24 15:26:47 2019 +0100
+++ b/test/lib/testlibrary/jdk/testlibrary/FileUtils.java	Fri May 24 16:15:32 2019 +0100
@@ -33,6 +33,7 @@
 import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 
 /**
@@ -190,5 +191,41 @@
         }
         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;
+    }
 }