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 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
5506 | 18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
2 | 21 |
*/ |
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
8401
diff
changeset
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 32 |
*/ |
33 |
||
34 |
/* |
|
35 |
* @test |
|
36 |
* @bug 4486658 |
|
37 |
* @summary Checks for responsiveness of blocking queues to cancellation. |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
38 |
* @library /lib/testlibrary/ |
2 | 39 |
*/ |
40 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
41 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
42 |
|
34347 | 43 |
import java.util.ArrayList; |
44 |
import java.util.List; |
|
45 |
import java.util.SplittableRandom; |
|
46 |
import java.util.concurrent.ArrayBlockingQueue; |
|
47 |
import java.util.concurrent.BlockingQueue; |
|
48 |
import java.util.concurrent.Callable; |
|
49 |
import java.util.concurrent.CancellationException; |
|
50 |
import java.util.concurrent.CountDownLatch; |
|
51 |
import java.util.concurrent.CyclicBarrier; |
|
52 |
import java.util.concurrent.ExecutorService; |
|
53 |
import java.util.concurrent.Executors; |
|
54 |
import java.util.concurrent.Future; |
|
55 |
import java.util.concurrent.LinkedBlockingDeque; |
|
56 |
import java.util.concurrent.LinkedBlockingQueue; |
|
57 |
import java.util.concurrent.SynchronousQueue; |
|
58 |
import java.util.concurrent.TimeUnit; |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
59 |
import jdk.testlibrary.Utils; |
2 | 60 |
|
61 |
public class CancelledProducerConsumerLoops { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
62 |
static final long LONG_DELAY_MS = Utils.adjustTimeout(10_000); |
34347 | 63 |
static ExecutorService pool; |
2 | 64 |
|
65 |
public static void main(String[] args) throws Exception { |
|
34347 | 66 |
final int maxPairs = (args.length > 0) ? Integer.parseInt(args[0]) : 5; |
2 | 67 |
|
34347 | 68 |
pool = Executors.newCachedThreadPool(); |
2 | 69 |
for (int i = 1; i <= maxPairs; i += (i+1) >>> 1) { |
34347 | 70 |
final List<BlockingQueue<Integer>> queues = new ArrayList<>(); |
71 |
queues.add(new ArrayBlockingQueue<Integer>(100)); |
|
72 |
queues.add(new LinkedBlockingQueue<Integer>(100)); |
|
73 |
queues.add(new LinkedBlockingDeque<Integer>(100)); |
|
74 |
queues.add(new SynchronousQueue<Integer>()); |
|
75 |
// unbounded queue implementations are prone to OOME: |
|
76 |
// PriorityBlockingQueue, LinkedTransferQueue |
|
77 |
for (BlockingQueue<Integer> queue : queues) |
|
78 |
new CancelledProducerConsumerLoops(i, queue).run(); |
|
2 | 79 |
} |
80 |
pool.shutdown(); |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
81 |
if (! pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)) |
34347 | 82 |
throw new AssertionError("timed out"); |
83 |
pool = null; |
|
84 |
} |
|
2 | 85 |
|
34347 | 86 |
final int npairs; |
87 |
final BlockingQueue<Integer> queue; |
|
88 |
final CountDownLatch producersInterrupted; |
|
89 |
final CountDownLatch consumersInterrupted; |
|
90 |
final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
|
91 |
final CyclicBarrier barrier; |
|
92 |
final SplittableRandom rnd = new SplittableRandom(); |
|
93 |
volatile boolean done = false; |
|
94 |
||
95 |
CancelledProducerConsumerLoops(int npairs, BlockingQueue<Integer> queue) { |
|
96 |
this.npairs = npairs; |
|
97 |
this.queue = queue; |
|
98 |
this.producersInterrupted = new CountDownLatch(npairs - 1); |
|
99 |
this.consumersInterrupted = new CountDownLatch(npairs - 1); |
|
100 |
this.barrier = new CyclicBarrier(npairs * 2 + 1, timer); |
|
101 |
} |
|
102 |
||
103 |
void run() throws Exception { |
|
3708 | 104 |
Future<?>[] prods = new Future<?>[npairs]; |
105 |
Future<?>[] cons = new Future<?>[npairs]; |
|
2 | 106 |
|
34347 | 107 |
for (int i = 0; i < npairs; i++) { |
108 |
prods[i] = pool.submit(new Producer()); |
|
109 |
cons[i] = pool.submit(new Consumer()); |
|
2 | 110 |
} |
111 |
barrier.await(); |
|
34347 | 112 |
Thread.sleep(rnd.nextInt(5)); |
2 | 113 |
|
34347 | 114 |
for (int i = 1; i < npairs; i++) { |
115 |
if (!prods[i].cancel(true) || |
|
116 |
!cons[i].cancel(true)) |
|
117 |
throw new AssertionError("completed before done"); |
|
118 |
} |
|
119 |
||
120 |
for (int i = 1; i < npairs; i++) { |
|
121 |
assertCancelled(prods[i]); |
|
122 |
assertCancelled(cons[i]); |
|
2 | 123 |
} |
124 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
125 |
if (!producersInterrupted.await(LONG_DELAY_MS, MILLISECONDS)) |
34347 | 126 |
throw new AssertionError("timed out"); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
127 |
if (!consumersInterrupted.await(LONG_DELAY_MS, MILLISECONDS)) |
34347 | 128 |
throw new AssertionError("timed out"); |
129 |
if (prods[0].isDone() || prods[0].isCancelled()) |
|
130 |
throw new AssertionError("completed too early"); |
|
131 |
||
132 |
done = true; |
|
133 |
||
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
134 |
if (! (prods[0].get(LONG_DELAY_MS, MILLISECONDS) instanceof Integer)) |
34347 | 135 |
throw new AssertionError("expected Integer"); |
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
136 |
if (! (cons[0].get(LONG_DELAY_MS, MILLISECONDS) instanceof Integer)) |
34347 | 137 |
throw new AssertionError("expected Integer"); |
138 |
} |
|
2 | 139 |
|
34347 | 140 |
void assertCancelled(Future<?> future) throws Exception { |
141 |
if (!future.isDone()) |
|
142 |
throw new AssertionError("not done"); |
|
143 |
if (!future.isCancelled()) |
|
144 |
throw new AssertionError("not cancelled"); |
|
145 |
try { |
|
36233
f85ed703cf7e
8150523: improve jtreg test timeout handling, especially -timeout:
dl
parents:
34347
diff
changeset
|
146 |
future.get(LONG_DELAY_MS, MILLISECONDS); |
34347 | 147 |
throw new AssertionError("should throw CancellationException"); |
148 |
} catch (CancellationException success) {} |
|
149 |
} |
|
150 |
||
151 |
class Producer implements Callable<Integer> { |
|
152 |
public Integer call() throws Exception { |
|
153 |
barrier.await(); |
|
154 |
int sum = 0; |
|
155 |
try { |
|
156 |
int x = 4321; |
|
157 |
while (!done) { |
|
158 |
if (Thread.interrupted()) throw new InterruptedException(); |
|
159 |
x = LoopHelpers.compute1(x); |
|
160 |
sum += LoopHelpers.compute2(x); |
|
161 |
queue.offer(new Integer(x), 1, TimeUnit.MILLISECONDS); |
|
162 |
} |
|
163 |
} catch (InterruptedException cancelled) { |
|
164 |
producersInterrupted.countDown(); |
|
2 | 165 |
} |
34347 | 166 |
return sum; |
2 | 167 |
} |
168 |
} |
|
169 |
||
34347 | 170 |
class Consumer implements Callable<Integer> { |
2 | 171 |
public Integer call() throws Exception { |
172 |
barrier.await(); |
|
34347 | 173 |
int sum = 0; |
174 |
try { |
|
175 |
while (!done) { |
|
176 |
Integer x = queue.poll(1, TimeUnit.MILLISECONDS); |
|
177 |
if (x != null) |
|
178 |
sum += LoopHelpers.compute1(x.intValue()); |
|
179 |
} |
|
180 |
} catch (InterruptedException cancelled) { |
|
181 |
consumersInterrupted.countDown(); |
|
2 | 182 |
} |
34347 | 183 |
return sum; |
2 | 184 |
} |
185 |
} |
|
186 |
} |