Path: blob/master/test/jdk/java/io/FileOutputStream/AtomicAppend.java
41152 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 663135226* @summary Check that appends are atomic27*/2829import java.io.File;30import java.io.FileOutputStream;31import java.util.concurrent.Executors;32import java.util.concurrent.ExecutorService;33import java.util.concurrent.TimeUnit;3435public class AtomicAppend {36// Before the fix for37// 6631352: Implement atomic append mode using FILE_APPEND_DATA (win)38// this would fail intermittently on windows39void test(String[] args) throws Throwable {40final int nThreads = 10;41final int writes = 1000;42final File file = new File("foo");43file.delete();44try {45final ExecutorService es = Executors.newFixedThreadPool(nThreads);46for (int i = 0; i < nThreads; i++)47es.execute(new Runnable() { public void run() {48try {49try (FileOutputStream s = new FileOutputStream(file, true)) {50for (int j = 0; j < 1000; j++) {51s.write((int) 'x');52s.flush();53}54}55} catch (Throwable t) { unexpected(t); }}});56es.shutdown();57es.awaitTermination(10L, TimeUnit.MINUTES);58equal(file.length(), (long) (nThreads * writes));59} finally {60file.delete();61}62}6364//--------------------- Infrastructure ---------------------------65volatile int passed = 0, failed = 0;66void pass() {passed++;}67void fail() {failed++; Thread.dumpStack();}68void fail(String msg) {System.err.println(msg); fail();}69void unexpected(Throwable t) {failed++; t.printStackTrace();}70void check(boolean cond) {if (cond) pass(); else fail();}71void equal(Object x, Object y) {72if (x == null ? y == null : x.equals(y)) pass();73else fail(x + " not equal to " + y);}74public static void main(String[] args) throws Throwable {75new AtomicAppend().instanceMain(args);76}77void instanceMain(String[] args) throws Throwable {78try {test(args);} catch (Throwable t) {unexpected(t);}79System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);80if (failed > 0) throw new AssertionError("Some tests failed");}81}828384