diff options
author | Daulet Zhanguzin <dauletz@google.com> | 2019-12-05 18:06:14 +0000 |
---|---|---|
committer | Daulet Zhanguzin <dauletz@google.com> | 2019-12-06 15:45:17 +0000 |
commit | 288fde0f567b926ca79e8d46fb1841813ebf98b6 (patch) | |
tree | 6ba1c7e799fa519cc5305c8475a3e8a15a50ccc4 | |
parent | a44951c4e76027f2dccd9a24c1f9d1abf9869ca9 (diff) | |
download | apache-harmony-288fde0f567b926ca79e8d46fb1841813ebf98b6.tar.gz |
Add retry to flaky UnixFileTests.
There is always going to be a race between the two system calls so the workaround to only fail the test if it does not succeed after multiple retries.
Fixes: 145338739
Test: tested with
'CtsLibcoreTestCases:org.apache.harmony.luni.tests.java.io.UnixFileTest
--rerun-until-failure 1000', however the same command didn't repro the
bug on master, hence flaky fix is not confirmed.
Change-Id: I60aa0f7bd31affef8ba9d3c340ca581d425c4d70
-rw-r--r-- | luni/src/test/api/unix/org/apache/harmony/luni/tests/java/io/UnixFileTest.java | 70 |
1 files changed, 52 insertions, 18 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 4d549fd..179a79b 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 @@ -23,9 +23,11 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.concurrent.Callable; import tests.support.resource.Support_Resources; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import libcore.io.Libcore; @@ -101,6 +103,24 @@ public class UnixFileTest extends TestCase { } /** + * Asserts that {@code fn} completes without {@link AssertionFailedError failing} + * at least once when run up to {@code retries} times. + */ + private static void assertAtLeastOnce(int retries, Callable<Void> fn) throws Exception { + assertTrue(retries > 0); + for (int i = 0; i < retries; i++) { + try { + fn.call(); + break; + } catch (AssertionFailedError e) { + if (i == retries - 1) { + throw e; + } + } + } + } + + /** * @tests java.io.File#canExecute() * @since 1.6 */ @@ -136,16 +156,23 @@ public class UnixFileTest extends TestCase { * @since 1.6 */ public void test_getFreeSpace() throws Exception { - long fileSpace = getLinuxSpace(FREE_SPACE_NUM, testFile); - long dirSpace = getLinuxSpace(FREE_SPACE_NUM, testDir); - // in case we cannot fetch the value from command line - if (fileSpace > 0) { - assertEquals(fileSpace, testFile.getFreeSpace()); - } - - if (dirSpace > 0) { - assertEquals(dirSpace, testDir.getFreeSpace()); - } + assertAtLeastOnce(3, () -> { + long fileSpace = getLinuxSpace(FREE_SPACE_NUM, testFile); + // in case we cannot fetch the value from command line + if (fileSpace > 0) { + assertEquals(fileSpace, testFile.getFreeSpace()); + } + return null; + }); + + assertAtLeastOnce(3, () -> { + long dirSpace = getLinuxSpace(FREE_SPACE_NUM, testDir); + // in case we cannot fetch the value from command line + if (dirSpace > 0) { + assertEquals(dirSpace, testDir.getFreeSpace()); + } + return null; + }); } /** @@ -168,14 +195,21 @@ public class UnixFileTest extends TestCase { * @since 1.6 */ public void test_getUsableSpace() throws Exception { - long fileSpace = getLinuxSpace(USABLE_SPACE_NUM, testFile); - long dirSpace = getLinuxSpace(USABLE_SPACE_NUM, testDir); - if (fileSpace > 0) { - assertEquals(fileSpace, testFile.getUsableSpace()); - } - if (dirSpace > 0) { - assertEquals(dirSpace, testDir.getUsableSpace()); - } + assertAtLeastOnce(3, () -> { + long fileSpace = getLinuxSpace(USABLE_SPACE_NUM, testFile); + if (fileSpace > 0) { + assertEquals(fileSpace, testFile.getUsableSpace()); + } + return null; + }); + + assertAtLeastOnce(3, () -> { + long dirSpace = getLinuxSpace(USABLE_SPACE_NUM, testDir); + if (dirSpace > 0) { + assertEquals(dirSpace, testDir.getUsableSpace()); + } + return null; + }); } /** |