blob: 696b0960fc09ac173b307a680b7b32f00fc4ff4d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6384064
27 * @summary Check proper handling of interrupts
28 * @run main/othervm -XX:-UseVMInterruptibleIO Interrupt
29 * @author Martin Buchholz
30 */
31
32import java.util.*;
33import java.util.concurrent.*;
34import static java.util.concurrent.TimeUnit.*;
35
36public class Interrupt {
37
38 static void checkInterrupted0(Iterable<Fun> fs, Executor ex) {
39 for (Fun f : fs) {
40 try {
41 ex.execute(new Runnable() {
42 final Thread thisThread = Thread.currentThread();
43 public void run() { thisThread.interrupt(); }});
44 f.f();
45 fail("Expected InterruptedException not thrown");
46 } catch (InterruptedException e) {
47 check(! Thread.interrupted());
48 } catch (Throwable t) { unexpected(t); }
49 }
50 }
51
52 static void checkInterrupted(Iterable<Fun> fs) {
53 final Executor immediateExecutor = new Executor() {
54 public void execute(Runnable r) {
55 r.run(); }};
56 final ScheduledThreadPoolExecutor stpe
57 = new ScheduledThreadPoolExecutor(1);
58 final Executor delayedExecutor = new Executor() {
59 public void execute(Runnable r) {
60 stpe.schedule(r, 20, MILLISECONDS); }};
61 checkInterrupted0(fs, immediateExecutor);
62 checkInterrupted0(fs, delayedExecutor);
63 stpe.shutdown();
64 }
65
66 static void testQueue(final BlockingQueue<Object> q) {
67 try {
68 final BlockingDeque<Object> deq =
69 q instanceof BlockingDeque ? (BlockingDeque<Object>) q : null;
70 q.clear();
71 List<Fun> fs = new ArrayList<Fun>();
72 fs.add(new Fun() { void f() throws Throwable
73 { q.take(); }});
74 fs.add(new Fun() { void f() throws Throwable
75 { q.poll(60, SECONDS); }});
76 if (deq != null) {
77 fs.add(new Fun() { void f() throws Throwable
78 { deq.takeFirst(); }});
79 fs.add(new Fun() { void f() throws Throwable
80 { deq.takeLast(); }});
81 fs.add(new Fun() { void f() throws Throwable
82 { deq.pollFirst(7, SECONDS); }});
83 fs.add(new Fun() { void f() throws Throwable
84 { deq.pollLast(7, SECONDS); }});
85 }
86
87 checkInterrupted(fs);
88
89 // fill q to capacity, to ensure insertions will block
90 while (q.remainingCapacity() > 0)
91 try { q.put(1); }
92 catch (Throwable t) { unexpected(t); }
93
94 fs.clear();
95 fs.add(new Fun() { void f() throws Throwable
96 { q.put(1); }});
97 fs.add(new Fun() { void f() throws Throwable
98 { q.offer(1, 7, SECONDS); }});
99 if (deq != null) {
100 fs.add(new Fun() { void f() throws Throwable
101 { deq.putFirst(1); }});
102 fs.add(new Fun() { void f() throws Throwable
103 { deq.putLast(1); }});
104 fs.add(new Fun() { void f() throws Throwable
105 { deq.offerFirst(1, 7, SECONDS); }});
106 fs.add(new Fun() { void f() throws Throwable
107 { deq.offerLast(1, 7, SECONDS); }});
108 }
109 checkInterrupted(fs);
110 } catch (Throwable t) { unexpected(t); }
111 }
112
113 private static void realMain(final String[] args) throws Throwable {
114 testQueue(new SynchronousQueue<Object>());
115 testQueue(new ArrayBlockingQueue<Object>(1,false));
116 testQueue(new ArrayBlockingQueue<Object>(1,true));
117 testQueue(new LinkedBlockingQueue<Object>(1));
118 testQueue(new LinkedBlockingDeque<Object>(1));
119 }
120
121 //--------------------- Infrastructure ---------------------------
122 static volatile int passed = 0, failed = 0;
123 static void pass() {passed++;}
124 static void fail() {failed++; Thread.dumpStack();}
125 static void fail(String msg) {System.out.println(msg); fail();}
126 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
127 static void check(boolean cond) {if (cond) pass(); else fail();}
128 static void equal(Object x, Object y) {
129 if (x == null ? y == null : x.equals(y)) pass();
130 else fail(x + " not equal to " + y);}
131 public static void main(String[] args) throws Throwable {
132 try {realMain(args);} catch (Throwable t) {unexpected(t);}
133 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
134 if (failed > 0) throw new AssertionError("Some tests failed");}
135 private static abstract class Fun {abstract void f() throws Throwable;}
136}