Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/util/concurrent/BlockingQueue/Interrupt.java
+++ new/test/java/util/concurrent/BlockingQueue/Interrupt.java
1 1 /*
2 2 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /*
25 25 * @test
26 26 * @bug 6384064
27 27 * @summary Check proper handling of interrupts
28 28 * @run main/othervm -XX:-UseVMInterruptibleIO Interrupt
29 29 * @author Martin Buchholz
30 30 */
31 31
32 32 import java.util.*;
33 33 import java.util.concurrent.*;
34 34 import static java.util.concurrent.TimeUnit.*;
35 35
36 36 public class Interrupt {
37 37
38 38 static void checkInterrupted0(Iterable<Fun> fs, Executor ex) {
39 39 for (Fun f : fs) {
40 40 try {
41 41 ex.execute(new Runnable() {
42 42 final Thread thisThread = Thread.currentThread();
43 43 public void run() { thisThread.interrupt(); }});
44 44 f.f();
45 45 fail("Expected InterruptedException not thrown");
46 46 } catch (InterruptedException e) {
47 47 check(! Thread.interrupted());
48 48 } catch (Throwable t) { unexpected(t); }
49 49 }
50 50 }
51 51
52 52 static void checkInterrupted(Iterable<Fun> fs) {
53 53 final Executor immediateExecutor = new Executor() {
54 54 public void execute(Runnable r) {
55 55 r.run(); }};
56 56 final ScheduledThreadPoolExecutor stpe
57 57 = new ScheduledThreadPoolExecutor(1);
58 58 final Executor delayedExecutor = new Executor() {
59 59 public void execute(Runnable r) {
60 60 stpe.schedule(r, 20, MILLISECONDS); }};
61 61 checkInterrupted0(fs, immediateExecutor);
62 62 checkInterrupted0(fs, delayedExecutor);
63 63 stpe.shutdown();
64 64 }
65 65
66 66 static void testQueue(final BlockingQueue<Object> q) {
67 67 try {
68 68 final BlockingDeque<Object> deq =
69 69 (q instanceof BlockingDeque<?>) ?
70 70 (BlockingDeque<Object>) q : null;
71 71 q.clear();
72 72 List<Fun> fs = new ArrayList<Fun>();
73 73 fs.add(new Fun() { void f() throws Throwable
74 74 { q.take(); }});
75 75 fs.add(new Fun() { void f() throws Throwable
76 76 { q.poll(60, SECONDS); }});
77 77 if (deq != null) {
78 78 fs.add(new Fun() { void f() throws Throwable
79 79 { deq.takeFirst(); }});
80 80 fs.add(new Fun() { void f() throws Throwable
81 81 { deq.takeLast(); }});
82 82 fs.add(new Fun() { void f() throws Throwable
83 83 { deq.pollFirst(7, SECONDS); }});
84 84 fs.add(new Fun() { void f() throws Throwable
85 85 { deq.pollLast(7, SECONDS); }});
86 86 }
87 87
88 88 checkInterrupted(fs);
89 89
90 90 // fill q to capacity, to ensure insertions will block
91 91 while (q.remainingCapacity() > 0)
92 92 try { q.put(1); }
93 93 catch (Throwable t) { unexpected(t); }
94 94
95 95 fs.clear();
96 96 fs.add(new Fun() { void f() throws Throwable
97 97 { q.put(1); }});
98 98 fs.add(new Fun() { void f() throws Throwable
99 99 { q.offer(1, 7, SECONDS); }});
100 100 if (deq != null) {
101 101 fs.add(new Fun() { void f() throws Throwable
102 102 { deq.putFirst(1); }});
103 103 fs.add(new Fun() { void f() throws Throwable
104 104 { deq.putLast(1); }});
105 105 fs.add(new Fun() { void f() throws Throwable
106 106 { deq.offerFirst(1, 7, SECONDS); }});
107 107 fs.add(new Fun() { void f() throws Throwable
108 108 { deq.offerLast(1, 7, SECONDS); }});
109 109 }
110 110 checkInterrupted(fs);
111 111 } catch (Throwable t) {
112 112 System.out.printf("Failed: %s%n", q.getClass().getSimpleName());
113 113 unexpected(t);
114 114 }
115 115 }
116 116
117 117 private static void realMain(final String[] args) throws Throwable {
118 118 testQueue(new SynchronousQueue<Object>());
119 119 testQueue(new ArrayBlockingQueue<Object>(1,false));
120 120 testQueue(new ArrayBlockingQueue<Object>(1,true));
121 121 testQueue(new LinkedBlockingQueue<Object>(1));
122 122 testQueue(new LinkedBlockingDeque<Object>(1));
123 123 }
124 124
125 125 //--------------------- Infrastructure ---------------------------
126 126 static volatile int passed = 0, failed = 0;
127 127 static void pass() {passed++;}
128 128 static void fail() {failed++; Thread.dumpStack();}
↓ open down ↓ |
128 lines elided |
↑ open up ↑ |
129 129 static void fail(String msg) {System.out.println(msg); fail();}
130 130 static void unexpected(Throwable t) {failed++; t.printStackTrace();}
131 131 static void check(boolean cond) {if (cond) pass(); else fail();}
132 132 static void equal(Object x, Object y) {
133 133 if (x == null ? y == null : x.equals(y)) pass();
134 134 else fail(x + " not equal to " + y);}
135 135 public static void main(String[] args) throws Throwable {
136 136 try {realMain(args);} catch (Throwable t) {unexpected(t);}
137 137 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
138 138 if (failed > 0) throw new AssertionError("Some tests failed");}
139 - private static abstract class Fun {abstract void f() throws Throwable;}
139 + private abstract static class Fun {abstract void f() throws Throwable;}
140 140 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX