Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 290   Methods: 8
NCLOC: 176   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuddyReplicationContentTest.java 50% 99.2% 100% 97.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    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.Fqn;
 13    import org.jboss.cache.misc.TestingUtil;
 14   
 15    /**
 16    * Teststhe transfer of content under *normal* operation
 17    *
 18    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 19    */
 20    public class BuddyReplicationContentTest extends BuddyReplicationTestsBase
 21    {
 22    private String key = "key";
 23    private String value = "value";
 24    private Log log = LogFactory.getLog(BuddyGroupAssignmentTest.class);
 25   
 26   
 27  18 private void assertNoStaleLocks(CacheImpl[] caches)
 28    {
 29  54 for (CacheImpl cache : caches) assertNoStaleLocks(cache);
 30    }
 31   
 32  54 private void assertNoStaleLocks(CacheImpl cache)
 33    {
 34  54 assertEquals("Number of locks in cache instance " + cache.toString(true) + " should be 0", 0, cache.getNumberOfLocksHeld());
 35    }
 36   
 37  5 protected void setUp() throws Exception
 38    {
 39  5 log.debug("Starting setUp()");
 40  5 super.setUp();
 41  5 log.debug("Finishing setUp()");
 42    }
 43   
 44  1 public void testSimplePut() throws Exception
 45    {
 46  1 log.debug("Running testSimplePut");
 47  1 caches = createCaches(3, false);
 48   
 49  1 String fqn = "/test";
 50  1 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
 51   
 52  1 assertNoStaleLocks(caches);
 53   
 54    // put something in cache 1
 55  1 caches[0].put(fqn, key, value);
 56   
 57  1 assertNoStaleLocks(caches);
 58   
 59    // this should be in neither of the other caches' "main" trees
 60  1 assertEquals(value, caches[0].get(fqn, key));
 61  1 assertNull("Should be null", caches[1].get(fqn, key));
 62  1 assertNull("Should be null", caches[2].get(fqn, key));
 63   
 64    // check the backup trees
 65   
 66  1 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
 67  1 assertNull("Should be null", caches[2].get(backupFqn, key));
 68   
 69  1 assertNoStaleLocks(caches);
 70    }
 71   
 72  1 public void testPutAndRemove() throws Exception
 73    {
 74  1 log.debug("Running testPutAndRemove");
 75  1 caches = createCaches(3, false);
 76   
 77  1 String fqn = "/test";
 78  1 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
 79   
 80  1 assertNoStaleLocks(caches);
 81   
 82    // put something in cache 1
 83  1 caches[0].put(fqn, key, value);
 84   
 85  1 assertNoStaleLocks(caches);
 86   
 87    // this should be in neither of the other caches' "main" trees
 88  1 assertEquals(value, caches[0].get(fqn, key));
 89  1 assertNull("Should be null", caches[1].get(fqn, key));
 90  1 assertNull("Should be null", caches[2].get(fqn, key));
 91   
 92    // check the backup trees
 93   
 94  1 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
 95  1 assertNull("Should be null", caches[2].get(backupFqn, key));
 96   
 97  1 assertNoStaleLocks(caches);
 98   
 99    // now remove
 100  1 caches[0].remove(fqn);
 101  1 assertNoStaleLocks(caches);
 102   
 103   
 104  1 assertNull("Should be null", caches[0].get(fqn, key));
 105  1 assertNull("Should be null", caches[1].get(fqn, key));
 106  1 assertNull("Should be null", caches[2].get(fqn, key));
 107   
 108    // check the backup trees
 109  1 assertNull("Should be null", caches[0].get(backupFqn, key));
 110  1 assertNull("Should be null", caches[1].get(backupFqn, key));
 111  1 assertNull("Should be null", caches[2].get(backupFqn, key));
 112   
 113  1 assertNoStaleLocks(caches);
 114    }
 115   
 116  1 public void testPutAndRemove2() throws Exception
 117    {
 118  1 log.debug("Running testPutAndRemove2");
 119  1 caches = createCaches(2, 4, false);
 120   
 121  1 String fqn = "/test";
 122  1 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
 123   
 124    // put something in cache 1
 125  1 assertNoStaleLocks(caches);
 126   
 127  1 caches[0].put(fqn, key, value);
 128   
 129  1 assertNoStaleLocks(caches);
 130   
 131    // this should be in neither of the other caches' "main" trees
 132  1 assertEquals(value, caches[0].get(fqn, key));
 133  1 assertNull("Should be null", caches[1].get(fqn, key));
 134  1 assertNull("Should be null", caches[2].get(fqn, key));
 135  1 assertNull("Should be null", caches[3].get(fqn, key));
 136   
 137    // check the backup trees
 138  1 assertEquals("Buddy should have data in backup tree", value, caches[1].get(backupFqn, key));
 139  1 assertEquals("Buddy should have data in backup tree", value, caches[2].get(backupFqn, key));
 140  1 assertNull("Should be null", caches[3].get(backupFqn, key));
 141   
 142  1 assertNoStaleLocks(caches);
 143   
 144    // now remove
 145  1 caches[0].remove(fqn);
 146  1 assertNoStaleLocks(caches);
 147   
 148  1 assertNull("Should be null", caches[0].get(fqn, key));
 149  1 assertNull("Should be null", caches[1].get(fqn, key));
 150  1 assertNull("Should be null", caches[2].get(fqn, key));
 151  1 assertNull("Should be null", caches[3].get(fqn, key));
 152   
 153    // check the backup trees
 154  1 assertNull("Should be null", caches[0].get(backupFqn, key));
 155  1 assertNull("Should be null", caches[1].get(backupFqn, key));
 156  1 assertNull("Should be null", caches[2].get(backupFqn, key));
 157  1 assertNull("Should be null", caches[3].get(backupFqn, key));
 158   
 159  1 assertNoStaleLocks(caches);
 160    }
 161   
 162  1 public void testBuddyJoin() throws Exception
 163    {
 164  1 log.debug("Running testBuddyJoin");
 165  1 caches = createCaches(2, false);
 166  1 CacheImpl cache2 = null;
 167   
 168  1 try
 169    {
 170  1 Fqn fqn = Fqn.fromString("/test");
 171  1 Fqn backupFqn = BuddyManager.getBackupFqn(caches[1].getLocalAddress(), fqn);
 172   
 173  1 assertNoStaleLocks(caches);
 174   
 175    // put something in cache 1
 176  1 caches[1].put(fqn, key, value);
 177   
 178  1 assertNoStaleLocks(caches);
 179   
 180    // this should be in neither of the other caches' "main" trees
 181  1 assertEquals(value, caches[1].get(fqn, key));
 182  1 assertFalse("Should be false", caches[0].exists(fqn));
 183   
 184    // check the backup trees
 185  1 assertEquals("Buddy should have data in backup tree", value, caches[0].get(backupFqn, key));
 186   
 187  1 assertNoStaleLocks(caches);
 188   
 189    // now add a new cache to the cluster
 190  1 cache2 = createCache(1, null);
 191   
 192    // allow this cache a few msecs to join
 193  1 TestingUtil.blockUntilViewsReceived(3000, caches[0], caches[1], cache2);
 194   
 195  1 TestingUtil.sleepThread(2000); // allow buddy group reorg
 196   
 197  1 dumpCacheContents(caches[0], caches[1], cache2);
 198   
 199    // now caches[1]'s buddy should be cache2, not cache[0]
 200  1 assertIsBuddy(caches[1], cache2, true);
 201    // this should still be the same
 202  1 assertIsBuddy(caches[0], caches[1], true);
 203    // and cache2's buddy should be cache[0]
 204  1 assertIsBuddy(cache2, caches[0], true);
 205   
 206    // so now the backup data we saw on cache[0] should have been removed.
 207  1 assertFalse("This backup data should have been removed", caches[0].exists(backupFqn));
 208   
 209    // while cache2 should now posess this backup (due to a state transfer)
 210  1 assertEquals("Backup state should have been transferred to this new cache instance", value, cache2.get(backupFqn, key));
 211   
 212  1 caches[1].remove(fqn);
 213  1 assertNoStaleLocks(caches);
 214   
 215   
 216  1 assertFalse("Should be null", caches[0].exists(fqn));
 217  1 assertFalse("Should be null", caches[1].exists(fqn));
 218  1 assertFalse("Should be null", cache2.exists(fqn));
 219   
 220    // check the backup trees
 221  1 assertFalse("Should be null", caches[0].exists(backupFqn));
 222  1 assertFalse("Should be null", caches[1].exists(backupFqn));
 223  1 assertFalse("Should be null", cache2.exists(backupFqn));
 224   
 225  1 assertNoStaleLocks(caches);
 226   
 227    }
 228    finally
 229    {
 230  1 if (cache2 != null) cache2.stop();
 231    }
 232    }
 233   
 234  1 public void testCompleteStateSurvival() throws Exception
 235    {
 236  1 log.debug("Running testCompleteStateSurvival");
 237  1 caches = null;
 238   
 239  1 caches = createCaches(3, false, true);
 240  1 caches[0].put("/0", "key", "value");
 241  1 caches[1].put("/1", "key", "value");
 242  1 caches[2].put("/2", "key", "value");
 243   
 244  1 TestingUtil.sleepThread(getSleepTimeout());
 245   
 246  1 log.info("stopping 2");
 247  1 caches[2].stop();
 248   
 249  1 log.info("0 ** " + caches[0].printLockInfo());
 250  1 log.info("1 ** " + caches[1].printLockInfo());
 251  1 log.info("2 ** " + caches[2].printLockInfo());
 252   
 253  1 TestingUtil.sleepThread(getSleepTimeout());
 254   
 255  1 assertEquals("value", caches[0].get("/2", "key"));
 256   
 257  1 log.info("0 ** " + caches[0].printLockInfo());
 258  1 log.info("1 ** " + caches[1].printLockInfo());
 259  1 log.info("2 ** " + caches[2].printLockInfo());
 260   
 261  1 TestingUtil.sleepThread(getSleepTimeout());
 262   
 263  1 caches[1].stop();
 264  1 log.info("0 ** " + caches[0].printLockInfo());
 265  1 log.info("1 ** " + caches[1].printLockInfo());
 266  1 log.info("2 ** " + caches[2].printLockInfo());
 267   
 268    // cache[0] is all thats left!!
 269   
 270  1 assertEquals("value", caches[0].get("/0", "key"));
 271   
 272  1 try
 273    {
 274  1 assertEquals("value", caches[0].get("/1", "key"));
 275    }
 276    catch (Exception e)
 277    {
 278    // may barf the first time around since we are unable to contact our buddy and store this data.
 279  1 if (e instanceof RuntimeException)
 280    {
 281  1 assertEquals(IllegalArgumentException.class, e.getCause().getClass());
 282    }
 283  0 else throw e;
 284    }
 285   
 286    // now try the assertion again since the local gravitation would have worked.
 287  1 assertEquals("value", caches[0].get("/1", "key"));
 288  1 assertEquals("value", caches[0].get("/2", "key"));
 289    }
 290    }