17 |
|
import java.util.Iterator; |
18 |
|
import java.util.NoSuchElementException; |
19 |
|
import java.util.concurrent.*; |
20 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
21 |
|
import junit.framework.Test; |
22 |
|
import junit.framework.TestSuite; |
23 |
|
|
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* Constructor builds new queue with size being zero and empty being true |
35 |
> |
* Constructor builds new queue with size being zero and empty |
36 |
> |
* being true |
37 |
|
*/ |
38 |
|
public void testConstructor1() { |
39 |
|
assertEquals(0, new LinkedTransferQueue().size()); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* Initializing constructor with null collection throws NullPointerException |
44 |
> |
* Initializing constructor with null collection throws |
45 |
> |
* NullPointerException |
46 |
|
*/ |
47 |
|
public void testConstructor2() { |
48 |
|
try { |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* Initializing from Collection of null elements throws NullPointerException |
56 |
> |
* Initializing from Collection of null elements throws |
57 |
> |
* NullPointerException |
58 |
|
*/ |
59 |
|
public void testConstructor3() { |
60 |
|
try { |
61 |
|
Integer[] ints = new Integer[SIZE]; |
62 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
62 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
63 |
|
shouldThrow(); |
64 |
|
} catch (NullPointerException success) { |
65 |
|
} |
75 |
|
for (int i = 0; i < SIZE - 1; ++i) { |
76 |
|
ints[i] = i; |
77 |
|
} |
78 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
78 |
> |
new LinkedTransferQueue(Arrays.asList(ints)); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) { |
81 |
|
} |
90 |
|
for (int i = 0; i < SIZE; ++i) { |
91 |
|
ints[i] = i; |
92 |
|
} |
93 |
< |
LinkedTransferQueue q = new LinkedTransferQueue(Arrays.asList(ints)); |
93 |
> |
LinkedTransferQueue q |
94 |
> |
= new LinkedTransferQueue(Arrays.asList(ints)); |
95 |
|
for (int i = 0; i < SIZE; ++i) { |
96 |
|
assertEquals(ints[i], q.poll()); |
97 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
/** |
181 |
< |
* addAll of a collection with any null elements throws NullPointerException after |
182 |
< |
* possibly adding some elements |
181 |
> |
* addAll of a collection with any null elements throws |
182 |
> |
* NullPointerException after possibly adding some elements |
183 |
|
*/ |
184 |
|
public void testAddAll3() { |
185 |
|
try { |
249 |
|
*/ |
250 |
|
public void testTakeFromEmpty() throws InterruptedException { |
251 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
252 |
< |
Thread t = new Thread(new Runnable() { |
253 |
< |
public void run() { |
254 |
< |
try { |
250 |
< |
q.take(); |
251 |
< |
threadShouldThrow(); |
252 |
< |
} catch (InterruptedException success) { |
253 |
< |
} catch (Throwable ex) { |
254 |
< |
threadUnexpectedException(ex); |
255 |
< |
} |
252 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
253 |
> |
void realRun() throws InterruptedException { |
254 |
> |
q.take(); |
255 |
|
}}); |
257 |
– |
t.start(); |
256 |
|
Thread.sleep(SHORT_DELAY_MS); |
257 |
|
t.interrupt(); |
258 |
|
t.join(); |
262 |
|
* Take removes existing elements until empty, then blocks interruptibly |
263 |
|
*/ |
264 |
|
public void testBlockingTake() throws InterruptedException { |
265 |
< |
Thread t = new Thread(new Runnable() { |
266 |
< |
public void run() { |
267 |
< |
try { |
268 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
269 |
< |
for (int i = 0; i < SIZE; ++i) { |
272 |
< |
threadAssertEquals(i, (int) q.take()); |
273 |
< |
} |
274 |
< |
q.take(); |
275 |
< |
threadShouldThrow(); |
276 |
< |
} catch (InterruptedException success) { |
277 |
< |
} catch (Throwable ex) { |
278 |
< |
threadUnexpectedException(ex); |
265 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
266 |
> |
void realRun() throws InterruptedException { |
267 |
> |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
268 |
> |
for (int i = 0; i < SIZE; ++i) { |
269 |
> |
threadAssertEquals(i, (int) q.take()); |
270 |
|
} |
271 |
+ |
q.take(); |
272 |
|
}}); |
281 |
– |
t.start(); |
273 |
|
Thread.sleep(SHORT_DELAY_MS); |
274 |
|
t.interrupt(); |
275 |
|
t.join(); |
292 |
|
public void testTimedPoll0() throws InterruptedException { |
293 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
294 |
|
for (int i = 0; i < SIZE; ++i) { |
295 |
< |
assertEquals(i, (int) q.poll(0, TimeUnit.MILLISECONDS)); |
295 |
> |
assertEquals(i, (int) q.poll(0, MILLISECONDS)); |
296 |
|
} |
297 |
< |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
297 |
> |
assertNull(q.poll(0, MILLISECONDS)); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
303 |
|
public void testTimedPoll() throws InterruptedException { |
304 |
|
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
305 |
|
for (int i = 0; i < SIZE; ++i) { |
306 |
< |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
306 |
> |
assertEquals(i, (int) q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
307 |
|
} |
308 |
< |
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
308 |
> |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
309 |
|
} |
310 |
|
|
311 |
|
/** |
313 |
|
* returning timeout status |
314 |
|
*/ |
315 |
|
public void testInterruptedTimedPoll() throws InterruptedException { |
316 |
< |
Thread t = new Thread(new Runnable() { |
317 |
< |
public void run() { |
318 |
< |
try { |
319 |
< |
LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
320 |
< |
for (int i = 0; i < SIZE; ++i) { |
321 |
< |
threadAssertEquals(i, (int) q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
331 |
< |
} |
332 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
333 |
< |
} catch (InterruptedException success) { |
334 |
< |
} catch (Throwable ex) { |
335 |
< |
threadUnexpectedException(ex); |
316 |
> |
final LinkedTransferQueue<Integer> q = populatedQueue(SIZE); |
317 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
318 |
> |
void realRun() throws InterruptedException { |
319 |
> |
for (int i = 0; i < SIZE; ++i) { |
320 |
> |
threadAssertEquals(i, (int) q.poll(LONG_DELAY_MS, |
321 |
> |
MILLISECONDS)); |
322 |
|
} |
323 |
+ |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
324 |
|
}}); |
325 |
< |
t.start(); |
339 |
< |
Thread.sleep(SHORT_DELAY_MS); |
325 |
> |
Thread.sleep(SMALL_DELAY_MS); |
326 |
|
t.interrupt(); |
327 |
|
t.join(); |
328 |
+ |
assertEquals(q.size(), 0); |
329 |
+ |
assertNull(q.poll()); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
335 |
|
*/ |
336 |
|
public void testTimedPollWithOffer() throws InterruptedException { |
337 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
338 |
< |
Thread t = new Thread(new Runnable() { |
339 |
< |
public void run() { |
340 |
< |
try { |
341 |
< |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
342 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
355 |
< |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
356 |
< |
threadShouldThrow(); |
357 |
< |
} catch (InterruptedException success) { |
358 |
< |
} catch (Throwable ex) { |
359 |
< |
threadUnexpectedException(ex); |
360 |
< |
} |
338 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
339 |
> |
void realRun() throws InterruptedException { |
340 |
> |
threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
341 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
342 |
> |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
343 |
|
}}); |
362 |
– |
t.start(); |
344 |
|
Thread.sleep(SMALL_DELAY_MS); |
345 |
< |
assertTrue(q.offer(zero, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
345 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
346 |
|
t.interrupt(); |
347 |
|
t.join(); |
348 |
|
} |
463 |
|
} |
464 |
|
|
465 |
|
/** |
466 |
< |
* retainAll(c) retains only those elements of c and reports true if changed |
466 |
> |
* retainAll(c) retains only those elements of c and reports true |
467 |
> |
* if changed |
468 |
|
*/ |
469 |
|
public void testRetainAll() { |
470 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
486 |
> |
* removeAll(c) removes only those elements of c and reports true |
487 |
> |
* if changed |
488 |
|
*/ |
489 |
|
public void testRemoveAll() { |
490 |
|
for (int i = 1; i < SIZE; ++i) { |
579 |
|
* iterator ordering is FIFO |
580 |
|
*/ |
581 |
|
public void testIteratorOrdering() { |
582 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
582 |
> |
final LinkedTransferQueue<Integer> q |
583 |
> |
= new LinkedTransferQueue<Integer>(); |
584 |
|
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
585 |
|
q.add(one); |
586 |
|
q.add(two); |
627 |
|
q.add(one); |
628 |
|
q.add(two); |
629 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
630 |
< |
executor.execute(new Runnable() { |
631 |
< |
public void run() { |
632 |
< |
try { |
633 |
< |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
634 |
< |
} catch (Throwable ex) { |
651 |
< |
threadUnexpectedException(ex); |
652 |
< |
} |
630 |
> |
|
631 |
> |
executor.execute(new CheckedRunnable() { |
632 |
> |
void realRun() { |
633 |
> |
threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, |
634 |
> |
MILLISECONDS)); |
635 |
|
}}); |
636 |
|
|
637 |
< |
executor.execute(new Runnable() { |
638 |
< |
public void run() { |
639 |
< |
try { |
640 |
< |
Thread.sleep(SMALL_DELAY_MS); |
659 |
< |
threadAssertEquals(one, q.take()); |
660 |
< |
} catch (Throwable ex) { |
661 |
< |
threadUnexpectedException(ex); |
662 |
< |
} |
637 |
> |
executor.execute(new CheckedRunnable() { |
638 |
> |
void realRun() throws InterruptedException { |
639 |
> |
Thread.sleep(SMALL_DELAY_MS); |
640 |
> |
threadAssertEquals(one, q.take()); |
641 |
|
}}); |
642 |
|
|
643 |
|
joinPool(executor); |
649 |
|
public void testPollInExecutor() { |
650 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
651 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
652 |
< |
executor.execute(new Runnable() { |
653 |
< |
public void run() { |
654 |
< |
try { |
655 |
< |
threadAssertNull(q.poll()); |
656 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
657 |
< |
threadAssertTrue(q.isEmpty()); |
658 |
< |
} catch (Throwable ex) { |
681 |
< |
threadUnexpectedException(ex); |
682 |
< |
} |
652 |
> |
|
653 |
> |
executor.execute(new CheckedRunnable() { |
654 |
> |
void realRun() throws InterruptedException { |
655 |
> |
threadAssertNull(q.poll()); |
656 |
> |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, |
657 |
> |
MILLISECONDS)); |
658 |
> |
threadAssertTrue(q.isEmpty()); |
659 |
|
}}); |
660 |
|
|
661 |
< |
executor.execute(new Runnable() { |
662 |
< |
public void run() { |
663 |
< |
try { |
664 |
< |
Thread.sleep(SMALL_DELAY_MS); |
689 |
< |
q.put(one); |
690 |
< |
} catch (Throwable ex) { |
691 |
< |
threadUnexpectedException(ex); |
692 |
< |
} |
661 |
> |
executor.execute(new CheckedRunnable() { |
662 |
> |
void realRun() throws InterruptedException { |
663 |
> |
Thread.sleep(SMALL_DELAY_MS); |
664 |
> |
q.put(one); |
665 |
|
}}); |
666 |
|
|
667 |
|
joinPool(executor); |
674 |
|
LinkedTransferQueue q = populatedQueue(SIZE); |
675 |
|
|
676 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
677 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
677 |
> |
ObjectOutputStream out |
678 |
> |
= new ObjectOutputStream(new BufferedOutputStream(bout)); |
679 |
|
out.writeObject(q); |
680 |
|
out.close(); |
681 |
|
|
682 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
683 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
682 |
> |
ByteArrayInputStream bin |
683 |
> |
= new ByteArrayInputStream(bout.toByteArray()); |
684 |
> |
ObjectInputStream in |
685 |
> |
= new ObjectInputStream(new BufferedInputStream(bin)); |
686 |
|
LinkedTransferQueue r = (LinkedTransferQueue) in.readObject(); |
687 |
|
|
688 |
|
assertEquals(q.size(), r.size()); |
746 |
|
*/ |
747 |
|
public void testDrainToWithActivePut() throws InterruptedException { |
748 |
|
final LinkedTransferQueue q = populatedQueue(SIZE); |
749 |
< |
Thread t = new Thread(new Runnable() { |
750 |
< |
public void run() { |
751 |
< |
try { |
777 |
< |
q.put(SIZE + 1); |
778 |
< |
} catch (Throwable ex) { |
779 |
< |
threadUnexpectedException(ex); |
780 |
< |
} |
749 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
750 |
> |
void realRun() { |
751 |
> |
q.put(SIZE + 1); |
752 |
|
}}); |
782 |
– |
t.start(); |
753 |
|
ArrayList l = new ArrayList(); |
754 |
|
q.drainTo(l); |
755 |
|
assertTrue(l.size() >= SIZE); |
812 |
|
public void testWaitingConsumer() throws InterruptedException { |
813 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
814 |
|
final ConsumerObserver waiting = new ConsumerObserver(); |
815 |
< |
new Thread(new Runnable() { |
816 |
< |
public void run() { |
817 |
< |
try { |
818 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
819 |
< |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
820 |
< |
threadAssertTrue(q.offer(new Object())); |
821 |
< |
} catch (Throwable ex) { |
822 |
< |
threadUnexpectedException(ex); |
823 |
< |
} |
824 |
< |
}}).start(); |
825 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) != null); |
826 |
< |
assertTrue(q.getWaitingConsumerCount() < waiting.getWaitingConsumers()); |
815 |
> |
|
816 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
817 |
> |
void realRun() throws InterruptedException { |
818 |
> |
Thread.sleep(SMALL_DELAY_MS); |
819 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
820 |
> |
waiting.setWaitingConsumer(q.getWaitingConsumerCount()); |
821 |
> |
threadAssertTrue(q.offer(new Object())); |
822 |
> |
}}); |
823 |
> |
|
824 |
> |
assertTrue(q.poll(LONG_DELAY_MS, MILLISECONDS) != null); |
825 |
> |
assertTrue(q.getWaitingConsumerCount() |
826 |
> |
< waiting.getWaitingConsumers()); |
827 |
> |
t.join(); |
828 |
|
} |
829 |
|
|
830 |
|
/** |
844 |
|
* is returned by this associated poll. |
845 |
|
*/ |
846 |
|
public void testTransfer2() throws InterruptedException { |
847 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
847 |
> |
final LinkedTransferQueue<Integer> q |
848 |
> |
= new LinkedTransferQueue<Integer>(); |
849 |
|
|
850 |
< |
new Thread(new Runnable() { |
851 |
< |
public void run() { |
852 |
< |
try { |
853 |
< |
q.transfer(SIZE); |
854 |
< |
threadAssertTrue(q.isEmpty()); |
883 |
< |
} catch (Throwable ex) { |
884 |
< |
threadUnexpectedException(ex); |
885 |
< |
} |
886 |
< |
}}).start(); |
850 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
851 |
> |
void realRun() throws InterruptedException { |
852 |
> |
q.transfer(SIZE); |
853 |
> |
threadAssertTrue(q.isEmpty()); |
854 |
> |
}}); |
855 |
|
|
856 |
|
Thread.sleep(SHORT_DELAY_MS); |
857 |
|
assertEquals(1, q.size()); |
858 |
|
assertEquals(SIZE, (int) q.poll()); |
859 |
|
assertTrue(q.isEmpty()); |
860 |
+ |
t.join(); |
861 |
|
} |
862 |
|
|
863 |
|
/** |
864 |
|
* transfer waits until a poll occurs, and then transfers in fifo order |
865 |
|
*/ |
866 |
|
public void testTransfer3() throws InterruptedException { |
867 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
867 |
> |
final LinkedTransferQueue<Integer> q |
868 |
> |
= new LinkedTransferQueue<Integer>(); |
869 |
|
|
870 |
< |
Thread first = |
871 |
< |
new Thread(new Runnable() { |
872 |
< |
public void run() { |
873 |
< |
try { |
874 |
< |
Integer i = SIZE + 1; |
875 |
< |
q.transfer(i); |
876 |
< |
threadAssertTrue(!q.contains(i)); |
907 |
< |
threadAssertEquals(1, q.size()); |
908 |
< |
} catch (Throwable ex) { |
909 |
< |
threadUnexpectedException(ex); |
910 |
< |
} |
911 |
< |
}}); |
912 |
< |
first.start(); |
870 |
> |
Thread first = newStartedThread(new CheckedRunnable() { |
871 |
> |
void realRun() throws InterruptedException { |
872 |
> |
Integer i = SIZE + 1; |
873 |
> |
q.transfer(i); |
874 |
> |
threadAssertTrue(!q.contains(i)); |
875 |
> |
threadAssertEquals(1, q.size()); |
876 |
> |
}}); |
877 |
|
|
878 |
< |
Thread interruptedThread = |
879 |
< |
new Thread(new Runnable() { |
880 |
< |
public void run() { |
881 |
< |
try { |
882 |
< |
while (q.size() == 0) |
883 |
< |
Thread.yield(); |
920 |
< |
q.transfer(SIZE); |
921 |
< |
threadShouldThrow(); |
922 |
< |
} catch (InterruptedException success) { |
923 |
< |
} catch (Throwable ex) { |
924 |
< |
threadUnexpectedException(ex); |
925 |
< |
} |
878 |
> |
Thread interruptedThread = newStartedThread( |
879 |
> |
new CheckedInterruptedRunnable() { |
880 |
> |
void realRun() throws InterruptedException { |
881 |
> |
while (q.size() == 0) |
882 |
> |
Thread.yield(); |
883 |
> |
q.transfer(SIZE); |
884 |
|
}}); |
927 |
– |
interruptedThread.start(); |
885 |
|
|
886 |
|
while (q.size() < 2) |
887 |
|
Thread.yield(); |
901 |
|
*/ |
902 |
|
public void testTransfer4() throws InterruptedException { |
903 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
904 |
< |
new Thread(new Runnable() { |
905 |
< |
public void run() { |
906 |
< |
try { |
907 |
< |
q.transfer(four); |
908 |
< |
threadAssertFalse(q.contains(four)); |
909 |
< |
threadAssertEquals(three, q.poll()); |
910 |
< |
} catch (Throwable ex) { |
911 |
< |
threadUnexpectedException(ex); |
912 |
< |
} |
956 |
< |
}}).start(); |
957 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
904 |
> |
|
905 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
906 |
> |
void realRun() throws InterruptedException { |
907 |
> |
q.transfer(four); |
908 |
> |
threadAssertFalse(q.contains(four)); |
909 |
> |
threadAssertEquals(three, q.poll()); |
910 |
> |
}}); |
911 |
> |
|
912 |
> |
Thread.sleep(SHORT_DELAY_MS); |
913 |
|
assertTrue(q.offer(three)); |
914 |
|
assertEquals(four, q.poll()); |
915 |
+ |
t.join(); |
916 |
|
} |
917 |
|
|
918 |
|
/** |
920 |
|
* is returned by this associated take. |
921 |
|
*/ |
922 |
|
public void testTransfer5() throws InterruptedException { |
923 |
< |
final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); |
923 |
> |
final LinkedTransferQueue<Integer> q |
924 |
> |
= new LinkedTransferQueue<Integer>(); |
925 |
|
|
926 |
< |
new Thread(new Runnable() { |
927 |
< |
public void run() { |
928 |
< |
try { |
929 |
< |
q.transfer(SIZE); |
930 |
< |
threadAssertTrue(q.isEmpty()); |
974 |
< |
} catch (Throwable ex) { |
975 |
< |
threadUnexpectedException(ex); |
976 |
< |
} |
977 |
< |
}}).start(); |
926 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
927 |
> |
void realRun() throws InterruptedException { |
928 |
> |
q.transfer(SIZE); |
929 |
> |
threadAssertTrue(q.isEmpty()); |
930 |
> |
}}); |
931 |
|
|
932 |
|
Thread.sleep(SHORT_DELAY_MS); |
933 |
|
assertEquals(SIZE, (int) q.take()); |
934 |
|
assertTrue(q.isEmpty()); |
935 |
+ |
t.join(); |
936 |
|
} |
937 |
|
|
938 |
|
/** |
942 |
|
try { |
943 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
944 |
|
q.tryTransfer(null); |
945 |
< |
this.shouldThrow(); |
945 |
> |
shouldThrow(); |
946 |
|
} catch (NullPointerException ex) { |
947 |
|
} |
948 |
|
} |
966 |
|
public void testTryTransfer3() throws InterruptedException { |
967 |
|
final Object hotPotato = new Object(); |
968 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
969 |
< |
new Thread(new Runnable() { |
970 |
< |
public void run() { |
971 |
< |
try { |
972 |
< |
while (! q.hasWaitingConsumer()) |
973 |
< |
Thread.yield(); |
974 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
975 |
< |
threadAssertTrue(q.isEmpty()); |
976 |
< |
threadAssertTrue(q.size() == 0); |
977 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
978 |
< |
} catch (Throwable ex) { |
979 |
< |
threadUnexpectedException(ex); |
980 |
< |
} |
1027 |
< |
}}).start(); |
1028 |
< |
assertTrue(q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS) == hotPotato); |
969 |
> |
|
970 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
971 |
> |
void realRun() { |
972 |
> |
while (! q.hasWaitingConsumer()) |
973 |
> |
Thread.yield(); |
974 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
975 |
> |
threadAssertTrue(q.isEmpty()); |
976 |
> |
threadAssertTrue(q.size() == 0); |
977 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
978 |
> |
}}); |
979 |
> |
|
980 |
> |
assertTrue(q.poll(MEDIUM_DELAY_MS, MILLISECONDS) == hotPotato); |
981 |
|
assertTrue(q.isEmpty()); |
982 |
+ |
t.join(); |
983 |
|
} |
984 |
|
|
985 |
|
/** |
989 |
|
public void testTryTransfer4() throws InterruptedException { |
990 |
|
final Object hotPotato = new Object(); |
991 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
992 |
< |
new Thread(new Runnable() { |
993 |
< |
public void run() { |
994 |
< |
try { |
995 |
< |
while (! q.hasWaitingConsumer()) |
996 |
< |
Thread.yield(); |
997 |
< |
threadAssertTrue(q.hasWaitingConsumer()); |
998 |
< |
threadAssertTrue(q.isEmpty()); |
999 |
< |
threadAssertTrue(q.size() == 0); |
1000 |
< |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1001 |
< |
} catch (Throwable ex) { |
1002 |
< |
threadUnexpectedException(ex); |
1050 |
< |
} |
1051 |
< |
}}).start(); |
992 |
> |
|
993 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
994 |
> |
void realRun() { |
995 |
> |
while (! q.hasWaitingConsumer()) |
996 |
> |
Thread.yield(); |
997 |
> |
threadAssertTrue(q.hasWaitingConsumer()); |
998 |
> |
threadAssertTrue(q.isEmpty()); |
999 |
> |
threadAssertTrue(q.size() == 0); |
1000 |
> |
threadAssertTrue(q.tryTransfer(hotPotato)); |
1001 |
> |
}}); |
1002 |
> |
|
1003 |
|
assertTrue(q.take() == hotPotato); |
1004 |
|
assertTrue(q.isEmpty()); |
1005 |
+ |
t.join(); |
1006 |
|
} |
1007 |
|
|
1008 |
|
/** |
1011 |
|
*/ |
1012 |
|
public void testTryTransfer5() throws InterruptedException { |
1013 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1014 |
< |
Thread toInterrupt = new Thread(new Runnable() { |
1015 |
< |
public void run() { |
1016 |
< |
try { |
1017 |
< |
q.tryTransfer(new Object(), LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
1066 |
< |
threadShouldThrow(); |
1067 |
< |
} catch (InterruptedException success) { |
1068 |
< |
} catch (Throwable ex) { |
1069 |
< |
threadUnexpectedException(ex); |
1070 |
< |
} |
1014 |
> |
|
1015 |
> |
Thread toInterrupt = newStartedThread(new CheckedInterruptedRunnable() { |
1016 |
> |
void realRun() throws InterruptedException { |
1017 |
> |
q.tryTransfer(new Object(), LONG_DELAY_MS, MILLISECONDS); |
1018 |
|
}}); |
1019 |
< |
toInterrupt.start(); |
1019 |
> |
|
1020 |
|
Thread.sleep(SMALL_DELAY_MS); |
1021 |
|
toInterrupt.interrupt(); |
1022 |
+ |
toInterrupt.join(); |
1023 |
|
} |
1024 |
|
|
1025 |
|
/** |
1027 |
|
*/ |
1028 |
|
public void testTryTransfer6() throws InterruptedException { |
1029 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1030 |
< |
new Thread(new Runnable() { |
1031 |
< |
public void run() { |
1032 |
< |
try { |
1033 |
< |
threadAssertFalse(q.tryTransfer(new Object(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1034 |
< |
} catch (Throwable ex) { |
1035 |
< |
threadUnexpectedException(ex); |
1036 |
< |
} |
1037 |
< |
}}).start(); |
1038 |
< |
Thread.sleep(LONG_DELAY_MS); |
1030 |
> |
|
1031 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1032 |
> |
void realRun() throws InterruptedException { |
1033 |
> |
threadAssertFalse |
1034 |
> |
(q.tryTransfer(new Object(), |
1035 |
> |
SHORT_DELAY_MS, MILLISECONDS)); |
1036 |
> |
}}); |
1037 |
> |
|
1038 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1039 |
|
assertTrue(q.isEmpty()); |
1040 |
+ |
t.join(); |
1041 |
|
} |
1042 |
|
|
1043 |
|
/** |
1047 |
|
public void testTryTransfer7() throws InterruptedException { |
1048 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1049 |
|
assertTrue(q.offer(four)); |
1050 |
< |
new Thread(new Runnable() { |
1051 |
< |
public void run() { |
1052 |
< |
try { |
1053 |
< |
threadAssertTrue(q.tryTransfer(five, LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
1054 |
< |
threadAssertTrue(q.isEmpty()); |
1055 |
< |
} catch (Throwable ex) { |
1056 |
< |
threadUnexpectedException(ex); |
1057 |
< |
} |
1109 |
< |
}}).start(); |
1050 |
> |
|
1051 |
> |
Thread t = newStartedThread(new CheckedRunnable() { |
1052 |
> |
void realRun() throws InterruptedException { |
1053 |
> |
threadAssertTrue(q.tryTransfer(five, |
1054 |
> |
MEDIUM_DELAY_MS, MILLISECONDS)); |
1055 |
> |
threadAssertTrue(q.isEmpty()); |
1056 |
> |
}}); |
1057 |
> |
|
1058 |
|
Thread.sleep(SHORT_DELAY_MS); |
1059 |
|
assertEquals(2, q.size()); |
1060 |
|
assertEquals(four, q.poll()); |
1061 |
|
assertEquals(five, q.poll()); |
1062 |
|
assertTrue(q.isEmpty()); |
1063 |
+ |
t.join(); |
1064 |
|
} |
1065 |
|
|
1066 |
|
/** |
1071 |
|
final LinkedTransferQueue q = new LinkedTransferQueue(); |
1072 |
|
assertTrue(q.offer(four)); |
1073 |
|
assertEquals(1, q.size()); |
1074 |
< |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
1074 |
> |
assertFalse(q.tryTransfer(five, SHORT_DELAY_MS, MILLISECONDS)); |
1075 |
|
assertEquals(1, q.size()); |
1076 |
|
assertEquals(four, q.poll()); |
1077 |
|
threadAssertTrue(q.isEmpty()); |