KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > word > IssueSearchFactoryTest


1 package org.tigris.scarab.util.word;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import org.tigris.scarab.om.IssueType;
50 import org.tigris.scarab.om.Module;
51 import org.tigris.scarab.om.ScarabUser;
52 import org.tigris.scarab.test.BaseScarabTestCase;
53
54 /**
55  * A Testing Suite for the util.word.IssueSearchFactory class. This class
56  * includes concurrency tests, which have timings on a much shorter time
57  * scale than is possible in production. Attempts to shorten the maxWait
58  * even further causes tests to fail, just because threads to not necessarily
59  * wake up immediately on the notifyAll signal. if failures are seen, try
60  * adjusting maxWait used here up before assuming the code is broken. Max
61  * wait in production is measured in seconds, values in this class are
62  * given in millis.
63  *
64  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
65  * @version $Id: IssueSearchFactoryTest.java 9446 2005-03-12 11:59:50Z jorgeuriarte $
66  */

67 public class IssueSearchFactoryTest extends BaseScarabTestCase
68 {
69     private IssueSearch search;
70
71   
72     /**
73      * Testing the factory's limit using one thread
74      * can't test maxWait this way.
75      */

76     public void testSingleThread()
77         throws Exception JavaDoc
78     {
79         IssueSearchFactory issueSearchFactory = new IssueSearchFactory()
80             {
81                 int getMaxInstances()
82                 {
83                     return 5;
84                 }
85                 int getMaxWait()
86                 {
87                     return 0;
88                 }
89             };
90
91         Module module = getModule();
92         IssueType it = getDefaultIssueType();
93         ScarabUser user = getUser1();
94
95         IssueSearch[] isa = new IssueSearch[5];
96         for (int i=0; i<5; i++)
97         {
98             isa[i] = issueSearchFactory.getInstance(module, it, user);
99         }
100         
101         // try to get one more than max
102
try
103         {
104             issueSearchFactory.getInstance(module, it, getUser1());
105             fail("Created more than maxInstances");
106         }
107         catch (MaxConcurrentSearchException expected)
108         {
109         }
110
111         // let the factory know we are done
112
issueSearchFactory.notifyDone();
113
114         // try again should work this time
115
try
116         {
117             issueSearchFactory.getInstance(module, it, getUser1());
118         }
119         catch (MaxConcurrentSearchException failure)
120         {
121             fail("Could not create new instance after returning one.");
122         }
123     }
124
125     /**
126      * I can't seem to grok this one. I know I should have paid more attention to
127      * threads in Java 101.
128      * @throws Exception
129      */

130     public void OFFtestConcurrency()
131         throws Exception JavaDoc
132     {
133         String JavaDoc message = multipleThreads(1);
134         assertTrue(message, message.length() == 0);
135         message = multipleThreads(2 * 50); // 2 * maxWait
136
assertTrue("Didn't timeout. " + message,
137                    message.startsWith("Exception: "));
138     }
139
140     private String JavaDoc multipleThreads(final int holdTime)
141         throws Exception JavaDoc
142     {
143         final long startTime = System.currentTimeMillis();
144
145         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(20);
146
147         final ISFactoryTest[] pts = new ISFactoryTest[2 * 5]; // 2 * maxActive
148
final ThreadGroup JavaDoc threadGroup = new ThreadGroup JavaDoc("foo")
149             {
150                 public void uncaughtException(Thread JavaDoc t, Throwable JavaDoc e)
151                 {
152                     for (int i = 0; i < pts.length; i++)
153                     {
154                         pts[i].stop();
155                     }
156
157                     sb.append("Exception: " + e.getMessage());
158                 }
159             };
160
161         // would like to use variables here but an inner class does not
162
// initialize its version of the local variable until after the
163
// ctor is executed and these methods are called from the ctor
164
// we have to hardcode.
165
IssueSearchFactory issueSearchFactory = new IssueSearchFactory()
166             {
167
168                 int getMaxInstances()
169                 {
170                     return 5;
171                 }
172                 int getMaxWait()
173                 {
174                     return 50;
175                 }
176             };
177
178         Module module = getModule();
179         IssueType it = getDefaultIssueType();
180         ScarabUser user = getUser1();
181
182
183         for (int i = 0; i < pts.length; i++)
184         {
185             pts[i] = new ISFactoryTest(threadGroup, holdTime,
186                                        issueSearchFactory,
187                                        module, it, user);
188         }
189
190         // let threads run for a bit
191
Thread.sleep(1000);
192         for (int i = 0; i < pts.length; i++)
193         {
194             pts[i].stop();
195         }
196
197         // check for deadlock, give threads time to complete an iteration
198
Thread.sleep(200);
199         for (int i = 0; i < pts.length; i++)
200         {
201             //System.out.println(pts[i].getState());
202
if (!pts[i].getState().startsWith("Stopped"))
203             {
204                 sb.append("Possible deadlock. First try increasing sleep time.");
205             }
206         }
207
208         long time = System.currentTimeMillis() - startTime;
209         System.out.println("Multithread test time = " + time + " ms");
210         return sb.toString();
211     }
212
213     private static int currentThreadCount = 0;
214
215     private class ISFactoryTest implements Runnable JavaDoc
216     {
217         /**
218          * The number of milliseconds to hold the object
219          */

220         private final int isHoldTime;
221         private final Module module;
222         private final IssueType it;
223         private final ScarabUser user;
224         private final IssueSearchFactory isFactory;
225
226         private boolean isRun;
227
228         private String JavaDoc state;
229         private int runCounter;
230
231         protected ISFactoryTest(ThreadGroup JavaDoc threadGroup, int isHoldTime,
232                                 IssueSearchFactory isFactory,
233                                 Module module, IssueType it, ScarabUser user)
234         {
235             this.isHoldTime = isHoldTime;
236             this.module = module;
237             this.it = it;
238             this.user = user;
239             this.isFactory = isFactory;
240             Thread JavaDoc thread = new Thread JavaDoc(threadGroup, this,
241                                        "Thread+" + currentThreadCount++);
242             thread.setDaemon(false);
243             thread.start();
244         }
245
246         public void run()
247         {
248             isRun = true;
249             runCounter = 0;
250             while (isRun)
251             {
252                 runCounter++;
253                 try
254                 {
255                     IssueSearch is = null;
256                     state = "Getting IS";
257                     is = isFactory.getInstance(module, it, user);
258                     state = "Using IS";
259                     assertTrue(null != is);
260                     Thread.sleep(isHoldTime);
261                     state = "Returning IS";
262                     isFactory.notifyDone();
263                     // if we let this thread immediately enter into competition
264
// for the lock on the factory, it sometimes beats those
265
// that were waiting.
266
Thread.sleep(10);
267                 }
268                 catch (RuntimeException JavaDoc e)
269                 {
270                     throw e;
271                 }
272                 catch (Exception JavaDoc e)
273                 {
274                     throw new RuntimeException JavaDoc(e.toString());
275                 }
276             }
277             state = "Stopped";
278         }
279
280         public void stop()
281         {
282             isRun = false;
283         }
284         
285         public String JavaDoc getState()
286         {
287             return state + "; ran " + runCounter + " times";
288         }
289     }
290 }
291
292
Popular Tags