|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * @test |
| 26 | + * @bug 8314120 |
| 27 | + * @summary Sanity test for FileDescriptor.sync |
| 28 | + * @library /test/lib |
| 29 | + * @run main Sync |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.FileDescriptor; |
| 34 | +import java.io.FileOutputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.io.SyncFailedException; |
| 37 | + |
| 38 | +public class Sync { |
| 39 | + |
| 40 | + static final String TEST_DIR = System.getProperty("test.dir", "."); |
| 41 | + static final int TRIES = 10_000; |
| 42 | + |
| 43 | + public static void testWith(File file) throws Exception { |
| 44 | + try (FileOutputStream fos = new FileOutputStream(file)) { |
| 45 | + FileDescriptor fd = fos.getFD(); |
| 46 | + for (int t = 0; t < TRIES; t++) { |
| 47 | + fd.sync(); |
| 48 | + } |
| 49 | + } catch (SyncFailedException sfe) { |
| 50 | + // Can happen on some filesystems, print it in the log |
| 51 | + System.out.println("Sync failed (acceptable)"); |
| 52 | + sfe.printStackTrace(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + // Run on platform threads |
| 58 | + System.out.println("With platform threads"); |
| 59 | + run(); |
| 60 | + |
| 61 | + System.out.println("Complete"); |
| 62 | + } |
| 63 | + |
| 64 | + private static class AutoDelete implements AutoCloseable { |
| 65 | + private final File file; |
| 66 | + |
| 67 | + public AutoDelete(File file) { |
| 68 | + this.file = file; |
| 69 | + } |
| 70 | + |
| 71 | + public File file() { |
| 72 | + return file; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void close() throws Exception { |
| 77 | + file.delete(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void run() throws Exception { |
| 82 | + try (var w = new AutoDelete(new File(TEST_DIR, "FileDescriptorSync1"))) { |
| 83 | + testWith(w.file()); |
| 84 | + } |
| 85 | + |
| 86 | + try (var w = new AutoDelete(File.createTempFile("FileDescriptorSync2", "tmp"))) { |
| 87 | + testWith(w.file()); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments