public void testBlockingQueue()
throws Exception
{
BlockingQueue queue = new BlockingQueue();
Assert.assertEquals("initial capacity", 0, queue.size());
final int num = 5;
Integer[] ia = new Integer[num];
for(int i = 0; i < num; i++) {
ia[i] = new Integer(i);
queue.add(ia[i]);
Assert.assertEquals("add-loop: queue-size #" + i, i + 1, queue.size());
}
for(int i = 0; i < num; i++) {
Integer I = (Integer)queue.next();
Assert.assertEquals("next-loop: queue-size #" + i, num - i - 1, queue.size());
Assert.assertEquals("loop:object received", ia[i], I);
}
long t1 = System.currentTimeMillis();
new Adder(queue, ia[0], 3000);
Integer I = (Integer)queue.next();
long t2 = System.currentTimeMillis();
Assert.assertTrue("wait()-period", (t2 - t1) > 2500 && (t2 - t1) < 3500);
Assert.assertEquals("object after release", ia[0], I);
Assert.assertEquals("capacity after release", 0, queue.size());
Worker[] workers = new Worker[num];
for(int i = 0; i < num; i++) {
workers[i] = new Worker(queue, num * 500);
}
for(int i = 0; i < num; i++) {
queue.add(ia[i]);
Thread.sleep(500);
}
for(int i = 0; i < num; i++) {
queue.add(ia[i]);
Thread.sleep(500);
}
for(int i = 0; i < num; i++) {
Assert.assertEquals("count of worker#" + i, 2, workers[i].getCount());
}