public void testEjbRemoveMultiThread() throws Exception
{
CounterSessionHome counterHome = (CounterSessionHome)getInitialContext().lookup("CounterSession");
CountedSessionHome countedHome = (CountedSessionHome)getInitialContext().lookup("CountedSession2");
CounterSession counter = counterHome.create();
counter.clearCounters();
final CountedSession counted = countedHome.create();
Runnable runnable = new Runnable() {
public void run()
{
try
{
// introduce 1sec delay
counted.doSomething(1000);
}
catch (RemoteException e)
{
// ignore
}
}
};
for (int i = 0; i < 10; i++)
{
new Thread(runnable).start();
}
// since the session pool is Maximum==5, using 10 concurrent
// requests ensures at least 5 instances will have to be created
// (ejbCreate() to handle the load. Those 5 extra instances, will also have
// to be destroyed (ejbRemove()) upon return, because the pool will
// only store the first 5
// wait for all 10 threads to finish
Thread.sleep(2000);
assertTrue("createCounter >= 5", counter.getCreateCounter() >= 5);
assertTrue("removeCounter == 5", counter.getRemoveCounter() == 5);
}