6144N/A/*
6144N/A * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
6144N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6144N/A *
6144N/A * This code is free software; you can redistribute it and/or modify it
6144N/A * under the terms of the GNU General Public License version 2 only, as
6144N/A * published by the Free Software Foundation.
6144N/A *
6144N/A * This code is distributed in the hope that it will be useful, but WITHOUT
6144N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6144N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
6144N/A * version 2 for more details (a copy is included in the LICENSE file that
6144N/A * accompanied this code).
6144N/A *
6144N/A * You should have received a copy of the GNU General Public License version
6144N/A * 2 along with this work; if not, write to the Free Software Foundation,
6144N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6144N/A *
6144N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6144N/A * or visit www.oracle.com if you need additional information or have any
6144N/A * questions.
6144N/A */
6144N/A
6144N/A/*
6144N/A * Written by Martin Buchholz with assistance from members of JCP JSR-166
6144N/A * Expert Group and released to the public domain, as explained at
6144N/A * http://creativecommons.org/publicdomain/zero/1.0/
6144N/A */
6144N/A
6144N/A/*
6144N/A * @test
6144N/A * @run main DoneTimedGetLoops 300
6144N/A * @summary isDone returning true guarantees that subsequent timed get
6144N/A * will never throw TimeoutException.
6144N/A */
6144N/A
6144N/Aimport java.util.*;
6144N/Aimport java.util.concurrent.*;
6144N/Aimport java.util.concurrent.atomic.*;
6144N/A
6144N/A@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
6144N/Apublic class DoneTimedGetLoops {
6144N/A final long testDurationMillisDefault = 10L * 1000L;
6144N/A final long testDurationMillis;
6144N/A
6144N/A static class PublicFutureTask extends FutureTask<Boolean> {
6144N/A final static Runnable noop = new Runnable() { public void run() {} };
6144N/A PublicFutureTask() { super(noop, null); }
6144N/A public void set(Boolean v) { super.set(v); }
6144N/A public void setException(Throwable t) { super.setException(t); }
6144N/A }
6144N/A
6144N/A DoneTimedGetLoops(String[] args) {
6144N/A testDurationMillis = (args.length > 0) ?
6144N/A Long.valueOf(args[0]) : testDurationMillisDefault;
6144N/A }
6144N/A
6144N/A void test(String[] args) throws Throwable {
6144N/A final long testDurationNanos = testDurationMillis * 1000L * 1000L;
6144N/A final long quittingTimeNanos = System.nanoTime() + testDurationNanos;
6144N/A final long timeoutMillis = 10L * 1000L;
6144N/A
6144N/A final AtomicReference<PublicFutureTask> normalRef
6144N/A = new AtomicReference<PublicFutureTask>();
6144N/A final AtomicReference<PublicFutureTask> abnormalRef
6144N/A = new AtomicReference<PublicFutureTask>();
6144N/A
6144N/A final Throwable throwable = new Throwable();
6144N/A
6144N/A abstract class CheckedThread extends Thread {
6144N/A CheckedThread(String name) {
6144N/A super(name);
6144N/A setDaemon(true);
6144N/A start();
6144N/A }
6144N/A /** Polls for quitting time. */
6144N/A protected boolean quittingTime() {
6144N/A return System.nanoTime() - quittingTimeNanos > 0;
6144N/A }
6144N/A /** Polls occasionally for quitting time. */
6144N/A protected boolean quittingTime(long i) {
6144N/A return (i % 1024) == 0 && quittingTime();
6144N/A }
6144N/A abstract protected void realRun() throws Exception;
6144N/A public void run() {
6144N/A try { realRun(); } catch (Throwable t) { unexpected(t); }
6144N/A }
6144N/A }
6144N/A
6144N/A Thread setter = new CheckedThread("setter") {
6144N/A protected void realRun() {
6144N/A while (! quittingTime()) {
6144N/A PublicFutureTask future = new PublicFutureTask();
6144N/A normalRef.set(future);
6144N/A future.set(Boolean.TRUE);
6144N/A }}};
6144N/A
6144N/A Thread setterException = new CheckedThread("setterException") {
6144N/A protected void realRun() {
6144N/A while (! quittingTime()) {
6144N/A PublicFutureTask future = new PublicFutureTask();
6144N/A abnormalRef.set(future);
6144N/A future.setException(throwable);
6144N/A }}};
6144N/A
6144N/A Thread doneTimedGetNormal = new CheckedThread("doneTimedGetNormal") {
6144N/A protected void realRun() throws Exception {
6144N/A while (! quittingTime()) {
6144N/A PublicFutureTask future = normalRef.get();
6144N/A if (future != null) {
6144N/A while (!future.isDone())
6144N/A ;
6144N/A check(future.get(0L, TimeUnit.HOURS) == Boolean.TRUE);
6144N/A }}}};
6144N/A
6144N/A Thread doneTimedGetAbnormal = new CheckedThread("doneTimedGetAbnormal") {
6144N/A protected void realRun() throws Exception {
6144N/A while (! quittingTime()) {
6144N/A PublicFutureTask future = abnormalRef.get();
6144N/A if (future != null) {
6144N/A while (!future.isDone())
6144N/A ;
6144N/A try { future.get(0L, TimeUnit.HOURS); fail(); }
6144N/A catch (ExecutionException t) {
6144N/A check(t.getCause() == throwable);
6144N/A }
6144N/A }}}};
6144N/A
6144N/A for (Thread thread : new Thread[] {
6144N/A setter,
6144N/A setterException,
6144N/A doneTimedGetNormal,
6144N/A doneTimedGetAbnormal }) {
6144N/A thread.join(timeoutMillis + testDurationMillis);
6144N/A if (thread.isAlive()) {
6144N/A System.err.printf("Hung thread: %s%n", thread.getName());
6144N/A failed++;
6144N/A for (StackTraceElement e : thread.getStackTrace())
6144N/A System.err.println(e);
6144N/A // Kludge alert
6144N/A thread.stop();
6144N/A thread.join(timeoutMillis);
6144N/A }
6144N/A }
6144N/A }
6144N/A
6144N/A //--------------------- Infrastructure ---------------------------
6144N/A volatile int passed = 0, failed = 0;
6144N/A void pass() {passed++;}
6144N/A void fail() {failed++; Thread.dumpStack();}
6144N/A void fail(String msg) {System.err.println(msg); fail();}
6144N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
6144N/A void check(boolean cond) {if (cond) pass(); else fail();}
6144N/A void equal(Object x, Object y) {
6144N/A if (x == null ? y == null : x.equals(y)) pass();
6144N/A else fail(x + " not equal to " + y);}
6144N/A public static void main(String[] args) throws Throwable {
6144N/A new DoneTimedGetLoops(args).instanceMain(args);}
6144N/A public void instanceMain(String[] args) throws Throwable {
6144N/A try {test(args);} catch (Throwable t) {unexpected(t);}
6144N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
6144N/A if (failed > 0) throw new AssertionError("Some tests failed");}
6144N/A}