KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > test > AbstractConcurrentStressTest


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.jca.test;
23
24 import java.util.ArrayList JavaDoc;
25
26 import org.jboss.test.JBossTestCase;
27
28 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
29
30 /**
31  * Abstract concurrent stress test.
32  *
33  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
34  * @version $Revision: 37406 $
35  */

36 public class AbstractConcurrentStressTest extends JBossTestCase
37 {
38    private ArrayList JavaDoc done = new ArrayList JavaDoc();
39    private int total;
40    private Throwable JavaDoc failed = null;
41    private SynchronizedInt nextId = new SynchronizedInt(0);
42    
43    public interface ConcurrentTestCallback
44    {
45       void finished() throws Throwable JavaDoc;
46    }
47    
48    public void runConcurrentTest(ConcurrentRunnable[] runnables, ConcurrentTestCallback callback) throws Throwable JavaDoc
49    {
50       total = runnables.length;
51       Thread JavaDoc[] threads = new Thread JavaDoc[total];
52       for (int i = 0; i < total; ++i)
53       {
54          threads[i] = new Thread JavaDoc(runnables[i], getName() + "-" + runnables[i].id);
55          threads[i].start();
56       }
57       for (int i = 0; i < total; ++i)
58          threads[i].join();
59
60       if (callback != null)
61          callback.finished();
62       
63       if (failed != null)
64          throw failed;
65       
66       for (int i = 0; i < total; ++i)
67          runnables[i].doCheck();
68    }
69    
70    public abstract class ConcurrentRunnable implements Runnable JavaDoc
71    {
72       private Throwable JavaDoc failure;
73       protected int id;
74       
75       public ConcurrentRunnable()
76       {
77          id = nextId.increment();
78       }
79       
80       public abstract void doStart() throws Throwable JavaDoc;
81       public abstract void doRun() throws Throwable JavaDoc;
82       public abstract void doEnd() throws Throwable JavaDoc;
83       public synchronized void setFailure(Throwable JavaDoc failure)
84       {
85          if (failure != null)
86          {
87             this.failure = failure;
88             failed(failure);
89             log.error("Error in " + this, failure);
90          }
91          if (failure instanceof RuntimeException JavaDoc)
92             throw (RuntimeException JavaDoc) failure;
93          else if (failure instanceof Error JavaDoc)
94             throw (Error JavaDoc) failure;
95          throw new RuntimeException JavaDoc(failure);
96       }
97       public void doCheck() throws Throwable JavaDoc
98       {
99          if (failure != null)
100             throw failure;
101       }
102       
103       public void run()
104       {
105          try
106          {
107             doStart();
108          }
109          catch (Throwable JavaDoc t)
110          {
111             setFailure(t);
112          }
113          waitDone();
114          for (int i =0; i < getIterationCount(); ++i)
115          {
116             try
117             {
118                doRun();
119             }
120             catch (Throwable JavaDoc t)
121             {
122                setFailure(t);
123                break;
124             }
125          }
126          waitDone();
127          try
128          {
129             doEnd();
130          }
131          catch (Throwable JavaDoc t)
132          {
133             setFailure(t);
134          }
135          waitDone();
136       }
137    }
138
139    protected synchronized void failed(Throwable JavaDoc failure)
140    {
141       if (failed == null)
142          failed = failure;
143       for (int i = 0; i < done.size(); ++i)
144          done.get(i).notify();
145       done.clear();
146    }
147    
148    protected synchronized void waitDone()
149    {
150       if (failed != null)
151          return;
152       if (done.size() < total - 1)
153       {
154          done.add(this);
155          doWait();
156       }
157       else
158       {
159          for (int i = 0; i < done.size(); ++i)
160             done.get(i).notify();
161          done.clear();
162       }
163    }
164
165    protected void doWait()
166    {
167       boolean interrupted = false;
168       try
169       {
170          while (failed == null)
171          {
172             try
173             {
174                wait();
175                return;
176             }
177             catch (InterruptedException JavaDoc e)
178             {
179                interrupted = true;
180             }
181          }
182       }
183       finally
184       {
185          if (interrupted)
186             Thread.currentThread().interrupt();
187       }
188       
189    }
190    
191    public AbstractConcurrentStressTest(String JavaDoc name)
192    {
193       super(name);
194    }
195 }
196
Popular Tags