*/
public void testDeadlock() throws Exception
{
final AnyMessage mesg = new AnyMessage();
final AtomicBoolean received = new AtomicBoolean(false);
final AtomicBoolean delivered = new AtomicBoolean(false);
final CountDownLatch threadsDone = new CountDownLatch(1);
final CountDownLatch getPutOrder = new CountDownLatch(1);
final AtomicReference getException = new AtomicReference(null);
final AtomicReference putException = new AtomicReference(null);
final CyclicBarrier barrier = new CyclicBarrier(2, new Runnable()
{
public void run()
{
threadsDone.countDown();
}
});
Runnable getCommand = new Runnable()
{
public void run()
{
try
{
synchronized (lock_)
{
getPutOrder.countDown();
objectUnderTest_.getMessageBlocking();
}
received.set(true);
} catch (Exception e)
{
getException.set(e);
}
finally
{
try
{
barrier.await();
} catch (InterruptedException e)
{
// ignored
} catch (BrokenBarrierException e)
{
// should not happen
e.printStackTrace();
}
}
}
};
Runnable putCommand = new Runnable()
{
public void run()
{
try
{
getPutOrder.await();
objectUnderTest_.enqeue(mesg.getHandle());
delivered.set(true);
} catch (Exception e)
{
putException.set(e);
}
finally
{
try
{
barrier.await();
} catch (InterruptedException e)
{
// ignored
} catch (BrokenBarrierException e)
{
// should not happen
e.printStackTrace();
}
}
}
};
Thread getter = new Thread(getCommand);
getter.setDaemon(true);
Thread putter = new Thread(putCommand);
putter.setDaemon(true);
getter.start();
putter.start();
threadsDone.await(1000, TimeUnit.MILLISECONDS);
assertTrue(delivered.get());
assertTrue(received.get());
assertNull(putException.get());
assertNull(getException.get());