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.tm.test;
023:
024: import java.util.ArrayList;
025:
026: import org.jboss.test.JBossTestCase;
027:
028: /**
029: * Abstract concurrent stress test.
030: *
031: * @author <a href="adrian@jboss.com">Adrian Brock</a>
032: * @version $Revision: 57211 $
033: */
034: public class AbstractConcurrentStressTest extends JBossTestCase {
035: private ArrayList done = new ArrayList();
036: private int total;
037:
038: public interface ConcurrentTestCallback {
039: void finished() throws Throwable;
040: }
041:
042: public void runConcurrentTest(ConcurrentRunnable[] runnables,
043: ConcurrentTestCallback callback) throws Throwable {
044: total = runnables.length;
045: Thread[] threads = new Thread[total];
046: for (int i = 0; i < total; ++i) {
047: threads[i] = new Thread(runnables[i], getName() + "-" + i);
048: threads[i].start();
049: }
050: for (int i = 0; i < total; ++i)
051: threads[i].join();
052:
053: if (callback != null)
054: callback.finished();
055:
056: for (int i = 0; i < total; ++i)
057: runnables[i].doCheck();
058: }
059:
060: public abstract class ConcurrentRunnable implements Runnable {
061: public Throwable failure;
062:
063: public abstract void doStart();
064:
065: public abstract void doRun();
066:
067: public abstract void doEnd();
068:
069: public void doCheck() throws Throwable {
070: if (failure != null)
071: throw failure;
072: }
073:
074: public void run() {
075: doStart();
076: waitDone();
077: for (int i = 0; i < getIterationCount(); ++i)
078: doRun();
079: waitDone();
080: doEnd();
081: waitDone();
082: }
083: }
084:
085: protected synchronized void waitDone() {
086: if (done.size() < total - 1) {
087: done.add(this );
088: doWait();
089: } else {
090: for (int i = 0; i < done.size(); ++i)
091: done.get(i).notify();
092: done.clear();
093: }
094: }
095:
096: protected void doWait() {
097: boolean interrupted = false;
098: try {
099: while (true) {
100: try {
101: wait();
102: return;
103: } catch (InterruptedException e) {
104: interrupted = true;
105: }
106: }
107: } finally {
108: if (interrupted)
109: Thread.currentThread().interrupt();
110: }
111:
112: }
113:
114: public AbstractConcurrentStressTest(String name) {
115: super(name);
116: }
117: }
|