martin | 3e71533 | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1 | /* |
ohair | bf91ea1 | 2011-04-06 22:06:11 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
martin | 3e71533 | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 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 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 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. |
martin | 3e71533 | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6631352 |
| 27 | * @summary Check that appends are atomic |
| 28 | */ |
| 29 | |
| 30 | import java.io.File; |
| 31 | import java.io.FileOutputStream; |
| 32 | import java.util.concurrent.Executors; |
| 33 | import java.util.concurrent.ExecutorService; |
| 34 | import java.util.concurrent.TimeUnit; |
| 35 | |
| 36 | public class AtomicAppend { |
| 37 | // Before the fix for |
| 38 | // 6631352: Implement atomic append mode using FILE_APPEND_DATA (win) |
| 39 | // this would fail intermittently on windows |
| 40 | void test(String[] args) throws Throwable { |
| 41 | final int nThreads = 10; |
| 42 | final int writes = 1000; |
| 43 | final File file = new File("foo"); |
| 44 | file.delete(); |
| 45 | try { |
| 46 | final ExecutorService es = Executors.newFixedThreadPool(nThreads); |
| 47 | for (int i = 0; i < nThreads; i++) |
| 48 | es.execute(new Runnable() { public void run() { |
| 49 | try { |
smarks | 40863b9 | 2011-03-01 15:05:32 -0800 | [diff] [blame] | 50 | try (FileOutputStream s = new FileOutputStream(file, true)) { |
| 51 | for (int j = 0; j < 1000; j++) { |
| 52 | s.write((int) 'x'); |
| 53 | s.flush(); |
| 54 | } |
martin | 3e71533 | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 55 | } |
martin | 3e71533 | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 56 | } catch (Throwable t) { unexpected(t); }}}); |
| 57 | es.shutdown(); |
| 58 | es.awaitTermination(10L, TimeUnit.MINUTES); |
| 59 | equal(file.length(), (long) (nThreads * writes)); |
| 60 | } finally { |
| 61 | file.delete(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | //--------------------- Infrastructure --------------------------- |
| 66 | volatile int passed = 0, failed = 0; |
| 67 | void pass() {passed++;} |
| 68 | void fail() {failed++; Thread.dumpStack();} |
| 69 | void fail(String msg) {System.err.println(msg); fail();} |
| 70 | void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 71 | void check(boolean cond) {if (cond) pass(); else fail();} |
| 72 | void equal(Object x, Object y) { |
| 73 | if (x == null ? y == null : x.equals(y)) pass(); |
| 74 | else fail(x + " not equal to " + y);} |
| 75 | public static void main(String[] args) throws Throwable { |
| 76 | new AtomicAppend().instanceMain(args);} |
| 77 | void instanceMain(String[] args) throws Throwable { |
| 78 | try {test(args);} catch (Throwable t) {unexpected(t);} |
| 79 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 80 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 81 | } |