1 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 24 public class TestingUtil 25 { 26 27 private static Random random = new Random (); 28 29 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 ("timed out before caches had complete views"); 52 } 53 54 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 ("timed out before caches had complete views"); 77 } 78 79 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 ("timed out before caches had complete views"); 102 } 103 104 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 ("timed out before caches had complete views"); 127 } 128 129 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 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 183 public static boolean isCacheViewComplete(CacheImpl cache, int memberCount) 184 { 185 List members = cache.getMembers(); 186 if (members == null || memberCount > members.size()) 187 { 188 return false; 189 } 190 else if (memberCount < members.size()) 191 { 192 StringBuffer sb = new StringBuffer ("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 (sb.toString()); 211 } 212 213 return true; 214 } 215 216 220 public static boolean isCacheViewComplete(Cache c, int memberCount) 221 { 222 CacheSPI cache = (CacheSPI) c; 223 List members = cache.getMembers(); 224 if (members == null || memberCount > members.size()) 225 { 226 return false; 227 } 228 else if (memberCount < members.size()) 229 { 230 StringBuffer sb = new StringBuffer ("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 (sb.toString()); 249 } 250 251 return true; 252 } 253 254 255 261 public static void sleepThread(long sleeptime) 262 { 263 try 264 { 265 Thread.sleep(sleeptime); 266 } 267 catch (InterruptedException 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 directoryName) 278 { 279 File file = new File (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 f) 288 { 289 if (f.isDirectory()) 290 { 291 File [] files = f.listFiles(); 292 for (File file : files) 293 { 294 recursivedelete(file); 295 } 296 } 297 f.delete(); 299 } 300 } 301 | Popular Tags |