1 7 8 package org.jboss.cache.pojo; 9 10 import org.jboss.cache.CacheImpl; 11 12 import java.util.Vector ; 13 14 20 public class TestingUtil 21 { 22 23 29 public static void blockUntilViewsReceived(PojoCache[] caches, long timeout) 30 { 31 long failTime = System.currentTimeMillis() + timeout; 32 33 while (System.currentTimeMillis() < failTime) 34 { 35 org.jboss.cache.pojo.TestingUtil.sleepThread(100); 36 if (org.jboss.cache.pojo.TestingUtil.areCacheViewsComplete(caches)) 37 return; 38 } 39 40 throw new RuntimeException ("timed out before caches had complete views"); 41 } 42 43 45 public static void blockUntilViewReceived(PojoCache cache, int groupSize, long timeout) 46 { 47 long failTime = System.currentTimeMillis() + timeout; 48 49 CacheImpl tcache = (CacheImpl) cache.getCache(); 50 while (System.currentTimeMillis() < failTime) 51 { 52 org.jboss.cache.pojo.TestingUtil.sleepThread(100); 53 if (org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(tcache, groupSize)) 54 return; 55 } 56 57 throw new RuntimeException ("timed out before caches had complete views"); 58 } 59 60 71 public static boolean areCacheViewsComplete(PojoCache[] caches) 72 { 73 int memberCount = caches.length; 74 75 for (int i = 0; i < memberCount; i++) 76 { 77 CacheImpl cache = (CacheImpl) caches[i].getCache(); 78 return org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(cache, memberCount); 79 } 80 81 return true; 82 } 83 84 90 public static boolean isCacheViewComplete(CacheImpl cache, int memberCount) 91 { 92 Vector members = cache.getMembers(); 93 if (members == null || memberCount > members.size()) 94 { 95 return false; 96 } 97 else if (memberCount < members.size()) 98 { 99 StringBuffer sb = new StringBuffer ("Cache at address "); 101 sb.append(cache.getLocalAddress()); 102 sb.append(" had "); 103 sb.append(members.size()); 104 sb.append(" members; expecting "); 105 sb.append(memberCount); 106 sb.append(". Members were ("); 107 for (int j = 0; j < members.size(); j++) 108 { 109 if (j > 0) 110 sb.append(", "); 111 sb.append(members.elementAt(j)); 112 } 113 sb.append(')'); 114 115 throw new IllegalStateException (sb.toString()); 116 } 117 118 return true; 119 } 120 121 122 128 public static void sleepThread(long sleeptime) 129 { 130 try 131 { 132 Thread.sleep(sleeptime); 133 } 134 catch (InterruptedException ie) 135 { 136 } 137 } 138 } 139 | Popular Tags |