Unit Testing for Blocking
Written by Brian Goetz in
“Java Concurency”
Methods called from one thread
void testPutThenTake() throws InterruptedException {
BoundedBlockingQueue<integer>
buf
=new BoundedBlockingQueue<Queue>
(1);
buf.put(42);
assertEquals(42, buf.take());
}
void testPutPutTakeTake() throws
InterruptedException {
BoundedBlockingQueue<Queue>
buf
=new BoundedBlockQueue<Integer>(1);
buf.put(42);
buf.put(17);
assertEquals(42, buf.take());
assertEquals(17, buf.take());
}
Methods called by different threads
void testPutPutTakeTake() throws
InterruptedException {
final BoundedBlockingQueue<Integer>
buf
=new BoundedBlockingQueue<integer>
(1)
Thread t = new Thread() {
public void run() {
assertEquals(42, buf.take());
assertEquals(17, buf.take());
}}
t.start();
buf.put(42);
buf.put(17);
t.join();
}