Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 196   Methods: 10
NCLOC: 140   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BuddyPoolBroadcastTest.java 100% 98.7% 100% 99%
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    package org.jboss.cache.buddyreplication;
 8   
 9    import org.apache.commons.logging.Log;
 10    import org.apache.commons.logging.LogFactory;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.misc.TestingUtil;
 13   
 14    import java.util.Map;
 15    import java.util.concurrent.CountDownLatch;
 16   
 17    /**
 18    * Tests basic group membership semantics
 19    *
 20    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 21    */
 22    public class BuddyPoolBroadcastTest extends BuddyReplicationTestsBase
 23    {
 24    private Log log = LogFactory.getLog(BuddyPoolBroadcastTest.class);
 25   
 26  4 private void checkConsistentPoolState(CacheImpl[] caches)
 27    {
 28  4 for (int i = 0; i < caches.length; i++)
 29    {
 30  17 Map groupMap = caches[i].getBuddyManager().buddyPool;
 31  17 for (int j = 0; j < caches.length; j++)
 32    {
 33  91 if (i != j)
 34    {
 35  74 Map groupMap2 = caches[j].getBuddyManager().buddyPool;
 36  74 for (CacheImpl cache : caches)
 37    {
 38  502 assertEquals("Comparing contents of cache " + (i + 1) + " pool map with cache " + (j + 1), groupMap.get(cache), groupMap2.get(cache));
 39    }
 40    }
 41    }
 42    }
 43    }
 44   
 45   
 46  5 protected void setUp() throws Exception
 47    {
 48  5 log.debug("Starting setUp()");
 49  5 super.setUp();
 50  5 log.debug("Finishing setUp()");
 51    }
 52   
 53  5 protected void tearDown() throws Exception
 54    {
 55  5 long st = System.currentTimeMillis();
 56  5 super.tearDown();
 57  5 System.out.println("Teardown: " + (System.currentTimeMillis() - st));
 58    }
 59   
 60  1 public void test2CachesWithPoolNames() throws Exception
 61    {
 62  1 log.error("Running test2CachesWithPoolNames");
 63  1 caches = createCaches(2, true);
 64  1 log.error("Created 2 caches");
 65   
 66  1 BuddyManager m = caches[0].getBuddyManager();
 67  1 Map groupMap = m.buddyPool;
 68   
 69  1 assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
 70  1 assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
 71    }
 72   
 73  1 public void test3CachesWithPoolNames() throws Exception
 74    {
 75  1 log.debug("Running test3CachesWithPoolNames");
 76  1 long st = System.currentTimeMillis();
 77  1 caches = createCaches(3, true);
 78  1 System.out.println("Setup: " + (System.currentTimeMillis() - st));
 79  1 st = System.currentTimeMillis();
 80   
 81  1 BuddyManager m = caches[0].getBuddyManager();
 82  1 Map groupMap = m.buddyPool;
 83   
 84  1 assertEquals("A", groupMap.get(caches[0].getLocalAddress()));
 85  1 assertEquals("B", groupMap.get(caches[1].getLocalAddress()));
 86  1 assertEquals("C", groupMap.get(caches[2].getLocalAddress()));
 87  1 System.out.println("Test: " + (System.currentTimeMillis() - st));
 88    }
 89   
 90  1 public void testBuddyPoolSync() throws Exception
 91    {
 92  1 log.debug("Running testBuddyPoolSync");
 93  1 caches = createCaches(3, true);
 94   
 95  1 Map map = caches[0].getBuddyManager().buddyPool;
 96   
 97    // first test the values
 98  1 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
 99  1 assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
 100  1 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
 101   
 102    // now test against each other
 103  1 checkConsistentPoolState(caches);
 104    }
 105   
 106  1 public void testChangingBuddyPoolMembership() throws Exception
 107    {
 108  1 log.debug("Running testChangingBuddyPoolMembership");
 109  1 caches = createCaches(3, true);
 110   
 111  1 Map map = caches[0].getBuddyManager().buddyPool;
 112   
 113    // first test the values
 114  1 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
 115  1 assertEquals("Failed on cache 1", "B", map.get(caches[1].getLocalAddress()));
 116  1 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
 117   
 118    // now test against each other
 119  1 checkConsistentPoolState(caches);
 120   
 121  1 caches[1].stop();
 122  1 caches[1] = createCache(1, "Z");
 123   
 124  1 TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
 125  1 TestingUtil.sleepThread(getSleepTimeout());
 126   
 127    // first test the values
 128  1 assertEquals("Failed on cache 1", "A", map.get(caches[0].getLocalAddress()));
 129  1 assertEquals("Failed on cache 1", "Z", map.get(caches[1].getLocalAddress()));
 130  1 assertEquals("Failed on cache 1", "C", map.get(caches[2].getLocalAddress()));
 131   
 132    // now test against each other
 133  1 checkConsistentPoolState(caches);
 134    }
 135   
 136  1 public void testConcurrency() throws Exception
 137    {
 138  1 log.debug("Running testConcurrency");
 139  1 int numCaches = 8;
 140  1 caches = new CacheImpl[numCaches];
 141  1 CountDownLatch latch = new CountDownLatch(1);
 142  1 CacheStarter[] starters = new CacheStarter[numCaches];
 143   
 144  1 for (int i = 0; i < numCaches; i++)
 145    {
 146  8 caches[i] = createCache(1, new String(new char[]{(char) ('A' + i)}), false, false);
 147  8 starters[i] = new CacheStarter("CacheStarter-" + i, latch, caches[i]);
 148  8 starters[i].start();
 149    }
 150   
 151    // now have the lot start simultaneously
 152    // TestingUtil.sleepThread(500);
 153  1 latch.countDown();
 154   
 155    // allow a generous sleep time
 156  1 TestingUtil.blockUntilViewsReceived(caches, 240000);
 157  1 TestingUtil.sleepThread(1000 * numCaches); // the max timeout we can expect is 2500ms * 10 nodes
 158   
 159    // and now look at the state of things.
 160  1 Map map = caches[0].getBuddyManager().buddyPool;
 161  1 System.out.println(map);
 162  1 for (int i = 0; i < numCaches; i++)
 163    {
 164  8 assertEquals("Failed on cache " + i + "(" + caches[i].getLocalAddress() + ")", new String(new char[]{(char) ('A' + i)}), map.get(caches[i].getLocalAddress()));
 165    }
 166   
 167  1 checkConsistentPoolState(caches);
 168    }
 169   
 170    public class CacheStarter extends Thread
 171    {
 172    private CountDownLatch latch;
 173    private CacheImpl cache;
 174   
 175  8 public CacheStarter(String name, CountDownLatch latch, CacheImpl cache)
 176    {
 177  8 super(name);
 178  8 this.latch = latch;
 179  8 this.cache = cache;
 180    }
 181   
 182  8 public void run()
 183    {
 184  8 try
 185    {
 186  8 latch.await();
 187  8 cache.start();
 188    }
 189    catch (Exception e)
 190    {
 191  0 e.printStackTrace();
 192    }
 193    }
 194    }
 195   
 196    }