KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > stress > ConcurrencyTestCase


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 test.stress;
23
24 import java.util.Random JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanServerFactory JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Stresses concurrent use of the MBeanServer for registration, querying.
39  * @author Scott.Stark@jboss.org
40  * @version $Revision: 37459 $
41  */

42 public class ConcurrencyTestCase
43    extends TestCase
44 {
45    // Constants ---------------------------------------------------------------
46
static final int N = 100000;
47    static final int Nadders = 2;
48    static final int NDomains = 1;
49    static final List JavaDoc names = Collections.synchronizedList(new LinkedList JavaDoc());
50
51    // Attributes --------------------------------------------------------------
52

53    /**
54     * The MBeanServer
55     */

56    private MBeanServer JavaDoc server;
57    private Random JavaDoc rnd;
58    private boolean adding;
59
60    // Constructor -------------------------------------------------------------
61

62    /**
63     * Construct the test
64     */

65    public ConcurrencyTestCase(String JavaDoc s)
66    {
67       super(s);
68    }
69
70    // Tests -------------------------------------------------------------------
71

72    public void testAddRemoveQuery() throws Exception JavaDoc
73    {
74       MBeanAdder[] adders = new MBeanAdder[Nadders];
75       Thread JavaDoc[] adderThreads = new Thread JavaDoc[Nadders];
76       MBeanRemover[] removers = new MBeanRemover[Nadders];
77       Thread JavaDoc[] removerThreads = new Thread JavaDoc[Nadders];
78       MBeanFinder finder = new MBeanFinder();
79       adding = true;
80       for(int n = 0; n < adders.length; n ++)
81       {
82          int minID = n * N / Nadders;
83          int maxID = (n+1) * N / Nadders;
84          adders[n] = new MBeanAdder(minID, maxID);
85          adderThreads[n] = new Thread JavaDoc(adders[n], "MBeanAdder#"+n);
86          adderThreads[n].start();
87       }
88       Thread JavaDoc t1 = new Thread JavaDoc(finder, "MBeanFinder");
89       t1.start();
90       for(int n = 0; n < adders.length; n ++)
91       {
92          removers[n] = new MBeanRemover();
93          removerThreads[n] = new Thread JavaDoc(removers[n], "MBeanRemover#"+n);
94          removerThreads[n].start();
95       }
96
97       for(int n = 0; n < adders.length; n ++)
98       {
99          adderThreads[n].join();
100       }
101       adding = false;
102       t1.join();
103       for(int n = 0; n < adders.length; n ++)
104       {
105          removerThreads[n].join();
106       }
107
108       for(int n = 0; n < adders.length; n ++)
109       {
110          assertNull("There was no exception in MBeanAdder#"+n, adders[n].getException());
111       }
112       assertNull("There was no exception in MBeanFinder", finder.getException());
113       for(int n = 0; n < adders.length; n ++)
114       {
115          assertNull("There was no exception in MBeanRemover#"+n, removers[n].getException());
116       }
117    }
118
119    // Support -----------------------------------------------------------------
120

121    /**
122     * Start a new test
123     */

124    protected void setUp()
125    {
126       server = MBeanServerFactory.createMBeanServer();
127       rnd = new Random JavaDoc();
128    }
129
130    /**
131     * End the test
132     */

133    protected void tearDown()
134       throws Exception JavaDoc
135    {
136       MBeanServerFactory.releaseMBeanServer(server);
137    }
138
139    /**
140     * Sleep for a bit
141     */

142    private void sleep(long time)
143    {
144       try
145       {
146          Thread.sleep(time);
147       }
148       catch (InterruptedException JavaDoc ignored)
149       {
150       }
151    }
152
153    class MBeanAdder implements Runnable JavaDoc
154    {
155       private int minID;
156       private int maxID;
157       private String JavaDoc domain;
158       private Properties JavaDoc nameProps = new Properties JavaDoc();
159       private Throwable JavaDoc ex;
160
161       MBeanAdder(int minID, int maxID)
162       {
163          this.minID = minID;
164          this.maxID = maxID;
165       }
166       public void run()
167       {
168          System.out.println("MBeanAdder, min="+minID+", max="+maxID+", starting");
169          nameProps.setProperty("type", "simple");
170          try
171          {
172             for(int n = 0; n < N; n ++)
173             {
174                int id = minID + n % maxID;
175                domain = "jboss.test."+ rnd.nextInt(NDomains);
176                nameProps.setProperty("id", "#"+id);
177                addMBean();
178             }
179          }
180          catch(Throwable JavaDoc t)
181          {
182             this.ex = t;
183             ex.printStackTrace();
184          }
185          System.out.println("MBeanAdder, min="+minID+", max="+maxID+", ending");
186       }
187       void addMBean() throws Exception JavaDoc
188       {
189          ObjectName JavaDoc name = new ObjectName JavaDoc(domain, nameProps);
190          if( server.isRegistered(name))
191             server.unregisterMBean(name);
192          Simple mbean = new Simple(name);
193          server.registerMBean(mbean, name);
194          names.add(name);
195       }
196       Throwable JavaDoc getException()
197       {
198          return ex;
199       }
200    }
201
202    class MBeanRemover implements Runnable JavaDoc
203    {
204       private Throwable JavaDoc ex;
205
206       public void run()
207       {
208          try
209          {
210             while( adding )
211             {
212                int max = names.size();
213                if( max == 0 )
214                {
215                   sleep(10);
216                   continue;
217                }
218                int index = rnd.nextInt(max);
219                try
220                {
221                   ObjectName JavaDoc name = (ObjectName JavaDoc) names.remove(index);
222                   server.unregisterMBean(name);
223                }
224                catch(IndexOutOfBoundsException JavaDoc ignore)
225                {
226                }
227             }
228          }
229          catch(Throwable JavaDoc t)
230          {
231             this.ex = t;
232             ex.printStackTrace();
233          }
234       }
235       Throwable JavaDoc getException()
236       {
237          return ex;
238       }
239    }
240
241    class MBeanFinder implements Runnable JavaDoc
242    {
243       private Throwable JavaDoc ex;
244       public void run()
245       {
246          try
247          {
248             ObjectName JavaDoc query = new ObjectName JavaDoc("jboss.test.*:type=simple,*");
249             int count = 0;
250             while( adding )
251             {
252                Set JavaDoc matches = server.queryNames(query, null);
253                count ++;
254                if( count % 1000 == 0 )
255                   System.out.println(count+" queries, names.size="+names.size()+", matches.size="+matches.size());
256             }
257          }
258          catch(Throwable JavaDoc t)
259          {
260             this.ex = t;
261             ex.printStackTrace();
262          }
263       }
264       Throwable JavaDoc getException()
265       {
266          return ex;
267       }
268    }
269
270    static interface SimpleMBean
271    {
272       public ObjectName JavaDoc getName();
273    }
274    static class Simple implements SimpleMBean
275    {
276       private ObjectName JavaDoc name;
277       Simple(ObjectName JavaDoc name)
278       {
279          this.name = name;
280       }
281       public ObjectName JavaDoc getName()
282       {
283          return name;
284       }
285    }
286 }
287
288
Popular Tags