KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > cmrstress > CMRStressTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.cmrstress;
23
24 import javax.ejb.DuplicateKeyException JavaDoc;
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 /**
35  * A test suite designed to stress test CMR operations by invoking lots of them
36  * at around the same time from multiple threads.
37  *
38  * In particular, we are testing the ability to load all the beans on the many
39  * side of 1..many relationship, where the getter has been
40  * marked <code>read-only</code>.
41  *
42  * This code is based upon the original test case provided by Andrew May.
43  *
44  * @see org.jboss.test.JBossTestCase.
45  *
46  * @version <tt>$Revision: 37406 $</tt>
47  * @author <a HREF="mailto:steve@resolvesw.com">Steve Coy</a>.
48  *
49  */

50 public class CMRStressTestCase extends JBossTestCase
51 {
52
53    /**
54     * This nested <code>Runnable</code> simply invokes a method of the specified
55     * parent bean many times in a loop.
56     * The parent bean method accesses its CMR collection of child beans and
57     * iterates over it.
58     */

59    public static class CMRTest implements Runnable JavaDoc
60    {
61       public CMRTest(String JavaDoc parentpk, int loops) throws Exception JavaDoc
62       {
63          mLoops = loops;
64          mParent = ParentUtil.getHome().findByPrimaryKey(parentpk);
65       }
66
67       public void run()
68       {
69          // Access the getter the specified number of times.
70
for (int i = 0; i < mLoops; ++i)
71          {
72             try
73             {
74                // The following getter starts a new transaction
75
// and accesses its CMR collection.
76
Object JavaDoc map = mParent.getPropertyMap();
77             }
78             catch (Throwable JavaDoc 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    // Constants -----------------------------------------------------
99

100    // Attributes ----------------------------------------------------
101

102    // Static --------------------------------------------------------
103

104    public static Test suite()
105       throws Exception JavaDoc
106    {
107       return getDeploySetup(CMRStressTestCase.class, "cmp2-cmrstress.jar");
108    }
109
110    // Constructors --------------------------------------------------
111

112    public CMRStressTestCase(String JavaDoc name)
113       throws java.io.IOException JavaDoc
114    {
115       super(name);
116    }
117
118    // Public --------------------------------------------------------
119

120    /**
121     * Launch threads which access the CMR collection of the parent bean created below.
122     * @see #setup.
123     */

124    public void testRelations() throws Exception JavaDoc
125    {
126       mTarget = getThreadCount();
127       getLog().info("testRelations started, count="+mTarget);
128       for (int i = 0; i < mTarget; ++i)
129       {
130          Thread JavaDoc thread = new Thread JavaDoc(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    // Protected -------------------------------------------------------
141

142    /* (non-Javadoc)
143     * @see junit.framework.TestCase#setUp()
144     */

145    protected void setUp() throws Exception JavaDoc
146    {
147       super.setUp();
148       
149       // Create a parent bean
150
Parent parent;
151       try
152       {
153          parent = ParentUtil.getHome().create(PARENT_PK);
154
155          // Create some child beans
156
for (int i = 0; i < getBeanCount(); ++i)
157             parent.addChild(i, CHILD_FIELD1 + i, CHILD_FIELD2 + i);
158
159       }
160       catch (DuplicateKeyException JavaDoc e)
161       {
162          getLog().info("Parent bean already exists");
163          parent = ParentUtil.getHome().findByPrimaryKey(PARENT_PK);
164          
165          // We'll assume that the tables were not removed before and keep going...
166
}
167       catch (Exception JavaDoc e)
168       {
169          getLog().error("Failed to create parent bean", e);
170          throw e;
171       }
172    }
173
174    
175    // Private -------------------------------------------------------
176

177    private void waitForCompletion() throws Exception JavaDoc
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 JavaDoc PARENT_PK = "arbitrary pk";
195    private static final String JavaDoc CHILD_FIELD1 = "dummy field 1 - ";
196    private static final String JavaDoc CHILD_FIELD2 = "dummy field 2 - ";
197    
198    private static final Object JavaDoc mLock = new Object JavaDoc();
199    private static int mCompleted = 0;
200    private static int mTarget;
201    private static Throwable JavaDoc mUnexpected;
202 }
203
Popular Tags