diff options
author | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-08-03 17:08:35 +0100 |
---|---|---|
committer | Przemyslaw Szczepaniak <pszczepaniak@google.com> | 2016-08-04 10:35:44 +0100 |
commit | f51b43afc2e930443452631896830f7cecc450a4 (patch) | |
tree | e0190b94a424efc8da9cde54643e7ed4f05b15fc | |
parent | efbc9a1e3829c601abfb37e5a654a46ed32dbb3e (diff) | |
download | apache-harmony-f51b43afc2e930443452631896830f7cecc450a4.tar.gz |
Fix #test_getFreeSpace flakiness
Using Process and "df" for obtaining the amount
of used, free and total blocks was flaky on
some devices (the amount of free space was sometimes
changing by page or two, for unknown reason).
Bug: 30587461
Test: org.apache.harmony.luni.tests.java.io.UnixFileTest
Change-Id: I49cd516ba63cf645fad4a7a88dd5932ea607bd27
(cherry picked from commit 7ade2296fac485ca521487581d4525c5ee71b510)
-rw-r--r-- | luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java | 49 |
1 files changed, 12 insertions, 37 deletions
diff --git a/luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java b/luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java index e457cd1..4d549fd 100644 --- a/luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java +++ b/luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java @@ -19,6 +19,7 @@ package org.apache.harmony.luni.tests.java.io; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -28,6 +29,8 @@ import tests.support.resource.Support_Resources; import junit.framework.TestCase; import libcore.io.Libcore; +import android.system.StructStatVfs; + /** * Please note that this case can only be passed on Linux due to some file @@ -85,44 +88,16 @@ public class UnixFileTest extends TestCase { } private static long getLinuxSpace(int index, File file) throws Exception { - long[] result = new long[3]; - String par = file.getAbsolutePath(); - String osName = System.getProperty("os.name"); - // in case the test case will run under other OS. - if (osName.toLowerCase().indexOf("linux") != -1) { - String[] cmd = new String[2]; - cmd[0] = "df"; - cmd[1] = par; // get the total space of file - Runtime rt = Runtime.getRuntime(); - - Process proc = rt.exec(cmd); - // get output from the command - ConsoleResulter outputResult = new ConsoleResulter(proc, proc - .getInputStream()); - - synchronized (proc) { - outputResult.start(); - proc.wait(); - } - // If there is no error, obtain the result - if (outputResult.resStr != null) { - // exit the subprocess safely - proc.waitFor(); - - // Split the results and look for the matching numerical values - String[] txtResult = outputResult.resStr.split(" "); - for (int i = 0, j = 0; i < txtResult.length; i++) { - String text = txtResult[i]; - if (text.matches("[0-9]+") && text.length() > 3) { - result[j++] = Long.parseLong(txtResult[i]) * 1024L; - } - } - } + StructStatVfs stat = Libcore.os.statvfs(file.getAbsolutePath()); + switch (index) { + case TOTAL_SPACE_NUM: + return stat.f_frsize * stat.f_blocks; + case FREE_SPACE_NUM: + return stat.f_frsize * stat.f_bfree; + case USABLE_SPACE_NUM: + return stat.f_frsize * stat.f_bavail; } - - // calculate free spaces according to df command - result[1] = result[0] - result[1]; - return result[index]; + throw new IllegalArgumentException("Unknown index: " + index); } /** |