undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6431315
|
||||
* @summary ExecutorService.invokeAll might hang
|
||||
* @author Martin Buchholz
|
||||
* @library /test/lib
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
/**
|
||||
* Adapted from Doug Lea, which was...
|
||||
* adapted from a posting by Tom Sugden tom at epcc.ed.ac.uk
|
||||
*/
|
||||
public class BlockingTaskExecutor {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
|
||||
static void realMain(String[] args) throws Throwable {
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
System.out.print(".");
|
||||
test();
|
||||
}
|
||||
}
|
||||
|
||||
static void test() throws Throwable {
|
||||
final ExecutorService executor = Executors.newCachedThreadPool();
|
||||
|
||||
final NotificationReceiver notifiee1 = new NotificationReceiver();
|
||||
final NotificationReceiver notifiee2 = new NotificationReceiver();
|
||||
|
||||
final Collection<Callable<Object>> tasks = new ArrayList<>();
|
||||
tasks.add(new BlockingTask(notifiee1));
|
||||
tasks.add(new BlockingTask(notifiee2));
|
||||
tasks.add(new NonBlockingTask());
|
||||
|
||||
// start a thread to invoke the tasks
|
||||
Thread thread = new Thread() { public void run() {
|
||||
try { executor.invokeAll(tasks); }
|
||||
catch (RejectedExecutionException t) {/* OK */}
|
||||
catch (Throwable t) { unexpected(t); }}};
|
||||
thread.start();
|
||||
|
||||
// Wait until tasks begin execution
|
||||
notifiee1.waitForNotification();
|
||||
notifiee2.waitForNotification();
|
||||
|
||||
// Now try to shutdown the executor service while tasks
|
||||
// are blocked. This should cause the tasks to be
|
||||
// interrupted.
|
||||
executor.shutdownNow();
|
||||
if (! executor.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
|
||||
throw new Error(
|
||||
String.format("Executor termination timed out after %d ms",
|
||||
LONG_DELAY_MS));
|
||||
|
||||
// Wait for the invocation thread to complete.
|
||||
thread.join(LONG_DELAY_MS);
|
||||
if (thread.isAlive()) {
|
||||
thread.interrupt();
|
||||
thread.join(LONG_DELAY_MS);
|
||||
throw new Error(
|
||||
String.format("invokeAll timed out after %d ms",
|
||||
LONG_DELAY_MS));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper class with a method to wait for a notification.
|
||||
*
|
||||
* The notification is received via the
|
||||
* {@code sendNotification} method.
|
||||
*/
|
||||
static class NotificationReceiver {
|
||||
/** Has the notifiee been notified? */
|
||||
boolean notified;
|
||||
|
||||
/**
|
||||
* Notify the notification receiver.
|
||||
*/
|
||||
public synchronized void sendNotification() {
|
||||
notified = true;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Waits until a notification has been received.
|
||||
*
|
||||
* @throws InterruptedException if the wait is interrupted
|
||||
*/
|
||||
public synchronized void waitForNotification()
|
||||
throws InterruptedException {
|
||||
while (! notified)
|
||||
wait();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A callable task that blocks until it is interrupted.
|
||||
* This task sends a notification to a notification receiver when
|
||||
* it is first called.
|
||||
*/
|
||||
static class BlockingTask implements Callable<Object> {
|
||||
private final NotificationReceiver notifiee;
|
||||
|
||||
BlockingTask(NotificationReceiver notifiee) {
|
||||
this.notifiee = notifiee;
|
||||
}
|
||||
|
||||
public Object call() throws InterruptedException {
|
||||
notifiee.sendNotification();
|
||||
|
||||
// wait indefinitely until task is interrupted
|
||||
while (true) {
|
||||
synchronized (this) {
|
||||
wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A callable task that simply returns a string result.
|
||||
*/
|
||||
static class NonBlockingTask implements Callable<Object> {
|
||||
public Object call() {
|
||||
return "NonBlockingTaskResult";
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4486658
|
||||
* @summary Checks for responsiveness of futures to cancellation.
|
||||
* Runs under the assumption that ITERS computations require more than
|
||||
* TIMEOUT msecs to complete.
|
||||
* @library /test/lib
|
||||
* @run main/timeout=2000 CancelledFutureLoops
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.BrokenBarrierException;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CyclicBarrier;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
public final class CancelledFutureLoops {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
static final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
static final SplittableRandom rnd = new SplittableRandom();
|
||||
static boolean print = false;
|
||||
static final int ITERS = 1000000;
|
||||
static final long TIMEOUT = 100;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
int maxThreads = 5;
|
||||
if (args.length > 0)
|
||||
maxThreads = Integer.parseInt(args[0]);
|
||||
|
||||
print = true;
|
||||
|
||||
for (int i = 2; i <= maxThreads; i += (i+1) >>> 1) {
|
||||
System.out.print("Threads: " + i);
|
||||
try {
|
||||
new FutureLoop(i, rnd.split()).test();
|
||||
}
|
||||
catch (BrokenBarrierException bb) {
|
||||
// OK; ignore
|
||||
}
|
||||
catch (ExecutionException ee) {
|
||||
// OK; ignore
|
||||
}
|
||||
Thread.sleep(TIMEOUT);
|
||||
}
|
||||
pool.shutdown();
|
||||
if (! pool.awaitTermination(6 * LONG_DELAY_MS, MILLISECONDS))
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
static final class FutureLoop implements Callable {
|
||||
private final int nthreads;
|
||||
private final SplittableRandom rnd;
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
|
||||
private final CyclicBarrier barrier;
|
||||
private int v;
|
||||
FutureLoop(int nthreads, SplittableRandom rnd) {
|
||||
this.nthreads = nthreads;
|
||||
this.rnd = rnd;
|
||||
barrier = new CyclicBarrier(nthreads+1, timer);
|
||||
v = rnd.nextInt();
|
||||
}
|
||||
|
||||
final void test() throws Exception {
|
||||
Future[] futures = new Future[nthreads];
|
||||
for (int i = 0; i < nthreads; ++i)
|
||||
futures[i] = pool.submit(this);
|
||||
|
||||
barrier.await();
|
||||
Thread.sleep(TIMEOUT);
|
||||
boolean tooLate = false;
|
||||
for (int i = 1; i < nthreads; ++i) {
|
||||
if (!futures[i].cancel(true))
|
||||
tooLate = true;
|
||||
// Unbunch some of the cancels
|
||||
if ( (i & 3) == 0)
|
||||
Thread.sleep(1 + rnd.nextInt(5));
|
||||
}
|
||||
|
||||
Object f0 = futures[0].get();
|
||||
if (!tooLate) {
|
||||
for (int i = 1; i < nthreads; ++i) {
|
||||
if (!futures[i].isDone() || !futures[i].isCancelled())
|
||||
throw new Error("Only one thread should complete");
|
||||
}
|
||||
}
|
||||
else
|
||||
System.out.print("(cancelled too late) ");
|
||||
|
||||
long endTime = System.nanoTime();
|
||||
long time = endTime - timer.startTime;
|
||||
if (print) {
|
||||
double secs = (double)time / 1000000000.0;
|
||||
System.out.println("\t " + secs + "s run time");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final Object call() throws Exception {
|
||||
barrier.await();
|
||||
int sum = v;
|
||||
int x = 0;
|
||||
int n = ITERS;
|
||||
while (n-- > 0) {
|
||||
lock.lockInterruptibly();
|
||||
try {
|
||||
v = x = LoopHelpers.compute1(v);
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
sum += LoopHelpers.compute2(LoopHelpers.compute2(x));
|
||||
}
|
||||
return new Integer(sum);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
211
test/jdk/java/util/concurrent/FutureTask/Customized.java
Normal file
211
test/jdk/java/util/concurrent/FutureTask/Customized.java
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6464365
|
||||
* @summary Test state transitions; check protected methods are called
|
||||
* @author Martin Buchholz
|
||||
*/
|
||||
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public class Customized {
|
||||
static final AtomicLong doneCount = new AtomicLong(0);
|
||||
static final AtomicLong setCount = new AtomicLong(0);
|
||||
static final AtomicLong setExceptionCount = new AtomicLong(0);
|
||||
|
||||
static void equal(long expected, AtomicLong actual) {
|
||||
equal(expected, actual.get());
|
||||
}
|
||||
|
||||
static void equalCounts(long x, long y, long z) {
|
||||
equal(x, doneCount);
|
||||
equal(y, setCount);
|
||||
equal(z, setExceptionCount);
|
||||
}
|
||||
|
||||
static class MyFutureTask<V> extends FutureTask<V> {
|
||||
MyFutureTask(Runnable r, V result) { super(r, result); }
|
||||
protected void done() {
|
||||
doneCount.getAndIncrement();
|
||||
super.done();
|
||||
}
|
||||
protected void set(V v) {
|
||||
setCount.getAndIncrement();
|
||||
super.set(v);
|
||||
}
|
||||
protected void setException(Throwable t) {
|
||||
setExceptionCount.getAndIncrement();
|
||||
super.setException(t);
|
||||
}
|
||||
public boolean runAndReset() {
|
||||
return super.runAndReset();
|
||||
}
|
||||
}
|
||||
|
||||
static <V> void checkReady(final FutureTask<V> task) {
|
||||
check(! task.isDone());
|
||||
check(! task.isCancelled());
|
||||
THROWS(TimeoutException.class,
|
||||
() -> task.get(0L, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
static <V> void checkDone(final FutureTask<V> task) {
|
||||
try {
|
||||
check(task.isDone());
|
||||
check(! task.isCancelled());
|
||||
check(task.get() != null);
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
|
||||
static <V> void checkCancelled(final FutureTask<V> task) {
|
||||
check(task.isDone());
|
||||
check(task.isCancelled());
|
||||
THROWS(CancellationException.class,
|
||||
() -> task.get(0L, TimeUnit.SECONDS),
|
||||
() -> task.get());
|
||||
}
|
||||
|
||||
static <V> void checkThrew(final FutureTask<V> task) {
|
||||
check(task.isDone());
|
||||
check(! task.isCancelled());
|
||||
THROWS(ExecutionException.class,
|
||||
() -> task.get(0L, TimeUnit.SECONDS),
|
||||
() -> task.get());
|
||||
}
|
||||
|
||||
static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) {
|
||||
task.cancel(mayInterruptIfRunning);
|
||||
checkCancelled(task);
|
||||
}
|
||||
|
||||
static <V> void run(FutureTask<V> task) {
|
||||
boolean isCancelled = task.isCancelled();
|
||||
task.run();
|
||||
check(task.isDone());
|
||||
equal(isCancelled, task.isCancelled());
|
||||
}
|
||||
|
||||
static void realMain(String[] args) throws Throwable {
|
||||
final Runnable nop = new Runnable() {
|
||||
public void run() {}};
|
||||
final Runnable bad = new Runnable() {
|
||||
public void run() { throw new Error(); }};
|
||||
|
||||
try {
|
||||
final MyFutureTask<Long> task = new MyFutureTask<>(nop, 42L);
|
||||
checkReady(task);
|
||||
equalCounts(0,0,0);
|
||||
check(task.runAndReset());
|
||||
checkReady(task);
|
||||
equalCounts(0,0,0);
|
||||
run(task);
|
||||
checkDone(task);
|
||||
equalCounts(1,1,0);
|
||||
equal(42L, task.get());
|
||||
run(task);
|
||||
checkDone(task);
|
||||
equalCounts(1,1,0);
|
||||
equal(42L, task.get());
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try {
|
||||
final MyFutureTask<Long> task = new MyFutureTask<>(nop, 42L);
|
||||
cancel(task, false);
|
||||
equalCounts(2,1,0);
|
||||
cancel(task, false);
|
||||
equalCounts(2,1,0);
|
||||
run(task);
|
||||
equalCounts(2,1,0);
|
||||
check(! task.runAndReset());
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try {
|
||||
final MyFutureTask<Long> task = new MyFutureTask<>(bad, 42L);
|
||||
checkReady(task);
|
||||
run(task);
|
||||
checkThrew(task);
|
||||
equalCounts(3,1,1);
|
||||
run(task);
|
||||
equalCounts(3,1,1);
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try {
|
||||
final MyFutureTask<Long> task = new MyFutureTask<>(nop, 42L);
|
||||
checkReady(task);
|
||||
task.set(99L);
|
||||
checkDone(task);
|
||||
equalCounts(4,2,1);
|
||||
run(task);
|
||||
equalCounts(4,2,1);
|
||||
task.setException(new Throwable());
|
||||
checkDone(task);
|
||||
equalCounts(4,2,2);
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
try {
|
||||
final MyFutureTask<Long> task = new MyFutureTask<>(nop, 42L);
|
||||
checkReady(task);
|
||||
task.setException(new Throwable());
|
||||
checkThrew(task);
|
||||
equalCounts(5,2,3);
|
||||
run(task);
|
||||
equalCounts(5,2,3);
|
||||
task.set(99L);
|
||||
checkThrew(task);
|
||||
equalCounts(5,3,3);
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
System.out.printf("doneCount=%d%n", doneCount.get());
|
||||
System.out.printf("setCount=%d%n", setCount.get());
|
||||
System.out.printf("setExceptionCount=%d%n", setExceptionCount.get());
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
interface Fun {void f() throws Throwable;}
|
||||
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
|
||||
for (Fun f : fs)
|
||||
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
|
||||
catch (Throwable t) {
|
||||
if (k.isAssignableFrom(t.getClass())) pass();
|
||||
else unexpected(t);}}
|
||||
}
|
||||
106
test/jdk/java/util/concurrent/FutureTask/DoneMeansDone.java
Normal file
106
test/jdk/java/util/concurrent/FutureTask/DoneMeansDone.java
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Martin Buchholz with assistance from members of JCP
|
||||
* JSR-166 Expert Group and released to the public domain, as
|
||||
* explained at http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8073704
|
||||
* @summary Checks that once isDone() returns true,
|
||||
* get() never throws InterruptedException or TimeoutException
|
||||
* @library /test/lib
|
||||
*/
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
public class DoneMeansDone {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
final int iters = 1000;
|
||||
final int nThreads = 2;
|
||||
final AtomicBoolean done = new AtomicBoolean(false);
|
||||
final AtomicReference<FutureTask<Boolean>> a = new AtomicReference<>();
|
||||
final CountDownLatch threadsStarted = new CountDownLatch(nThreads);
|
||||
final Callable<Boolean> alwaysTrue = new Callable<>() {
|
||||
public Boolean call() {
|
||||
return true;
|
||||
}};
|
||||
|
||||
final Runnable observer = new Runnable() { public void run() {
|
||||
threadsStarted.countDown();
|
||||
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
|
||||
try {
|
||||
for (FutureTask<Boolean> f; !done.get();) {
|
||||
f = a.get();
|
||||
if (f != null) {
|
||||
do {} while (!f.isDone());
|
||||
Thread.currentThread().interrupt();
|
||||
if (!(rnd.nextBoolean()
|
||||
? f.get()
|
||||
: f.get(-1L, TimeUnit.DAYS)))
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
} catch (Exception t) { throw new AssertionError(t); }
|
||||
}};
|
||||
|
||||
final ArrayList<Future<?>> futures = new ArrayList<>(nThreads);
|
||||
final ExecutorService pool = Executors.newCachedThreadPool();
|
||||
for (int i = 0; i < nThreads; i++)
|
||||
futures.add(pool.submit(observer));
|
||||
threadsStarted.await();
|
||||
for (int i = 0; i < iters; i++) {
|
||||
FutureTask<Boolean> f = new FutureTask<>(alwaysTrue);
|
||||
a.set(f);
|
||||
f.run();
|
||||
}
|
||||
done.set(true);
|
||||
pool.shutdown();
|
||||
if (!pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS))
|
||||
throw new AssertionError();
|
||||
for (Future<?> future : futures)
|
||||
future.get();
|
||||
}
|
||||
}
|
||||
168
test/jdk/java/util/concurrent/FutureTask/DoneTimedGetLoops.java
Normal file
168
test/jdk/java/util/concurrent/FutureTask/DoneTimedGetLoops.java
Normal file
|
|
@ -0,0 +1,168 @@
|
|||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Martin Buchholz with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @library /test/lib
|
||||
* @run main DoneTimedGetLoops 300
|
||||
* @summary isDone returning true guarantees that subsequent timed get
|
||||
* will never throw TimeoutException.
|
||||
*/
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import jdk.test.lib.Utils;
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
|
||||
public class DoneTimedGetLoops {
|
||||
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000);
|
||||
final long testDurationMillisDefault = 10_000L;
|
||||
final long testDurationMillis;
|
||||
|
||||
static class PublicFutureTask extends FutureTask<Boolean> {
|
||||
static final Runnable noop = new Runnable() { public void run() {} };
|
||||
PublicFutureTask() { super(noop, null); }
|
||||
public void set(Boolean v) { super.set(v); }
|
||||
public void setException(Throwable t) { super.setException(t); }
|
||||
}
|
||||
|
||||
DoneTimedGetLoops(String[] args) {
|
||||
testDurationMillis = (args.length > 0) ?
|
||||
Long.valueOf(args[0]) : testDurationMillisDefault;
|
||||
}
|
||||
|
||||
void test(String[] args) throws Throwable {
|
||||
final long testDurationNanos = testDurationMillis * 1000L * 1000L;
|
||||
final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
|
||||
|
||||
final AtomicReference<PublicFutureTask> normalRef
|
||||
= new AtomicReference<>();
|
||||
final AtomicReference<PublicFutureTask> abnormalRef
|
||||
= new AtomicReference<>();
|
||||
|
||||
final Throwable throwable = new Throwable();
|
||||
|
||||
abstract class CheckedThread extends Thread {
|
||||
CheckedThread(String name) {
|
||||
super(name);
|
||||
setDaemon(true);
|
||||
start();
|
||||
}
|
||||
/** Polls for quitting time. */
|
||||
protected boolean quittingTime() {
|
||||
return System.nanoTime() - quittingTimeNanos > 0;
|
||||
}
|
||||
/** Polls occasionally for quitting time. */
|
||||
protected boolean quittingTime(long i) {
|
||||
return (i % 1024) == 0 && quittingTime();
|
||||
}
|
||||
protected abstract void realRun() throws Exception;
|
||||
public void run() {
|
||||
try { realRun(); } catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
}
|
||||
|
||||
Thread setter = new CheckedThread("setter") {
|
||||
protected void realRun() {
|
||||
while (! quittingTime()) {
|
||||
PublicFutureTask future = new PublicFutureTask();
|
||||
normalRef.set(future);
|
||||
future.set(Boolean.TRUE);
|
||||
}}};
|
||||
|
||||
Thread setterException = new CheckedThread("setterException") {
|
||||
protected void realRun() {
|
||||
while (! quittingTime()) {
|
||||
PublicFutureTask future = new PublicFutureTask();
|
||||
abnormalRef.set(future);
|
||||
future.setException(throwable);
|
||||
}}};
|
||||
|
||||
Thread doneTimedGetNormal = new CheckedThread("doneTimedGetNormal") {
|
||||
protected void realRun() throws Exception {
|
||||
while (! quittingTime()) {
|
||||
PublicFutureTask future = normalRef.get();
|
||||
if (future != null) {
|
||||
while (!future.isDone())
|
||||
;
|
||||
check(future.get(0L, TimeUnit.HOURS) == Boolean.TRUE);
|
||||
}}}};
|
||||
|
||||
Thread doneTimedGetAbnormal = new CheckedThread("doneTimedGetAbnormal") {
|
||||
protected void realRun() throws Exception {
|
||||
while (! quittingTime()) {
|
||||
PublicFutureTask future = abnormalRef.get();
|
||||
if (future != null) {
|
||||
while (!future.isDone())
|
||||
;
|
||||
try { future.get(0L, TimeUnit.HOURS); fail(); }
|
||||
catch (ExecutionException t) {
|
||||
check(t.getCause() == throwable);
|
||||
}
|
||||
}}}};
|
||||
|
||||
for (Thread thread : new Thread[] {
|
||||
setter,
|
||||
setterException,
|
||||
doneTimedGetNormal,
|
||||
doneTimedGetAbnormal }) {
|
||||
thread.join(LONG_DELAY_MS + testDurationMillis);
|
||||
if (thread.isAlive()) {
|
||||
System.err.printf("Hung thread: %s%n", thread.getName());
|
||||
failed++;
|
||||
for (StackTraceElement e : thread.getStackTrace())
|
||||
System.err.println(e);
|
||||
thread.join(LONG_DELAY_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new DoneTimedGetLoops(args).instanceMain(args);}
|
||||
public void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
87
test/jdk/java/util/concurrent/FutureTask/ExplicitSet.java
Normal file
87
test/jdk/java/util/concurrent/FutureTask/ExplicitSet.java
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 7132378
|
||||
* @summary Race in FutureTask if used with explicit set ( not Runnable )
|
||||
* @author Chris Hegarty
|
||||
*/
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class ExplicitSet {
|
||||
|
||||
static void realMain(String[] args) throws Throwable {
|
||||
for (int i = 1; i <= 10000; i++) {
|
||||
//System.out.print(".");
|
||||
test();
|
||||
}
|
||||
}
|
||||
|
||||
static void test() throws Throwable {
|
||||
final SettableTask task = new SettableTask();
|
||||
|
||||
Thread thread = new Thread() { public void run() {
|
||||
try {
|
||||
check(task.get() != null);
|
||||
} catch (Exception e) { unexpected(e); }
|
||||
}};
|
||||
thread.start();
|
||||
|
||||
task.set(Boolean.TRUE);
|
||||
thread.join(5000);
|
||||
}
|
||||
|
||||
static class SettableTask extends FutureTask<Boolean> {
|
||||
SettableTask() {
|
||||
super(new Callable<Boolean>() {
|
||||
public Boolean call() {
|
||||
fail("The task should never be run!");
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(Boolean b) {
|
||||
super.set(b);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
static volatile int passed = 0, failed = 0;
|
||||
static void pass() {passed++;}
|
||||
static void fail() {failed++; Thread.dumpStack();}
|
||||
static void fail(String msg) {System.out.println(msg); fail();}
|
||||
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
static void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
static void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
}
|
||||
103
test/jdk/java/util/concurrent/FutureTask/LoopHelpers.java
Normal file
103
test/jdk/java/util/concurrent/FutureTask/LoopHelpers.java
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is available under and governed by the GNU General Public
|
||||
* License version 2 only, as published by the Free Software Foundation.
|
||||
* However, the following notice accompanied the original version of this
|
||||
* file:
|
||||
*
|
||||
* Written by Doug Lea with assistance from members of JCP JSR-166
|
||||
* Expert Group and released to the public domain, as explained at
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/
|
||||
*/
|
||||
|
||||
/**
|
||||
* Misc utilities in JSR166 performance tests
|
||||
*/
|
||||
class LoopHelpers {
|
||||
|
||||
// Some mindless computation to do between synchronizations...
|
||||
|
||||
/**
|
||||
* generates 32 bit pseudo-random numbers.
|
||||
* Adapted from http://www.snippets.org
|
||||
*/
|
||||
public static int compute1(int x) {
|
||||
int lo = 16807 * (x & 0xFFFF);
|
||||
int hi = 16807 * (x >>> 16);
|
||||
lo += (hi & 0x7FFF) << 16;
|
||||
if ((lo & 0x80000000) != 0) {
|
||||
lo &= 0x7fffffff;
|
||||
++lo;
|
||||
}
|
||||
lo += hi >>> 15;
|
||||
if (lo == 0 || (lo & 0x80000000) != 0) {
|
||||
lo &= 0x7fffffff;
|
||||
++lo;
|
||||
}
|
||||
return lo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a linear congruential random number a random number
|
||||
* of times.
|
||||
*/
|
||||
public static int compute2(int x) {
|
||||
int loops = (x >>> 4) & 7;
|
||||
while (loops-- > 0) {
|
||||
x = (x * 2147483647) % 16807;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
public static class BarrierTimer implements Runnable {
|
||||
public volatile long startTime;
|
||||
public volatile long endTime;
|
||||
public void run() {
|
||||
long t = System.nanoTime();
|
||||
if (startTime == 0)
|
||||
startTime = t;
|
||||
else
|
||||
endTime = t;
|
||||
}
|
||||
public void clear() {
|
||||
startTime = 0;
|
||||
endTime = 0;
|
||||
}
|
||||
public long getTime() {
|
||||
return endTime - startTime;
|
||||
}
|
||||
}
|
||||
|
||||
public static String rightJustify(long n) {
|
||||
// There's probably a better way to do this...
|
||||
String field = " ";
|
||||
String num = Long.toString(n);
|
||||
if (num.length() >= field.length())
|
||||
return num;
|
||||
StringBuilder b = new StringBuilder(field);
|
||||
b.replace(b.length()-num.length(), b.length(), num);
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8060052
|
||||
* @summary FutureTask; fix underflow when timeout = Long.MIN_VALUE
|
||||
* @author Chris Hegarty
|
||||
*/
|
||||
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// If the bug exists the test will eventually be interrupted by the
|
||||
// test harness and fail with an InterruptedException, otherwise it
|
||||
// will throw a TimeoutException almost immediately and return silently.
|
||||
|
||||
public class NegativeTimeout {
|
||||
public static void main(String[] args) throws Exception {
|
||||
FutureTask<Void> task = new FutureTask<>(() -> null);
|
||||
try {
|
||||
task.get(Long.MIN_VALUE, TimeUnit.NANOSECONDS);
|
||||
} catch (TimeoutException success) {}
|
||||
}
|
||||
}
|
||||
147
test/jdk/java/util/concurrent/FutureTask/Throw.java
Normal file
147
test/jdk/java/util/concurrent/FutureTask/Throw.java
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6415572
|
||||
* @summary Check exceptional behavior in run and done methods
|
||||
*/
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class Throw {
|
||||
|
||||
static void THROW(final Throwable t) {
|
||||
if (t != null)
|
||||
Throw.<RuntimeException>uncheckedThrow(t);
|
||||
}
|
||||
|
||||
Callable<Void> thrower(final Throwable t) {
|
||||
return new Callable<Void>() { public Void call() {
|
||||
THROW(t); return null; }};
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class DoneError extends Error {}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class DoneException extends RuntimeException {}
|
||||
|
||||
static class MyFutureTask extends FutureTask<Void> {
|
||||
MyFutureTask(Callable<Void> task) { super(task); }
|
||||
public boolean runAndReset() { return super.runAndReset(); }
|
||||
}
|
||||
|
||||
MyFutureTask checkTask(final MyFutureTask task) {
|
||||
check(! task.isCancelled());
|
||||
check(! task.isDone());
|
||||
return task;
|
||||
}
|
||||
|
||||
MyFutureTask taskFor(final Throwable callableThrowable,
|
||||
final Throwable doneThrowable) {
|
||||
return checkTask(
|
||||
new MyFutureTask(thrower(callableThrowable)) {
|
||||
protected void done() { THROW(doneThrowable); }});
|
||||
}
|
||||
|
||||
void test(String[] args) throws Throwable {
|
||||
final Throwable[] callableThrowables = {
|
||||
null, new Exception(), new Error(), new RuntimeException() };
|
||||
final Throwable[] doneThrowables = {
|
||||
new DoneError(), new DoneException() };
|
||||
for (final Throwable c : callableThrowables) {
|
||||
for (final Throwable d : doneThrowables) {
|
||||
THROWS(d.getClass(),
|
||||
new F(){void f(){
|
||||
taskFor(c, d).cancel(false);}},
|
||||
new F(){void f(){
|
||||
taskFor(c, d).run();}});
|
||||
if (c != null)
|
||||
THROWS(d.getClass(),
|
||||
new F(){void f(){
|
||||
taskFor(c, d).runAndReset();}});
|
||||
}
|
||||
|
||||
try {
|
||||
final MyFutureTask task = taskFor(c, null);
|
||||
check(task.cancel(false));
|
||||
THROWS(CancellationException.class,
|
||||
new F(){void f() throws Throwable { task.get(); }});
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
|
||||
if (c != null) {
|
||||
final MyFutureTask task = taskFor(c, null);
|
||||
task.run();
|
||||
try {
|
||||
task.get();
|
||||
fail("Expected ExecutionException");
|
||||
} catch (ExecutionException ee) {
|
||||
equal(c.getClass(), ee.getCause().getClass());
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
|
||||
if (c != null) {
|
||||
final MyFutureTask task = taskFor(c, null);
|
||||
task.runAndReset();
|
||||
try {
|
||||
task.get();
|
||||
fail("Expected ExecutionException");
|
||||
} catch (ExecutionException ee) {
|
||||
check(c.getClass().isInstance(ee.getCause()));
|
||||
} catch (Throwable t) { unexpected(t); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------- Infrastructure ---------------------------
|
||||
volatile int passed = 0, failed = 0;
|
||||
void pass() {passed++;}
|
||||
void fail() {failed++; Thread.dumpStack();}
|
||||
void fail(String msg) {System.err.println(msg); fail();}
|
||||
void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
||||
void check(boolean cond) {if (cond) pass(); else fail();}
|
||||
void equal(Object x, Object y) {
|
||||
if (x == null ? y == null : x.equals(y)) pass();
|
||||
else fail(x + " not equal to " + y);}
|
||||
public static void main(String[] args) throws Throwable {
|
||||
new Throw().instanceMain(args);}
|
||||
void instanceMain(String[] args) throws Throwable {
|
||||
try {test(args);} catch (Throwable t) {unexpected(t);}
|
||||
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
||||
if (failed > 0) throw new AssertionError("Some tests failed");}
|
||||
abstract class F {abstract void f() throws Throwable;}
|
||||
void THROWS(Class<? extends Throwable> k, F... fs) {
|
||||
for (F f : fs)
|
||||
try {f.f(); fail("Expected " + k.getName() + " not thrown");}
|
||||
catch (Throwable t) {
|
||||
if (k.isAssignableFrom(t.getClass())) pass();
|
||||
else unexpected(t);}}
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T extends Throwable> void uncheckedThrow(Throwable t) throws T {
|
||||
throw (T)t; // rely on vacuous cast
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue