package org.jboss.test.cmp2.cmrstress;
import javax.ejb.DuplicateKeyException;
import junit.framework.Test;
import org.jboss.test.JBossTestCase;
import org.jboss.test.cmp2.cmrstress.interfaces.Parent;
import org.jboss.test.cmp2.cmrstress.interfaces.ParentUtil;
public class CMRStressTestCase extends JBossTestCase
{
public static class CMRTest implements Runnable
{
public CMRTest(String parentpk, int loops) throws Exception
{
mLoops = loops;
mParent = ParentUtil.getHome().findByPrimaryKey(parentpk);
}
public void run()
{
for (int i = 0; i < mLoops; ++i)
{
try
{
Object map = mParent.getPropertyMap();
}
catch (Throwable e)
{
System.out.println(Thread.currentThread().getName());
e.printStackTrace();
mUnexpected = e;
}
}
synchronized(mLock)
{
++mCompleted;
System.out.println("Completed " + mCompleted + " of " + mTarget);
mLock.notifyAll();
}
}
private final int mLoops;
private final Parent mParent;
}
public static Test suite()
throws Exception
{
return getDeploySetup(CMRStressTestCase.class, "cmp2-cmrstress.jar");
}
public CMRStressTestCase(String name)
throws java.io.IOException
{
super(name);
}
public void testRelations() throws Exception
{
mTarget = getThreadCount();
getLog().info("testRelations started, count="+mTarget);
for (int i = 0; i < mTarget; ++i)
{
Thread thread = new Thread(new CMRTest(PARENT_PK, getIterationCount()), "CMRTestThread-" + i);
thread.start();
getLog().info("Started thread: "+thread);
}
waitForCompletion();
getLog().info("testRelations finished");
}
protected void setUp() throws Exception
{
super.setUp();
Parent parent;
try
{
parent = ParentUtil.getHome().create(PARENT_PK);
for (int i = 0; i < getBeanCount(); ++i)
parent.addChild(i, CHILD_FIELD1 + i, CHILD_FIELD2 + i);
}
catch (DuplicateKeyException e)
{
getLog().info("Parent bean already exists");
parent = ParentUtil.getHome().findByPrimaryKey(PARENT_PK);
}
catch (Exception e)
{
getLog().error("Failed to create parent bean", e);
throw e;
}
}
private void waitForCompletion() throws Exception
{
getLog().debug("Waiting for completion");
synchronized (mLock)
{
while (mCompleted < mTarget)
{
mLock.wait();
if (mUnexpected != null)
{
getLog().error("Unexpected exception", mUnexpected);
fail("Unexpected exception");
}
}
}
}
private static final String PARENT_PK = "arbitrary pk";
private static final String CHILD_FIELD1 = "dummy field 1 - ";
private static final String CHILD_FIELD2 = "dummy field 2 - ";
private static final Object mLock = new Object();
private static int mCompleted = 0;
private static int mTarget;
private static Throwable mUnexpected;
}