1 /*
2  * Copyright (c) 2008, 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  */
23 
24 /*
25  * @test
26  * @bug 6602600
27  * @run main/othervm -Xmx8m BasicCancelTest
28  * @summary Check effectiveness of RemoveOnCancelPolicy
29  */
30 
31 import java.util.Random;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.RejectedExecutionException;
34 import java.util.concurrent.ScheduledThreadPoolExecutor;
35 import java.util.concurrent.ThreadPoolExecutor;
36 import java.util.concurrent.TimeUnit;
37 
38 /**
39  * Simple timer cancellation test. Submits tasks to a scheduled executor
40  * service and immediately cancels them.
41  */
42 public class BasicCancelTest {
43 
checkShutdown(final ExecutorService es)44     void checkShutdown(final ExecutorService es) {
45         final Runnable nop = new Runnable() {public void run() {}};
46         try {
47             if (new Random().nextBoolean()) {
48                 check(es.isShutdown());
49                 if (es instanceof ThreadPoolExecutor)
50                     check(((ThreadPoolExecutor) es).isTerminating()
51                           || es.isTerminated());
52                 THROWS(RejectedExecutionException.class,
53                        new F(){void f(){es.execute(nop);}});
54             }
55         } catch (Throwable t) { unexpected(t); }
56     }
57 
checkTerminated(final ThreadPoolExecutor tpe)58     void checkTerminated(final ThreadPoolExecutor tpe) {
59         try {
60             checkShutdown(tpe);
61             check(tpe.getQueue().isEmpty());
62             check(tpe.isTerminated());
63             check(! tpe.isTerminating());
64             equal(tpe.getActiveCount(), 0);
65             equal(tpe.getPoolSize(), 0);
66             equal(tpe.getTaskCount(), tpe.getCompletedTaskCount());
67             check(tpe.awaitTermination(0L, TimeUnit.SECONDS));
68         } catch (Throwable t) { unexpected(t); }
69     }
70 
test(String[] args)71     void test(String[] args) throws Throwable {
72 
73         final ScheduledThreadPoolExecutor pool =
74             new ScheduledThreadPoolExecutor(1);
75 
76         // Needed to avoid OOME
77         pool.setRemoveOnCancelPolicy(true);
78 
79         final long moreThanYouCanChew = Runtime.getRuntime().freeMemory() / 4;
80         System.out.printf("moreThanYouCanChew=%d%n", moreThanYouCanChew);
81 
82         Runnable noopTask = new Runnable() { public void run() {}};
83 
84         for (long i = 0; i < moreThanYouCanChew; i++)
85             pool.schedule(noopTask, 10, TimeUnit.MINUTES).cancel(true);
86 
87         pool.shutdown();
88         check(pool.awaitTermination(1L, TimeUnit.DAYS));
89         checkTerminated(pool);
90         equal(pool.getTaskCount(), 0L);
91         equal(pool.getCompletedTaskCount(), 0L);
92     }
93 
94     //--------------------- Infrastructure ---------------------------
95     volatile int passed = 0, failed = 0;
pass()96     void pass() {passed++;}
fail()97     void fail() {failed++; Thread.dumpStack();}
fail(String msg)98     void fail(String msg) {System.err.println(msg); fail();}
unexpected(Throwable t)99     void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)100     void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)101     void equal(Object x, Object y) {
102         if (x == null ? y == null : x.equals(y)) pass();
103         else fail(x + " not equal to " + y);}
main(String[] args)104     public static void main(String[] args) throws Throwable {
105         new BasicCancelTest().instanceMain(args);}
instanceMain(String[] args)106     void instanceMain(String[] args) throws Throwable {
107         try {test(args);} catch (Throwable t) {unexpected(t);}
108         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
109         if (failed > 0) throw new AssertionError("Some tests failed");}
f()110     abstract class F {abstract void f() throws Throwable;}
THROWS(Class<? extends Throwable> k, F... fs)111     void THROWS(Class<? extends Throwable> k, F... fs) {
112         for (F f : fs)
113             try {f.f(); fail("Expected " + k.getName() + " not thrown");}
114             catch (Throwable t) {
115                 if (k.isAssignableFrom(t.getClass())) pass();
116                 else unexpected(t);}}
117 }
118