1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
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; |
15 |
| import java.util.List; |
16 |
| import java.util.Random; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| public class TestingUtil |
25 |
| { |
26 |
| |
27 |
| private static Random random = new Random(); |
28 |
| public static final Object ALWAYS_EQUALS_OBJECT = new Object() |
29 |
| { |
30 |
0
| public boolean equals()
|
31 |
| { |
32 |
0
| return true;
|
33 |
| } |
34 |
| }; |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
98
| public static void blockUntilViewsReceived(Cache[] caches, long timeout)
|
46 |
| { |
47 |
98
| long failTime = System.currentTimeMillis() + timeout;
|
48 |
| |
49 |
98
| while (System.currentTimeMillis() < failTime)
|
50 |
| { |
51 |
98
| sleepThread(100);
|
52 |
97
| if (areCacheViewsComplete(caches))
|
53 |
| { |
54 |
97
| return;
|
55 |
| } |
56 |
| } |
57 |
| |
58 |
0
| throw new RuntimeException("timed out before caches had complete views");
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
31
| public static void blockUntilViewsReceived(long timeout, Cache... caches)
|
65 |
| { |
66 |
31
| blockUntilViewsReceived(caches, timeout);
|
67 |
| } |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
66
| public static void blockUntilViewsReceived(CacheImpl[] caches, long timeout)
|
79 |
| { |
80 |
66
| long failTime = System.currentTimeMillis() + timeout;
|
81 |
| |
82 |
148
| while (System.currentTimeMillis() < failTime)
|
83 |
| { |
84 |
148
| sleepThread(100);
|
85 |
148
| if (areCacheViewsComplete(caches))
|
86 |
| { |
87 |
66
| return;
|
88 |
| } |
89 |
| } |
90 |
| |
91 |
0
| throw new RuntimeException("timed out before caches had complete views");
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
3
| public static void blockUntilViewsReceived(long timeout, boolean barfIfTooManyMembers, Cache... caches)
|
105 |
| { |
106 |
3
| long failTime = System.currentTimeMillis() + timeout;
|
107 |
| |
108 |
3
| while (System.currentTimeMillis() < failTime)
|
109 |
| { |
110 |
3
| sleepThread(100);
|
111 |
3
| if (areCacheViewsComplete(caches, barfIfTooManyMembers))
|
112 |
| { |
113 |
3
| return;
|
114 |
| } |
115 |
| } |
116 |
| |
117 |
0
| throw new RuntimeException("timed out before caches had complete views");
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
0
| public static void blockUntilViewReceived(CacheSPI cache, int groupSize, long timeout)
|
130 |
| { |
131 |
0
| long failTime = System.currentTimeMillis() + timeout;
|
132 |
| |
133 |
0
| while (System.currentTimeMillis() < failTime)
|
134 |
| { |
135 |
0
| sleepThread(100);
|
136 |
0
| if (isCacheViewComplete(cache, groupSize))
|
137 |
| { |
138 |
0
| return;
|
139 |
| } |
140 |
| } |
141 |
| |
142 |
0
| throw new RuntimeException("timed out before caches had complete views");
|
143 |
| } |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
0
| public static void blockUntilViewReceived(CacheImpl cache, int groupSize, long timeout)
|
155 |
| { |
156 |
0
| long failTime = System.currentTimeMillis() + timeout;
|
157 |
| |
158 |
0
| while (System.currentTimeMillis() < failTime)
|
159 |
| { |
160 |
0
| sleepThread(100);
|
161 |
0
| if (isCacheViewComplete(cache, groupSize))
|
162 |
| { |
163 |
0
| return;
|
164 |
| } |
165 |
| } |
166 |
| |
167 |
0
| throw new RuntimeException("timed out before caches had complete views");
|
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
246
| public static boolean areCacheViewsComplete(Cache[] caches)
|
182 |
| { |
183 |
246
| return areCacheViewsComplete(caches, true);
|
184 |
| } |
185 |
| |
186 |
249
| public static boolean areCacheViewsComplete(Cache[] caches, boolean barfIfTooManyMembers)
|
187 |
| { |
188 |
249
| int memberCount = caches.length;
|
189 |
| |
190 |
249
| for (int i = 0; i < memberCount; i++)
|
191 |
| { |
192 |
487
| if (!isCacheViewComplete(caches[i], memberCount, barfIfTooManyMembers))
|
193 |
| { |
194 |
82
| return false;
|
195 |
| } |
196 |
| } |
197 |
| |
198 |
166
| return true;
|
199 |
| } |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
148
| public static boolean areCacheViewsComplete(CacheImpl[] caches)
|
213 |
| { |
214 |
0
| if (caches == null) throw new NullPointerException("Cache impl array is null");
|
215 |
148
| Cache[] c = new Cache[caches.length];
|
216 |
837
| for (int i = 0; i < caches.length; i++) c[i] = caches[i];
|
217 |
148
| return areCacheViewsComplete(c);
|
218 |
| } |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
0
| public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
|
225 |
| { |
226 |
0
| List members = cache.getMembers();
|
227 |
0
| if (members == null || memberCount > members.size())
|
228 |
| { |
229 |
0
| return false;
|
230 |
| } |
231 |
0
| else if (memberCount < members.size())
|
232 |
| { |
233 |
| |
234 |
0
| StringBuffer sb = new StringBuffer("Cache at address ");
|
235 |
0
| sb.append(cache.getLocalAddress());
|
236 |
0
| sb.append(" had ");
|
237 |
0
| sb.append(members.size());
|
238 |
0
| sb.append(" members; expecting ");
|
239 |
0
| sb.append(memberCount);
|
240 |
0
| sb.append(". Members were (");
|
241 |
0
| for (int j = 0; j < members.size(); j++)
|
242 |
| { |
243 |
0
| if (j > 0)
|
244 |
| { |
245 |
0
| sb.append(", ");
|
246 |
| } |
247 |
0
| sb.append(members.get(j));
|
248 |
| } |
249 |
0
| sb.append(')');
|
250 |
| |
251 |
0
| throw new IllegalStateException(sb.toString());
|
252 |
| } |
253 |
| |
254 |
0
| return true;
|
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
0
| public static boolean isCacheViewComplete(Cache c, int memberCount)
|
262 |
| { |
263 |
0
| return isCacheViewComplete(c, memberCount, true);
|
264 |
| } |
265 |
| |
266 |
487
| public static boolean isCacheViewComplete(Cache c, int memberCount, boolean barfIfTooManyMembers)
|
267 |
| { |
268 |
487
| CacheSPI cache = (CacheSPI) c;
|
269 |
487
| List members = cache.getMembers();
|
270 |
487
| if (members == null || memberCount > members.size())
|
271 |
| { |
272 |
82
| return false;
|
273 |
| } |
274 |
405
| else if (memberCount < members.size())
|
275 |
| { |
276 |
1
| if (barfIfTooManyMembers)
|
277 |
| { |
278 |
| |
279 |
1
| StringBuffer sb = new StringBuffer("Cache at address ");
|
280 |
1
| sb.append(cache.getLocalAddress());
|
281 |
1
| sb.append(" had ");
|
282 |
1
| sb.append(members.size());
|
283 |
1
| sb.append(" members; expecting ");
|
284 |
1
| sb.append(memberCount);
|
285 |
1
| sb.append(". Members were (");
|
286 |
1
| for (int j = 0; j < members.size(); j++)
|
287 |
| { |
288 |
10
| if (j > 0)
|
289 |
| { |
290 |
9
| sb.append(", ");
|
291 |
| } |
292 |
10
| sb.append(members.get(j));
|
293 |
| } |
294 |
1
| sb.append(')');
|
295 |
| |
296 |
1
| throw new IllegalStateException(sb.toString());
|
297 |
| } |
298 |
0
| else return false;
|
299 |
| } |
300 |
| |
301 |
404
| return true;
|
302 |
| } |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
14522
| public static void sleepThread(long sleeptime)
|
312 |
| { |
313 |
14522
| try
|
314 |
| { |
315 |
14522
| Thread.sleep(sleeptime);
|
316 |
| } |
317 |
| catch (InterruptedException ie) |
318 |
| { |
319 |
| } |
320 |
| } |
321 |
| |
322 |
64
| public static void sleepRandom(int maxTime)
|
323 |
| { |
324 |
64
| sleepThread(random.nextInt(maxTime));
|
325 |
| } |
326 |
| |
327 |
323
| public static void recursiveFileRemove(String directoryName)
|
328 |
| { |
329 |
323
| File file = new File(directoryName);
|
330 |
323
| if (file.exists())
|
331 |
| { |
332 |
321
| System.out.println("Deleting file " + file);
|
333 |
321
| recursivedelete(file);
|
334 |
| } |
335 |
| } |
336 |
| |
337 |
612
| private static void recursivedelete(File f)
|
338 |
| { |
339 |
612
| if (f.isDirectory())
|
340 |
| { |
341 |
349
| File[] files = f.listFiles();
|
342 |
349
| for (File file : files)
|
343 |
| { |
344 |
291
| recursivedelete(file);
|
345 |
| } |
346 |
| } |
347 |
| |
348 |
612
| f.delete();
|
349 |
| } |
350 |
| } |