KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > misc > TestingUtil


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.cache.misc;
9
10 import org.jboss.cache.Cache;
11 import org.jboss.cache.CacheImpl;
12 import org.jboss.cache.CacheSPI;
13
14 import java.io.File JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Random JavaDoc;
17
18 /**
19  * Utilities for unit testing JBossCache.
20  *
21  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
22  * @version $Revision$
23  */

24 public class TestingUtil
25 {
26
27    private static Random JavaDoc random = new Random JavaDoc();
28
29    /**
30     * Loops, continually calling {@link #areCacheViewsComplete(CacheSPI[])}
31     * until it either returns true or <code>timeout</code> ms have elapsed.
32     *
33     * @param caches caches which must all have consistent views
34     * @param timeout max number of ms to loop
35     * @throws RuntimeException if <code>timeout</code> ms have elapse without
36     * all caches having the same number of members.
37     */

38    public static void blockUntilViewsReceived(Cache[] caches, long timeout)
39    {
40       long failTime = System.currentTimeMillis() + timeout;
41
42       while (System.currentTimeMillis() < failTime)
43       {
44          sleepThread(100);
45          if (areCacheViewsComplete(caches))
46          {
47             return;
48          }
49       }
50
51       throw new RuntimeException JavaDoc("timed out before caches had complete views");
52    }
53
54    /**
55     * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
56     * until it either returns true or <code>timeout</code> ms have elapsed.
57     *
58     * @param caches caches which must all have consistent views
59     * @param timeout max number of ms to loop
60     * @throws RuntimeException if <code>timeout</code> ms have elapse without
61     * all caches having the same number of members.
62     */

63    public static void blockUntilViewsReceived(CacheImpl[] caches, long timeout)
64    {
65       long failTime = System.currentTimeMillis() + timeout;
66
67       while (System.currentTimeMillis() < failTime)
68       {
69          sleepThread(100);
70          if (areCacheViewsComplete(caches))
71          {
72             return;
73          }
74       }
75
76       throw new RuntimeException JavaDoc("timed out before caches had complete views");
77    }
78
79    /**
80     * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
81     * until it either returns true or <code>timeout</code> ms have elapsed.
82     *
83     * @param groupSize number of caches expected in the group
84     * @param timeout max number of ms to loop
85     * @throws RuntimeException if <code>timeout</code> ms have elapse without
86     * all caches having the same number of members.
87     */

88    public static void blockUntilViewReceived(CacheSPI cache, int groupSize, long timeout)
89    {
90       long failTime = System.currentTimeMillis() + timeout;
91
92       while (System.currentTimeMillis() < failTime)
93       {
94          sleepThread(100);
95          if (isCacheViewComplete(cache, groupSize))
96          {
97             return;
98          }
99       }
100
101       throw new RuntimeException JavaDoc("timed out before caches had complete views");
102    }
103
104    /**
105     * Loops, continually calling {@link #areCacheViewsComplete(CacheImpl[])}
106     * until it either returns true or <code>timeout</code> ms have elapsed.
107     *
108     * @param groupSize number of caches expected in the group
109     * @param timeout max number of ms to loop
110     * @throws RuntimeException if <code>timeout</code> ms have elapse without
111     * all caches having the same number of members.
112     */

113    public static void blockUntilViewReceived(CacheImpl cache, int groupSize, long timeout)
114    {
115       long failTime = System.currentTimeMillis() + timeout;
116
117       while (System.currentTimeMillis() < failTime)
118       {
119          sleepThread(100);
120          if (isCacheViewComplete(cache, groupSize))
121          {
122             return;
123          }
124       }
125
126       throw new RuntimeException JavaDoc("timed out before caches had complete views");
127    }
128
129    /**
130     * Checks each cache to see if the number of elements in the array
131     * returned by {@link CacheSPI#getMembers()} matches the size of
132     * the <code>caches</code> parameter.
133     *
134     * @param caches caches that should form a View
135     * @return <code>true</code> if all caches have
136     * <code>caches.length</code> members; false otherwise
137     * @throws IllegalStateException if any of the caches have MORE view
138     * members than caches.length
139     */

140    public static boolean areCacheViewsComplete(Cache[] caches)
141    {
142       int memberCount = caches.length;
143
144       for (int i = 0; i < memberCount; i++)
145       {
146          if (!isCacheViewComplete(caches[i], memberCount))
147          {
148             return false;
149          }
150       }
151
152       return true;
153    }
154
155    /**
156     * Checks each cache to see if the number of elements in the array
157     * returned by {@link CacheImpl#getMembers()} matches the size of
158     * the <code>caches</code> parameter.
159     *
160     * @param caches caches that should form a View
161     * @return <code>true</code> if all caches have
162     * <code>caches.length</code> members; false otherwise
163     * @throws IllegalStateException if any of the caches have MORE view
164     * members than caches.length
165     */

166    public static boolean areCacheViewsComplete(CacheImpl[] caches)
167    {
168       int memberCount = caches.length;
169
170       for (int i = 0; i < memberCount; i++)
171       {
172          CacheImpl cache = caches[i];
173          return isCacheViewComplete(cache, memberCount);
174       }
175
176       return true;
177    }
178
179    /**
180     * @param cache
181     * @param memberCount
182     */

183    public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
184    {
185       List JavaDoc members = cache.getMembers();
186       if (members == null || memberCount > members.size())
187       {
188          return false;
189       }
190       else if (memberCount < members.size())
191       {
192          // This is an exceptional condition
193
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Cache at address ");
194          sb.append(cache.getLocalAddress());
195          sb.append(" had ");
196          sb.append(members.size());
197          sb.append(" members; expecting ");
198          sb.append(memberCount);
199          sb.append(". Members were (");
200          for (int j = 0; j < members.size(); j++)
201          {
202             if (j > 0)
203             {
204                sb.append(", ");
205             }
206             sb.append(members.get(j));
207          }
208          sb.append(')');
209
210          throw new IllegalStateException JavaDoc(sb.toString());
211       }
212
213       return true;
214    }
215
216    /**
217     * @param cache
218     * @param memberCount
219     */

220    public static boolean isCacheViewComplete(Cache c, int memberCount)
221    {
222       CacheSPI cache = (CacheSPI) c;
223       List JavaDoc members = cache.getMembers();
224       if (members == null || memberCount > members.size())
225       {
226          return false;
227       }
228       else if (memberCount < members.size())
229       {
230          // This is an exceptional condition
231
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Cache at address ");
232          sb.append(cache.getLocalAddress());
233          sb.append(" had ");
234          sb.append(members.size());
235          sb.append(" members; expecting ");
236          sb.append(memberCount);
237          sb.append(". Members were (");
238          for (int j = 0; j < members.size(); j++)
239          {
240             if (j > 0)
241             {
242                sb.append(", ");
243             }
244             sb.append(members.get(j));
245          }
246          sb.append(')');
247
248          throw new IllegalStateException JavaDoc(sb.toString());
249       }
250
251       return true;
252    }
253
254
255    /**
256     * Puts the current thread to sleep for the desired number of ms, suppressing
257     * any exceptions.
258     *
259     * @param sleeptime number of ms to sleep
260     */

261    public static void sleepThread(long sleeptime)
262    {
263       try
264       {
265          Thread.sleep(sleeptime);
266       }
267       catch (InterruptedException JavaDoc ie)
268       {
269       }
270    }
271
272    public static void sleepRandom(int maxTime)
273    {
274       sleepThread(random.nextInt(maxTime));
275    }
276
277    public static void recursiveFileRemove(String JavaDoc directoryName)
278    {
279       File JavaDoc file = new File JavaDoc(directoryName);
280       if (file.exists())
281       {
282          System.out.println("Deleting file " + file);
283          recursivedelete(file);
284       }
285    }
286
287    private static void recursivedelete(File JavaDoc f)
288    {
289       if (f.isDirectory())
290       {
291          File JavaDoc[] files = f.listFiles();
292          for (File JavaDoc file : files)
293          {
294             recursivedelete(file);
295          }
296       }
297       //System.out.println("File " + f.toURI() + " deleted = " + f.delete());
298
f.delete();
299    }
300 }
301
Popular Tags