test/java/io/FileOutputStream/AtomicAppend.java

Print this page
rev 3565 : 7022624: use try-with-resources in java.io tests
Reviewed-by: XXX


  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 {
  50                         FileOutputStream s = new FileOutputStream(file, true);
  51                         for (int j = 0; j < 1000; j++) {
  52                             s.write((int) 'x');
  53                             s.flush();
  54                         }
  55                         s.close();
  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 {


  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 {
  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                             }
  55                         }
  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 {