Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 138   Methods: 5
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestingUtil.java 0% 5.3% 20% 4.9%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.pojo;
 9   
 10    import org.jboss.cache.CacheImpl;
 11   
 12    import java.util.List;
 13   
 14    /**
 15    * Utilities for unit testing JBossCache.
 16    *
 17    * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
 18    * @version $Revision: 1.3 $
 19    */
 20    public class TestingUtil
 21    {
 22   
 23    /**
 24    * @param caches caches which must all have consistent views
 25    * @param timeout max number of ms to loop
 26    * @throws RuntimeException if <code>timeout</code> ms have elapse without
 27    * all caches having the same number of members.
 28    */
 29  0 public static void blockUntilViewsReceived(PojoCache[] caches, long timeout)
 30    {
 31  0 long failTime = System.currentTimeMillis() + timeout;
 32   
 33  0 while (System.currentTimeMillis() < failTime)
 34    {
 35  0 org.jboss.cache.pojo.TestingUtil.sleepThread(100);
 36  0 if (org.jboss.cache.pojo.TestingUtil.areCacheViewsComplete(caches))
 37  0 return;
 38    }
 39   
 40  0 throw new RuntimeException("timed out before caches had complete views");
 41    }
 42   
 43    /**
 44    */
 45  0 public static void blockUntilViewReceived(PojoCache cache, int groupSize, long timeout)
 46    {
 47  0 long failTime = System.currentTimeMillis() + timeout;
 48   
 49  0 CacheImpl tcache = (CacheImpl) cache.getCache();
 50  0 while (System.currentTimeMillis() < failTime)
 51    {
 52  0 org.jboss.cache.pojo.TestingUtil.sleepThread(100);
 53  0 if (org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(tcache, groupSize))
 54  0 return;
 55    }
 56   
 57  0 throw new RuntimeException("timed out before caches had complete views");
 58    }
 59   
 60    /**
 61    * Checks each cache to see if the number of elements in the array
 62    * returned by {@link org.jboss.cache.CacheImpl#getMembers()} matches the size of
 63    * the <code>caches</code> parameter.
 64    *
 65    * @param caches caches that should form a View
 66    * @return <code>true</code> if all caches have
 67    * <code>caches.length</code> members; false otherwise
 68    * @throws IllegalStateException if any of the caches have MORE view
 69    * members than caches.length
 70    */
 71  0 public static boolean areCacheViewsComplete(PojoCache[] caches)
 72    {
 73  0 int memberCount = caches.length;
 74   
 75  0 for (int i = 0; i < memberCount; i++)
 76    {
 77  0 CacheImpl cache = (CacheImpl) caches[i].getCache();
 78  0 return org.jboss.cache.pojo.TestingUtil.isCacheViewComplete(cache, memberCount);
 79    }
 80   
 81  0 return true;
 82    }
 83   
 84    /**
 85    * FIXME Comment this
 86    *
 87    * @param cache
 88    * @param memberCount
 89    */
 90  0 public static boolean isCacheViewComplete(CacheImpl cache, int memberCount)
 91    {
 92  0 List members = cache.getMembers();
 93  0 if (members == null || memberCount > members.size())
 94    {
 95  0 return false;
 96    }
 97  0 else if (memberCount < members.size())
 98    {
 99    // This is an exceptional condition
 100  0 StringBuffer sb = new StringBuffer("Cache at address ");
 101  0 sb.append(cache.getLocalAddress());
 102  0 sb.append(" had ");
 103  0 sb.append(members.size());
 104  0 sb.append(" members; expecting ");
 105  0 sb.append(memberCount);
 106  0 sb.append(". Members were (");
 107  0 for (int j = 0; j < members.size(); j++)
 108    {
 109  0 if (j > 0)
 110  0 sb.append(", ");
 111  0 sb.append(members.get(j));
 112    }
 113  0 sb.append(')');
 114   
 115  0 throw new IllegalStateException(sb.toString());
 116    }
 117   
 118  0 return true;
 119    }
 120   
 121   
 122    /**
 123    * Puts the current thread to sleep for the desired number of ms, suppressing
 124    * any exceptions.
 125    *
 126    * @param sleeptime number of ms to sleep
 127    */
 128  1962 public static void sleepThread(long sleeptime)
 129    {
 130  1962 try
 131    {
 132  1962 Thread.sleep(sleeptime);
 133    }
 134    catch (InterruptedException ie)
 135    {
 136    }
 137    }
 138    }