jdk/test/java/io/FileOutputStream/AtomicAppend.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 47 c8f0e41aea68
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 47
diff changeset
     2
 * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     4
 *
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     8
 *
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    13
 * accompanied this code).
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    14
 *
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    18
 *
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    21
 * have any questions.
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    22
 */
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    23
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    24
/*
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    25
 * @test
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    26
 * @bug 6631352
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    27
 * @summary Check that appends are atomic
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    28
 */
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    29
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    30
import java.io.File;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    31
import java.io.FileOutputStream;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    32
import java.util.concurrent.Executors;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    33
import java.util.concurrent.ExecutorService;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    34
import java.util.concurrent.TimeUnit;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    35
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    36
public class AtomicAppend {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    37
    // Before the fix for
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    38
    // 6631352: Implement atomic append mode using FILE_APPEND_DATA (win)
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    39
    // this would fail intermittently on windows
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    40
    void test(String[] args) throws Throwable {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    41
        final int nThreads = 10;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    42
        final int writes = 1000;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    43
        final File file = new File("foo");
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    44
        file.delete();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    45
        try {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    46
            final ExecutorService es = Executors.newFixedThreadPool(nThreads);
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    47
            for (int i = 0; i < nThreads; i++)
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    48
                es.execute(new Runnable() { public void run() {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    49
                    try {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    50
                        FileOutputStream s = new FileOutputStream(file, true);
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    51
                        for (int j = 0; j < 1000; j++) {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    52
                            s.write((int) 'x');
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    53
                            s.flush();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    54
                        }
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    55
                        s.close();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    56
                    } catch (Throwable t) { unexpected(t); }}});
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    57
            es.shutdown();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    58
            es.awaitTermination(10L, TimeUnit.MINUTES);
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    59
            equal(file.length(), (long) (nThreads * writes));
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    60
        } finally {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    61
            file.delete();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    62
        }
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    63
    }
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    64
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    65
    //--------------------- Infrastructure ---------------------------
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    66
    volatile int passed = 0, failed = 0;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    67
    void pass() {passed++;}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    68
    void fail() {failed++; Thread.dumpStack();}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    69
    void fail(String msg) {System.err.println(msg); fail();}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    70
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    71
    void check(boolean cond) {if (cond) pass(); else fail();}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    72
    void equal(Object x, Object y) {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    73
        if (x == null ? y == null : x.equals(y)) pass();
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    74
        else fail(x + " not equal to " + y);}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    75
    public static void main(String[] args) throws Throwable {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    76
        new AtomicAppend().instanceMain(args);}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    77
    void instanceMain(String[] args) throws Throwable {
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    78
        try {test(args);} catch (Throwable t) {unexpected(t);}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    79
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    80
        if (failed > 0) throw new AssertionError("Some tests failed");}
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents:
diff changeset
    81
}