author | dl |
Thu, 03 Mar 2016 10:43:07 -0800 | |
changeset 36233 | f85ed703cf7e |
parent 34347 | 4a17f9e90a0f |
child 43522 | f9c6f543c4db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
2 |
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6384064 |
|
27 |
* @summary Check proper handling of interrupts |
|
28 |
* @author Martin Buchholz |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
29 |
* @library /lib/testlibrary/ |
2 | 30 |
*/ |
31 |
||
34347 | 32 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
33 |
||
34 |
import java.util.ArrayList; |
|
35 |
import java.util.List; |
|
36 |
import java.util.concurrent.ArrayBlockingQueue; |
|
37 |
import java.util.concurrent.BlockingDeque; |
|
38 |
import java.util.concurrent.BlockingQueue; |
|
39 |
import java.util.concurrent.LinkedBlockingDeque; |
|
40 |
import java.util.concurrent.LinkedBlockingQueue; |
|
41 |
import java.util.concurrent.SynchronousQueue; |
|
42 |
import java.util.concurrent.Executor; |
|
43 |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
44 |
import jdk.testlibrary.Utils; |
2 | 45 |
|
46 |
public class Interrupt { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
47 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
2 | 48 |
|
49 |
static void checkInterrupted0(Iterable<Fun> fs, Executor ex) { |
|
50 |
for (Fun f : fs) { |
|
51 |
try { |
|
52 |
ex.execute(new Runnable() { |
|
53 |
final Thread thisThread = Thread.currentThread(); |
|
54 |
public void run() { thisThread.interrupt(); }}); |
|
55 |
f.f(); |
|
56 |
fail("Expected InterruptedException not thrown"); |
|
57 |
} catch (InterruptedException e) { |
|
58 |
check(! Thread.interrupted()); |
|
59 |
} catch (Throwable t) { unexpected(t); } |
|
60 |
} |
|
61 |
} |
|
62 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
63 |
static void checkInterrupted(Iterable<Fun> fs) |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
64 |
throws InterruptedException { |
2 | 65 |
final Executor immediateExecutor = new Executor() { |
66 |
public void execute(Runnable r) { |
|
67 |
r.run(); }}; |
|
68 |
final ScheduledThreadPoolExecutor stpe |
|
69 |
= new ScheduledThreadPoolExecutor(1); |
|
70 |
final Executor delayedExecutor = new Executor() { |
|
71 |
public void execute(Runnable r) { |
|
72 |
stpe.schedule(r, 20, MILLISECONDS); }}; |
|
73 |
checkInterrupted0(fs, immediateExecutor); |
|
74 |
checkInterrupted0(fs, delayedExecutor); |
|
75 |
stpe.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
76 |
check(stpe.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
2 | 77 |
} |
78 |
||
79 |
static void testQueue(final BlockingQueue<Object> q) { |
|
80 |
try { |
|
81 |
final BlockingDeque<Object> deq = |
|
3708 | 82 |
(q instanceof BlockingDeque<?>) ? |
83 |
(BlockingDeque<Object>) q : null; |
|
2 | 84 |
q.clear(); |
85 |
List<Fun> fs = new ArrayList<Fun>(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
86 |
fs.add(() -> q.take()); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
87 |
fs.add(() -> q.poll(LONG_DELAY_MS, MILLISECONDS)); |
2 | 88 |
if (deq != null) { |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
89 |
fs.add(() -> deq.takeFirst()); |
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
90 |
fs.add(() -> deq.takeLast()); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
91 |
fs.add(() -> deq.pollFirst(LONG_DELAY_MS, MILLISECONDS)); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
92 |
fs.add(() -> deq.pollLast(LONG_DELAY_MS, MILLISECONDS)); |
2 | 93 |
} |
94 |
||
95 |
checkInterrupted(fs); |
|
96 |
||
97 |
// fill q to capacity, to ensure insertions will block |
|
98 |
while (q.remainingCapacity() > 0) |
|
99 |
try { q.put(1); } |
|
100 |
catch (Throwable t) { unexpected(t); } |
|
101 |
||
102 |
fs.clear(); |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
103 |
fs.add(() -> q.put(1)); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
104 |
fs.add(() -> q.offer(1, LONG_DELAY_MS, MILLISECONDS)); |
2 | 105 |
if (deq != null) { |
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
106 |
fs.add(() -> deq.putFirst(1)); |
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
107 |
fs.add(() -> deq.putLast(1)); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
108 |
fs.add(() -> deq.offerFirst(1, LONG_DELAY_MS, MILLISECONDS)); |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
109 |
fs.add(() -> deq.offerLast(1, LONG_DELAY_MS, MILLISECONDS)); |
2 | 110 |
} |
111 |
checkInterrupted(fs); |
|
3708 | 112 |
} catch (Throwable t) { |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
113 |
System.out.printf("Failed: %s%n", q.getClass().getSimpleName()); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
114 |
unexpected(t); |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
115 |
} finally { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
24692
diff
changeset
|
116 |
Thread.interrupted(); // clear interrupts, just in case |
3708 | 117 |
} |
2 | 118 |
} |
119 |
||
120 |
private static void realMain(final String[] args) throws Throwable { |
|
121 |
testQueue(new SynchronousQueue<Object>()); |
|
122 |
testQueue(new ArrayBlockingQueue<Object>(1,false)); |
|
123 |
testQueue(new ArrayBlockingQueue<Object>(1,true)); |
|
124 |
testQueue(new LinkedBlockingQueue<Object>(1)); |
|
125 |
testQueue(new LinkedBlockingDeque<Object>(1)); |
|
126 |
} |
|
127 |
||
128 |
//--------------------- Infrastructure --------------------------- |
|
129 |
static volatile int passed = 0, failed = 0; |
|
130 |
static void pass() {passed++;} |
|
131 |
static void fail() {failed++; Thread.dumpStack();} |
|
132 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
133 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
134 |
static void check(boolean cond) {if (cond) pass(); else fail();} |
|
135 |
static void equal(Object x, Object y) { |
|
136 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
137 |
else fail(x + " not equal to " + y);} |
|
138 |
public static void main(String[] args) throws Throwable { |
|
139 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
140 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
|
141 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
24692
268fbc344d53
8037866: Replace the Fun class in tests with lambdas
igerasim
parents:
22952
diff
changeset
|
142 |
interface Fun {void f() throws Throwable;} |
2 | 143 |
} |