001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.lock.test;
023:
024: import java.rmi.*;
025:
026: import java.util.HashMap;
027: import java.util.StringTokenizer;
028:
029: import javax.ejb.FinderException;
030:
031: import javax.naming.Context;
032: import javax.naming.InitialContext;
033: import junit.framework.Assert;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.log4j.Category;
040:
041: import org.jboss.test.JBossTestCase;
042: import org.jboss.test.lock.interfaces.EnterpriseEntity;
043:
044: import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
045:
046: /**
047: * #Description of the Class
048: */
049: public abstract class EnterpriseEntityTest extends JBossTestCase {
050: /**
051: * Description of the Field
052: */
053: public final static int DEFAULT_THREAD_COUNT = 20;
054: /**
055: * Description of the Field
056: */
057: public final static int DEFAULT_ITERATIONS = 10;
058:
059: private String jndiname;
060:
061: /**
062: * The number of threads to test with.
063: */
064: private int nbThreads;
065: private int completedThreads;
066:
067: /**
068: * The number of iterations each thread will go through
069: */
070: private int iterations;
071:
072: private Worker[] threads;
073:
074: private HashMap param = new HashMap();
075:
076: private EnterpriseEntity entity;
077:
078: private boolean failed;
079:
080: /**
081: * Constructor for the EnterpriseEntityTest object
082: *
083: * @param name Description of Parameter
084: * @param jndiname Description of Parameter
085: */
086: public EnterpriseEntityTest(final String name, final String jndiname) {
087: super (name);
088: this .jndiname = jndiname;
089: }
090:
091: /**
092: * A unit test for JUnit
093: *
094: * @exception Exception Description of Exception
095: */
096: public void testSingleBean() throws Exception {
097: getLog().debug(
098: "Spawning " + nbThreads + " threads for " + iterations
099: + " iterations with single bean call");
100:
101: Task prototype = new Task() {
102: /**
103: * Main processing method for the EnterpriseEntityTest object
104: *
105: * @param name Description of Parameter
106: * @param i Description of Parameter
107: * @exception Exception Description of Exception
108: */
109: public void run(String name, int i) throws Exception {
110: entity.setField(name + " i=" + i);
111: }
112: };
113:
114: run(prototype);
115: }
116:
117: /**
118: * A unit test for JUnit
119: *
120: * @exception Exception Description of Exception
121: */
122: public void testB2B() throws Exception {
123: getLog().debug(
124: "Spawning " + nbThreads + " threads for " + iterations
125: + " iterations with bean to bean call");
126:
127: entity.setNextEntity("daniel");
128:
129: Task prototype = new Task() {
130: /**
131: * Main processing method for the EnterpriseEntityTest object
132: *
133: * @param name Description of Parameter
134: * @param i Description of Parameter
135: * @exception Exception Description of Exception
136: */
137: public void run(String name, int i) throws Exception {
138: entity.setAndCopyField(name + " i=" + i);
139: }
140: };
141:
142: run(prototype);
143: }
144:
145: /**
146: * The JUnit setup method
147: *
148: * @exception Exception Description of Exception
149: */
150: protected void setUp() throws Exception {
151: nbThreads = getThreadCount();//DEFAULT_THREAD_COUNT;
152: iterations = getIterationCount();//DEFAULT_ITERATIONS;
153: getLog().debug(
154: "+++ Setting up: " + getClass().getName() + " test: "
155: + getName());
156: EnterpriseEntityHome home = (EnterpriseEntityHome) getInitialContext()
157: .lookup(jndiname);
158:
159: try {
160: entity = home.findByPrimaryKey("seb");
161: } catch (FinderException e) {
162: entity = home.create("seb");
163: }
164:
165: // setup the threads
166: threads = new Worker[nbThreads];
167: }
168:
169: /**
170: * Sets the Failed attribute of the EnterpriseEntityTest object
171: */
172: protected synchronized void setFailed() {
173: failed = true;
174: }
175:
176: /**
177: * #Description of the Method
178: *
179: * @param prototype Description of Parameter
180: * @exception Exception Description of Exception
181: */
182: protected void startAll(Task prototype) throws Exception {
183: completedThreads = 0;
184: for (int i = 0; i < nbThreads; i++) {
185: Task task = (Task) prototype.clone();
186: threads[i] = new Worker("Thread #" + (i + 1), task,
187: getLog());
188: threads[i].start();
189: }
190: }
191:
192: /**
193: * #Description of the Method
194: *
195: * @exception Exception Description of Exception
196: */
197: protected void joinAll() throws Exception {
198: // wait for all the threads to finish
199: for (int i = 0; i < nbThreads; i++) {
200: threads[i].join();
201: }
202: }
203:
204: /**
205: * Main processing method for the EnterpriseEntityTest object
206: *
207: * @param prototype Description of Parameter
208: * @exception Exception Description of Exception
209: */
210: protected void run(Task prototype) throws Exception {
211: startAll(prototype);
212: joinAll();
213: assertTrue(!failed);
214: }
215:
216: /**
217: * #Description of the Method
218: *
219: * @return Description of the Returned Value
220: */
221: protected boolean hasFailed() {
222: return failed;
223: }
224:
225: /////////////////////////////////////////////////////////////////////////
226: // Iteration Worker & Task //
227: /////////////////////////////////////////////////////////////////////////
228:
229: /**
230: * #Description of the Class
231: */
232: public abstract class Task implements Cloneable {
233: /**
234: * Main processing method for the Task object
235: *
236: * @param name Description of Parameter
237: * @param i Description of Parameter
238: * @exception Exception Description of Exception
239: */
240: public abstract void run(String name, int i) throws Exception;
241:
242: /**
243: * #Description of the Method
244: *
245: * @return Description of the Returned Value
246: */
247: public Object clone() {
248: try {
249: return super .clone();
250: } catch (CloneNotSupportedException e) {
251: throw new InternalError();
252: }
253: }
254: }
255:
256: /**
257: * #Description of the Class
258: */
259: public class Worker extends Thread {
260: /**
261: * Description of the Field
262: */
263: public String name;
264: /**
265: * Description of the Field
266: */
267: public boolean running;
268: /**
269: * Description of the Field
270: */
271: public Task task;
272:
273: private Category log;
274:
275: /**
276: * Constructor for the Worker object
277: *
278: * @param name Description of Parameter
279: * @param task Description of Parameter
280: * @param log Description of Parameter
281: */
282: public Worker(final String name, final Task task, Category log) {
283: this .name = name;
284: this .task = task;
285: this .log = log;
286: running = true;
287: }
288:
289: /**
290: * Main processing method for the Worker object
291: */
292: public void run() {
293: long start = System.currentTimeMillis();
294: int i;
295:
296: for (i = 0; i < iterations; i++) {
297: if (!running || hasFailed()) {
298: break;
299: }
300:
301: try {
302: task.run(name, i);
303: //log.debug(name + " " + (i+1) + " iterations done");
304: } catch (Throwable t) {
305: log.error(name + " caught an exception, dying", t);
306: t.printStackTrace();
307: running = false;
308: setFailed();
309: }
310: }
311:
312: synchronized (this ) {
313: completedThreads++;
314: }
315: long end = System.currentTimeMillis();
316: log.debug(name + ": did " + i + " iterations in "
317: + (end - start) + "ms, complete="
318: + completedThreads);
319: }
320: }
321: }
|