test/java/util/concurrent/FutureTask/Customized.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 2010, Oracle and/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  */


  53             doneCount.getAndIncrement();
  54             super.done();
  55         }
  56         protected void set(V v) {
  57             setCount.getAndIncrement();
  58             super.set(v);
  59         }
  60         protected void setException(Throwable t) {
  61             setExceptionCount.getAndIncrement();
  62             super.setException(t);
  63         }
  64         public boolean runAndReset() {
  65             return super.runAndReset();
  66         }
  67     }
  68 
  69     static <V> void checkReady(final FutureTask<V> task) {
  70         check(! task.isDone());
  71         check(! task.isCancelled());
  72         THROWS(TimeoutException.class,
  73                new Fun(){void f() throws Throwable {
  74                        task.get(0L, TimeUnit.SECONDS); }});
  75     }
  76 
  77     static <V> void checkDone(final FutureTask<V> task) {
  78         try {
  79             check(task.isDone());
  80             check(! task.isCancelled());
  81             check(task.get() != null);
  82         } catch (Throwable t) { unexpected(t); }
  83     }
  84 
  85     static <V> void checkCancelled(final FutureTask<V> task) {
  86         check(task.isDone());
  87         check(task.isCancelled());
  88         THROWS(CancellationException.class,
  89            new Fun(){void f() throws Throwable {
  90                task.get(0L, TimeUnit.SECONDS); }},
  91            new Fun(){void f() throws Throwable {
  92                task.get(); }});
  93     }
  94 
  95     static <V> void checkThrew(final FutureTask<V> task) {
  96         check(task.isDone());
  97         check(! task.isCancelled());
  98         THROWS(ExecutionException.class,
  99            new Fun(){void f() throws Throwable {
 100                task.get(0L, TimeUnit.SECONDS); }},
 101            new Fun(){void f() throws Throwable {
 102                task.get(); }});
 103     }
 104 
 105     static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
 106         task.cancel(mayInterruptIfRunning);
 107         checkCancelled(task);
 108     }
 109 
 110     static <V> void run(FutureTask<V> task) {
 111         boolean isCancelled = task.isCancelled();
 112         task.run();
 113         check(task.isDone());
 114         equal(isCancelled, task.isCancelled());
 115     }
 116 
 117     static void realMain(String[] args) throws Throwable {
 118         final Runnable nop = new Runnable() {
 119                 public void run() {}};
 120         final Runnable bad = new Runnable() {
 121                 public void run() { throw new Error(); }};
 122 


 186 
 187         System.out.printf("doneCount=%d%n", doneCount.get());
 188         System.out.printf("setCount=%d%n", setCount.get());
 189         System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
 190     }
 191 
 192     //--------------------- Infrastructure ---------------------------
 193     static volatile int passed = 0, failed = 0;
 194     static void pass() {passed++;}
 195     static void fail() {failed++; Thread.dumpStack();}
 196     static void fail(String msg) {System.out.println(msg); fail();}
 197     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 198     static void check(boolean cond) {if (cond) pass(); else fail();}
 199     static void equal(Object x, Object y) {
 200         if (x == null ? y == null : x.equals(y)) pass();
 201         else fail(x + " not equal to " + y);}
 202     public static void main(String[] args) throws Throwable {
 203         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 204         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 205         if (failed > 0) throw new AssertionError("Some tests failed");}
 206     private abstract static class Fun {abstract void f() throws Throwable;}
 207     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
 208         for (Fun f : fs)
 209             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
 210             catch (Throwable t) {
 211                 if (k.isAssignableFrom(t.getClass())) pass();
 212                 else unexpected(t);}}
 213 }
   1 /*
   2  * Copyright (c) 2007, 2014, Oracle and/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  */


  53             doneCount.getAndIncrement();
  54             super.done();
  55         }
  56         protected void set(V v) {
  57             setCount.getAndIncrement();
  58             super.set(v);
  59         }
  60         protected void setException(Throwable t) {
  61             setExceptionCount.getAndIncrement();
  62             super.setException(t);
  63         }
  64         public boolean runAndReset() {
  65             return super.runAndReset();
  66         }
  67     }
  68 
  69     static <V> void checkReady(final FutureTask<V> task) {
  70         check(! task.isDone());
  71         check(! task.isCancelled());
  72         THROWS(TimeoutException.class,
  73                () -> task.get(0L, TimeUnit.SECONDS));

  74     }
  75 
  76     static <V> void checkDone(final FutureTask<V> task) {
  77         try {
  78             check(task.isDone());
  79             check(! task.isCancelled());
  80             check(task.get() != null);
  81         } catch (Throwable t) { unexpected(t); }
  82     }
  83 
  84     static <V> void checkCancelled(final FutureTask<V> task) {
  85         check(task.isDone());
  86         check(task.isCancelled());
  87         THROWS(CancellationException.class,
  88                () -> task.get(0L, TimeUnit.SECONDS),
  89                () -> task.get());


  90     }
  91 
  92     static <V> void checkThrew(final FutureTask<V> task) {
  93         check(task.isDone());
  94         check(! task.isCancelled());
  95         THROWS(ExecutionException.class,
  96                () -> task.get(0L, TimeUnit.SECONDS),
  97                () -> task.get());


  98     }
  99 
 100     static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
 101         task.cancel(mayInterruptIfRunning);
 102         checkCancelled(task);
 103     }
 104 
 105     static <V> void run(FutureTask<V> task) {
 106         boolean isCancelled = task.isCancelled();
 107         task.run();
 108         check(task.isDone());
 109         equal(isCancelled, task.isCancelled());
 110     }
 111 
 112     static void realMain(String[] args) throws Throwable {
 113         final Runnable nop = new Runnable() {
 114                 public void run() {}};
 115         final Runnable bad = new Runnable() {
 116                 public void run() { throw new Error(); }};
 117 


 181 
 182         System.out.printf("doneCount=%d%n", doneCount.get());
 183         System.out.printf("setCount=%d%n", setCount.get());
 184         System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
 185     }
 186 
 187     //--------------------- Infrastructure ---------------------------
 188     static volatile int passed = 0, failed = 0;
 189     static void pass() {passed++;}
 190     static void fail() {failed++; Thread.dumpStack();}
 191     static void fail(String msg) {System.out.println(msg); fail();}
 192     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 193     static void check(boolean cond) {if (cond) pass(); else fail();}
 194     static void equal(Object x, Object y) {
 195         if (x == null ? y == null : x.equals(y)) pass();
 196         else fail(x + " not equal to " + y);}
 197     public static void main(String[] args) throws Throwable {
 198         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 199         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 200         if (failed > 0) throw new AssertionError("Some tests failed");}
 201     @FunctionalInterface interface Fun {void f() throws Throwable;}
 202     static void THROWS(Class<? extends Throwable> k, Fun... fs) {
 203         for (Fun f : fs)
 204             try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
 205             catch (Throwable t) {
 206                 if (k.isAssignableFrom(t.getClass())) pass();
 207                 else unexpected(t);}}
 208 }