36N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
36N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36N/A *
36N/A * This code is free software; you can redistribute it and/or modify it
36N/A * under the terms of the GNU General Public License version 2 only, as
36N/A * published by the Free Software Foundation.
36N/A *
36N/A * This code is distributed in the hope that it will be useful, but WITHOUT
36N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
36N/A * version 2 for more details (a copy is included in the LICENSE file that
36N/A * accompanied this code).
36N/A *
36N/A * You should have received a copy of the GNU General Public License version
36N/A * 2 along with this work; if not, write to the Free Software Foundation,
36N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
36N/A */
36N/A
36N/A/*
36N/A * @test
36N/A * @bug 6602600
36N/A * @run main/othervm -Xmx8m BasicCancelTest
36N/A * @summary Check effectiveness of RemoveOnCancelPolicy
36N/A */
36N/A
36N/Aimport java.util.concurrent.*;
36N/Aimport java.util.Random;
36N/A
36N/A/**
36N/A * Simple timer cancellation test. Submits tasks to a scheduled executor
36N/A * service and immediately cancels them.
36N/A */
36N/Apublic class BasicCancelTest {
36N/A
36N/A void checkShutdown(final ExecutorService es) {
36N/A final Runnable nop = new Runnable() {public void run() {}};
36N/A try {
36N/A if (new Random().nextBoolean()) {
36N/A check(es.isShutdown());
36N/A if (es instanceof ThreadPoolExecutor)
36N/A check(((ThreadPoolExecutor) es).isTerminating()
36N/A || es.isTerminated());
36N/A THROWS(RejectedExecutionException.class,
36N/A new F(){void f(){es.execute(nop);}});
36N/A }
36N/A } catch (Throwable t) { unexpected(t); }
36N/A }
36N/A
36N/A void checkTerminated(final ThreadPoolExecutor tpe) {
36N/A try {
36N/A checkShutdown(tpe);
36N/A check(tpe.getQueue().isEmpty());
36N/A check(tpe.isTerminated());
36N/A check(! tpe.isTerminating());
36N/A equal(tpe.getActiveCount(), 0);
36N/A equal(tpe.getPoolSize(), 0);
36N/A equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
36N/A check(tpe.awaitTermination(0, TimeUnit.SECONDS));
36N/A } catch (Throwable t) { unexpected(t); }
36N/A }
36N/A
36N/A void test(String[] args) throws Throwable {
36N/A
36N/A final ScheduledThreadPoolExecutor pool =
36N/A new ScheduledThreadPoolExecutor(1);
36N/A
36N/A // Needed to avoid OOME
36N/A pool.setRemoveOnCancelPolicy(true);
36N/A
36N/A final long moreThanYouCanChew = Runtime.getRuntime().freeMemory() / 4;
36N/A System.out.printf("moreThanYouCanChew=%d%n", moreThanYouCanChew);
36N/A
36N/A Runnable noopTask = new Runnable() { public void run() {}};
36N/A
36N/A for (long i = 0; i < moreThanYouCanChew; i++)
36N/A pool.schedule(noopTask, 10, TimeUnit.MINUTES).cancel(true);
36N/A
36N/A pool.shutdown();
36N/A check(pool.awaitTermination(1L, TimeUnit.DAYS));
36N/A checkTerminated(pool);
36N/A equal(pool.getTaskCount(), 0L);
36N/A equal(pool.getCompletedTaskCount(), 0L);
36N/A }
36N/A
36N/A //--------------------- Infrastructure ---------------------------
36N/A volatile int passed = 0, failed = 0;
36N/A void pass() {passed++;}
36N/A void fail() {failed++; Thread.dumpStack();}
36N/A void fail(String msg) {System.err.println(msg); fail();}
36N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
36N/A void check(boolean cond) {if (cond) pass(); else fail();}
36N/A void equal(Object x, Object y) {
36N/A if (x == null ? y == null : x.equals(y)) pass();
36N/A else fail(x + " not equal to " + y);}
36N/A public static void main(String[] args) throws Throwable {
36N/A new BasicCancelTest().instanceMain(args);}
36N/A void instanceMain(String[] args) throws Throwable {
36N/A try {test(args);} catch (Throwable t) {unexpected(t);}
36N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
36N/A if (failed > 0) throw new AssertionError("Some tests failed");}
36N/A abstract class F {abstract void f() throws Throwable;}
36N/A void THROWS(Class<? extends Throwable> k, F... fs) {
36N/A for (F f : fs)
36N/A try {f.f(); fail("Expected " + k.getName() + " not thrown");}
36N/A catch (Throwable t) {
36N/A if (k.isAssignableFrom(t.getClass())) pass();
36N/A else unexpected(t);}}
36N/A}