ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/SynchronousQueueTest.java
Revision: 1.1
Committed: Sun Aug 31 19:24:55 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Log Message:
First check-in of tests to be in tck

File Contents

# Content
1 /*
2 * Written by members of JCP JSR-166 Expert Group and released to the
3 * public domain. Use, modify, and redistribute this code in any way
4 * without acknowledgement. Other contributors include Andrew Wright,
5 * Jeffrey Hayes, Pat Fischer, Mike Judd.
6 */
7
8 import junit.framework.*;
9 import java.util.*;
10 import java.util.concurrent.*;
11
12 public class SynchronousQueueTest extends TestCase {
13
14 private final static int N = 1;
15 private static long SHORT_DELAY_MS = 100;
16 private static long MEDIUM_DELAY_MS = 1000;
17 private static long LONG_DELAY_MS = 10000;
18
19 public static void main(String[] args) {
20 junit.textui.TestRunner.run (suite());
21 }
22
23 public static Test suite() {
24 return new TestSuite(SynchronousQueueTest.class);
25 }
26
27 public void testEmptyFull() {
28 SynchronousQueue q = new SynchronousQueue();
29 assertTrue(q.isEmpty());
30 assertEquals(0, q.size());
31 assertEquals(0, q.remainingCapacity());
32 assertFalse(q.offer(new Integer(3)));
33 }
34
35 public void testOfferNull(){
36 try {
37 SynchronousQueue q = new SynchronousQueue();
38 q.offer(null);
39 fail("should throw NPE");
40 } catch (NullPointerException success) { }
41 }
42
43 public void testOffer(){
44 SynchronousQueue q = new SynchronousQueue();
45 assertFalse(q.offer(new Integer(1)));
46 }
47
48 public void testAdd(){
49 try {
50 SynchronousQueue q = new SynchronousQueue();
51 assertEquals(0, q.remainingCapacity());
52 q.add(new Integer(0));
53 } catch (IllegalStateException success){
54 }
55 }
56
57 public void testAddAll1(){
58 try {
59 SynchronousQueue q = new SynchronousQueue();
60 q.addAll(null);
61 fail("Cannot add null collection");
62 }
63 catch (NullPointerException success) {}
64 }
65 public void testAddAll2(){
66 try {
67 SynchronousQueue q = new SynchronousQueue();
68 Integer[] ints = new Integer[N];
69 q.addAll(Arrays.asList(ints));
70 fail("Cannot add null elements");
71 }
72 catch (NullPointerException success) {}
73 }
74 public void testAddAll4(){
75 try {
76 SynchronousQueue q = new SynchronousQueue();
77 Integer[] ints = new Integer[N];
78 for (int i = 0; i < N; ++i)
79 ints[i] = new Integer(i);
80 q.addAll(Arrays.asList(ints));
81 fail("Cannot add with insufficient capacity");
82 }
83 catch (IllegalStateException success) {}
84 }
85
86 public void testPutNull() {
87 try {
88 SynchronousQueue q = new SynchronousQueue();
89 q.put(null);
90 fail("put should throw NPE");
91 }
92 catch (NullPointerException success){
93 }
94 catch (InterruptedException ie) {
95 fail("Unexpected exception");
96 }
97 }
98
99 public void testBlockingPut(){
100 Thread t = new Thread(new Runnable() {
101 public void run() {
102 try {
103 SynchronousQueue q = new SynchronousQueue();
104 q.put(new Integer(0));
105 fail("put should block");
106 } catch (InterruptedException ie){
107 }
108 }});
109 t.start();
110 try {
111 Thread.sleep(SHORT_DELAY_MS);
112 t.interrupt();
113 t.join();
114 }
115 catch (InterruptedException ie) {
116 fail("Unexpected exception");
117 }
118 }
119
120 public void testPutWithTake() {
121 final SynchronousQueue q = new SynchronousQueue();
122 Thread t = new Thread(new Runnable() {
123 public void run(){
124 int added = 0;
125 try {
126 q.put(new Object());
127 ++added;
128 q.put(new Object());
129 ++added;
130 q.put(new Object());
131 ++added;
132 q.put(new Object());
133 ++added;
134 fail("Should block");
135 } catch (InterruptedException e){
136 assertTrue(added >= 1);
137 }
138 }
139 });
140 try {
141 t.start();
142 Thread.sleep(SHORT_DELAY_MS);
143 q.take();
144 Thread.sleep(SHORT_DELAY_MS);
145 t.interrupt();
146 t.join();
147 } catch (Exception e){
148 fail("Unexpected exception");
149 }
150 }
151
152 public void testTimedOffer() {
153 final SynchronousQueue q = new SynchronousQueue();
154 Thread t = new Thread(new Runnable() {
155 public void run(){
156 try {
157
158 assertFalse(q.offer(new Object(), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS));
159 q.offer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS);
160 fail("Should block");
161 } catch (InterruptedException success){}
162 }
163 });
164
165 try {
166 t.start();
167 Thread.sleep(SHORT_DELAY_MS);
168 t.interrupt();
169 t.join();
170 } catch (Exception e){
171 fail("Unexpected exception");
172 }
173 }
174
175
176 public void testTakeFromEmpty() {
177 final SynchronousQueue q = new SynchronousQueue();
178 Thread t = new Thread(new Runnable() {
179 public void run(){
180 try {
181 q.take();
182 fail("Should block");
183 } catch (InterruptedException success){ }
184 }
185 });
186 try {
187 t.start();
188 Thread.sleep(SHORT_DELAY_MS);
189 t.interrupt();
190 t.join();
191 } catch (Exception e){
192 fail("Unexpected exception");
193 }
194 }
195
196 public void testPoll(){
197 SynchronousQueue q = new SynchronousQueue();
198 assertNull(q.poll());
199 }
200
201 public void testTimedPoll0() {
202 try {
203 SynchronousQueue q = new SynchronousQueue();
204 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
205 } catch (InterruptedException e){
206 fail("Unexpected exception");
207 }
208 }
209
210 public void testTimedPoll() {
211 try {
212 SynchronousQueue q = new SynchronousQueue();
213 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
214 } catch (InterruptedException e){
215 fail("Unexpected exception");
216 }
217 }
218
219 public void testInterruptedTimedPoll(){
220 Thread t = new Thread(new Runnable() {
221 public void run() {
222 try {
223 SynchronousQueue q = new SynchronousQueue();
224 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
225 } catch (InterruptedException success){
226 }
227 }});
228 t.start();
229 try {
230 Thread.sleep(SHORT_DELAY_MS);
231 t.interrupt();
232 t.join();
233 }
234 catch (InterruptedException ie) {
235 fail("Unexpected exception");
236 }
237 }
238
239 public void testTimedPollWithOffer(){
240 final SynchronousQueue q = new SynchronousQueue();
241 Thread t = new Thread(new Runnable() {
242 public void run(){
243 try {
244 assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
245 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
246 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
247 fail("Should block");
248 } catch (InterruptedException success) { }
249 }
250 });
251 try {
252 t.start();
253 Thread.sleep(SHORT_DELAY_MS * 2);
254 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
255 t.interrupt();
256 t.join();
257 } catch (Exception e){
258 fail("Unexpected exception");
259 }
260 }
261
262
263 public void testPeek(){
264 SynchronousQueue q = new SynchronousQueue();
265 assertNull(q.peek());
266 }
267
268 public void testElement(){
269 SynchronousQueue q = new SynchronousQueue();
270 try {
271 q.element();
272 fail("no such element");
273 }
274 catch (NoSuchElementException success) {}
275 }
276
277 public void testRemove(){
278 SynchronousQueue q = new SynchronousQueue();
279 try {
280 q.remove();
281 fail("remove should throw");
282 } catch (NoSuchElementException success){
283 }
284 }
285
286 public void testRemoveElement(){
287 SynchronousQueue q = new SynchronousQueue();
288 for (int i = 1; i < N; i+=2) {
289 assertFalse(q.remove(new Integer(i)));
290 }
291 assert(q.isEmpty());
292 }
293
294 public void testContains(){
295 SynchronousQueue q = new SynchronousQueue();
296 for (int i = 0; i < N; ++i) {
297 assertFalse(q.contains(new Integer(i)));
298 }
299 }
300
301 public void testClear(){
302 SynchronousQueue q = new SynchronousQueue();
303 q.clear();
304 assertTrue(q.isEmpty());
305 }
306
307 public void testContainsAll(){
308 SynchronousQueue q = new SynchronousQueue();
309 Integer[] empty = new Integer[0];
310 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
311 // assertTrue(q.containsAll(Arrays.asList(empty)));
312 assertFalse(q.containsAll(Arrays.asList(ints)));
313 }
314
315 public void testRetainAll(){
316 SynchronousQueue q = new SynchronousQueue();
317 Integer[] empty = new Integer[0];
318 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
319 q.retainAll(Arrays.asList(ints));
320 // assertTrue(q.containsAll(Arrays.asList(empty)));
321 assertFalse(q.containsAll(Arrays.asList(ints)));
322 }
323
324 public void testRemoveAll(){
325 SynchronousQueue q = new SynchronousQueue();
326 Integer[] empty = new Integer[0];
327 Integer[] ints = new Integer[1]; ints[0] = new Integer(0);
328 q.removeAll(Arrays.asList(ints));
329 // assertTrue(q.containsAll(Arrays.asList(empty)));
330 assertFalse(q.containsAll(Arrays.asList(ints)));
331 }
332
333
334 public void testToArray(){
335 SynchronousQueue q = new SynchronousQueue();
336 Object[] o = q.toArray();
337 assertEquals(o.length, 0);
338 }
339
340 public void testToArray2(){
341 SynchronousQueue q = new SynchronousQueue();
342 Integer[] ints = new Integer[1];
343 assertNull(ints[0]);
344 }
345
346 public void testIterator(){
347 SynchronousQueue q = new SynchronousQueue();
348 Iterator it = q.iterator();
349 assertFalse(it.hasNext());
350 try {
351 Object x = it.next();
352 fail("should throw");
353 }
354 catch (NoSuchElementException success) {}
355 }
356
357 public void testIteratorRemove(){
358 SynchronousQueue q = new SynchronousQueue();
359 Iterator it = q.iterator();
360 try {
361 it.remove();
362 fail("should throw");
363 }
364 catch (IllegalStateException success) {}
365 }
366
367 public void testToString(){
368 SynchronousQueue q = new SynchronousQueue();
369 String s = q.toString();
370 assertTrue(s != null);
371 }
372
373
374 public void testOfferInExecutor() {
375 final SynchronousQueue q = new SynchronousQueue();
376 ExecutorService executor = Executors.newFixedThreadPool(2);
377 final Integer one = new Integer(1);
378
379 executor.execute(new Runnable() {
380 public void run() {
381 assertFalse(q.offer(one));
382 try {
383 assertTrue(q.offer(one, MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
384 assertEquals(0, q.remainingCapacity());
385 }
386 catch (InterruptedException e) {
387 fail("should not be interrupted");
388 }
389 }
390 });
391
392 executor.execute(new Runnable() {
393 public void run() {
394 try {
395 Thread.sleep(MEDIUM_DELAY_MS);
396 assertEquals(one, q.take());
397 }
398 catch (InterruptedException e) {
399 fail("should not be interrupted");
400 }
401 }
402 });
403
404 executor.shutdown();
405
406 }
407
408 public void testPollInExecutor() {
409
410 final SynchronousQueue q = new SynchronousQueue();
411
412 ExecutorService executor = Executors.newFixedThreadPool(2);
413
414 executor.execute(new Runnable() {
415 public void run() {
416 assertNull(q.poll());
417 try {
418 assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS));
419 assertTrue(q.isEmpty());
420 }
421 catch (InterruptedException e) {
422 fail("should not be interrupted");
423 }
424 }
425 });
426
427 executor.execute(new Runnable() {
428 public void run() {
429 try {
430 Thread.sleep(MEDIUM_DELAY_MS);
431 q.put(new Integer(1));
432 }
433 catch (InterruptedException e) {
434 fail("should not be interrupted");
435 }
436 }
437 });
438
439 executor.shutdown();
440
441 }
442
443 }