1 22 package org.jboss.test.cmp2.cmrstress; 23 24 import javax.ejb.DuplicateKeyException ; 25 26 import junit.framework.Test; 27 28 import org.jboss.test.JBossTestCase; 29 import org.jboss.test.cmp2.cmrstress.interfaces.Parent; 30 import org.jboss.test.cmp2.cmrstress.interfaces.ParentUtil; 31 32 33 34 50 public class CMRStressTestCase extends JBossTestCase 51 { 52 53 59 public static class CMRTest implements Runnable 60 { 61 public CMRTest(String parentpk, int loops) throws Exception 62 { 63 mLoops = loops; 64 mParent = ParentUtil.getHome().findByPrimaryKey(parentpk); 65 } 66 67 public void run() 68 { 69 for (int i = 0; i < mLoops; ++i) 71 { 72 try 73 { 74 Object map = mParent.getPropertyMap(); 77 } 78 catch (Throwable e) 79 { 80 System.out.println(Thread.currentThread().getName()); 81 e.printStackTrace(); 82 mUnexpected = e; 83 } 84 } 85 synchronized(mLock) 86 { 87 ++mCompleted; 88 System.out.println("Completed " + mCompleted + " of " + mTarget); 89 mLock.notifyAll(); 90 } 91 } 92 93 private final int mLoops; 94 private final Parent mParent; 95 96 } 97 98 100 102 104 public static Test suite() 105 throws Exception 106 { 107 return getDeploySetup(CMRStressTestCase.class, "cmp2-cmrstress.jar"); 108 } 109 110 112 public CMRStressTestCase(String name) 113 throws java.io.IOException 114 { 115 super(name); 116 } 117 118 120 124 public void testRelations() throws Exception 125 { 126 mTarget = getThreadCount(); 127 getLog().info("testRelations started, count="+mTarget); 128 for (int i = 0; i < mTarget; ++i) 129 { 130 Thread thread = new Thread (new CMRTest(PARENT_PK, getIterationCount()), "CMRTestThread-" + i); 131 thread.start(); 132 getLog().info("Started thread: "+thread); 133 } 134 waitForCompletion(); 135 136 getLog().info("testRelations finished"); 137 } 138 139 140 142 145 protected void setUp() throws Exception 146 { 147 super.setUp(); 148 149 Parent parent; 151 try 152 { 153 parent = ParentUtil.getHome().create(PARENT_PK); 154 155 for (int i = 0; i < getBeanCount(); ++i) 157 parent.addChild(i, CHILD_FIELD1 + i, CHILD_FIELD2 + i); 158 159 } 160 catch (DuplicateKeyException e) 161 { 162 getLog().info("Parent bean already exists"); 163 parent = ParentUtil.getHome().findByPrimaryKey(PARENT_PK); 164 165 } 167 catch (Exception e) 168 { 169 getLog().error("Failed to create parent bean", e); 170 throw e; 171 } 172 } 173 174 175 177 private void waitForCompletion() throws Exception 178 { 179 getLog().debug("Waiting for completion"); 180 synchronized (mLock) 181 { 182 while (mCompleted < mTarget) 183 { 184 mLock.wait(); 185 if (mUnexpected != null) 186 { 187 getLog().error("Unexpected exception", mUnexpected); 188 fail("Unexpected exception"); 189 } 190 } 191 } 192 } 193 194 private static final String PARENT_PK = "arbitrary pk"; 195 private static final String CHILD_FIELD1 = "dummy field 1 - "; 196 private static final String CHILD_FIELD2 = "dummy field 2 - "; 197 198 private static final Object mLock = new Object (); 199 private static int mCompleted = 0; 200 private static int mTarget; 201 private static Throwable mUnexpected; 202 } 203 | Popular Tags |