public void testConcurrentClose() throws Exception
{
int THREAD_COUNT = 10;
final FailoverValve valve = new FailoverValve(10000);
final Slot[] slot = new Slot[THREAD_COUNT];
// prevent the valve from being possible to close
log.info("entering the valve");
valve.enter();
// attempt to close the valve from 10 concurrent threads
for(int i = 0; i < THREAD_COUNT; i++)
{
slot[i] = new Slot();
final int ii = i;
new Thread(new Runnable()
{
public void run()
{
try
{
log.info("attempting to close");
valve.close();
log.info("First thread could close the valve");
slot[ii].put("CLOSED");
valve.open();
log.info("Firs thread opened the Valve");
}
catch(InterruptedException e)
{
log.error(e);
}
}
}, "Closer(" + i + ")").start();
}
// wait a second so they'll attempt closing
log.info("sleeping for 2 seconds");
Thread.sleep(2000);
log.info("slept");
// make sure none closed the valve
for(int i = 0; i < THREAD_COUNT; i++)
{
Object o = slot[i].peek();
assertNull(o);
}
log.info("leaving the valve");
valve.leave();
// the valve should be "closeable", so all waiting threads, plus a new one, should be able
// to close it
final Slot loneSlot = new Slot();
new Thread(new Runnable()
{
public void run()
{
try
{
log.info("attempting to close");
valve.close();
log.info("Second thread could close the valve");
loneSlot.put("CLOSED");
}
catch(InterruptedException e)
{