Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 207   Methods: 6
NCLOC: 140   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuddyReplicationFailoverTest.java 91.7% 100% 100% 99.2%
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.jboss.cache.CacheImpl;
 10    import org.jboss.cache.Fqn;
 11    import org.jboss.cache.misc.TestingUtil;
 12   
 13    /**
 14    * Tests behaviour when data owners fail - essentially this tests data gravitation
 15    *
 16    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 17    */
 18    public class BuddyReplicationFailoverTest extends BuddyReplicationTestsBase
 19    {
 20    protected boolean optimisticLocks = false;
 21    private String key = "key";
 22    private String value = "value";
 23    private CacheImpl[] caches;
 24   
 25  12 protected void tearDown() throws Exception
 26    {
 27  12 if (caches != null)
 28    {
 29  12 cleanup(caches);
 30  12 caches = null;
 31    }
 32    }
 33   
 34   
 35  3 public void testDataGravitationKillOwner() throws Exception
 36    {
 37  3 testDataGravitation(true);
 38    }
 39   
 40  3 public void testDataGravitationDontKillOwner() throws Exception
 41    {
 42  3 testDataGravitation(false);
 43    }
 44   
 45  6 private void testDataGravitation(boolean killOwner) throws Exception
 46    {
 47  6 caches = createCaches(3, false, true, optimisticLocks);
 48   
 49  6 String fqn = "/test";
 50  6 String backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
 51   
 52  6 dumpCacheContents(caches);
 53   
 54  6 caches[0].put(fqn, key, value);
 55   
 56  6 dumpCacheContents(caches);
 57   
 58  6 assertEquals("Value should exist", value, caches[0].get(fqn, key));
 59   
 60  6 dumpCacheContents(caches);
 61   
 62    // use exists instead of get() to prevent going up the interceptor stack
 63  6 assertTrue("Should be false", !caches[1].exists(fqn));
 64  6 assertTrue("Should be false", !caches[2].exists(fqn));
 65   
 66  6 assertNull("Should be null", caches[0].get(backupFqn, key));
 67  6 assertEquals("Value should exist", value, caches[1].get(backupFqn, key));
 68  6 assertNull("Should be null", caches[2].get(backupFqn, key));
 69   
 70  6 if (killOwner)
 71    {
 72  3 caches[0].stop();
 73  3 caches[0] = null;
 74  3 TestingUtil.sleepThread(500);
 75    }
 76   
 77  6 System.out.println("***** Killed original data owner, about to call a get on a different cache instance. *****");
 78   
 79    // according to data gravitation, a call to *any* cache should retrieve the data, and move the data to the new cache.
 80  6 assertEquals("Value should have gravitated", value, caches[2].get(fqn, key));
 81   
 82  6 TestingUtil.sleepThread(500);
 83   
 84  6 dumpCacheContents(caches);
 85   
 86    // now lets test the eviction part of gravitation
 87  6 String newBackupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + fqn;
 88   
 89    // use exists instead of get() to prevent going up the interceptor stack
 90  3 if (!killOwner) assertTrue("Should be false", !caches[0].exists(fqn));
 91  6 assertTrue("Should be false", !caches[1].exists(fqn));
 92   
 93    // the old backup should no longer exist
 94  3 if (!killOwner) assertNull("Should be null", caches[0].get(backupFqn, key));
 95  6 assertNull("Should be null", caches[1].get(backupFqn, key));
 96  6 assertNull("Should be null", caches[2].get(backupFqn, key));
 97   
 98    // and the backup should now exist in caches[2]'s buddy which is caches[0]
 99  6 if (killOwner)
 100    {
 101  3 assertEquals("Value should exist", value, caches[1].get(newBackupFqn, key));
 102    }
 103    else
 104    {
 105  3 assertEquals("Value should exist", value, caches[0].get(newBackupFqn, key));
 106  3 assertNull("Should be null", caches[1].get(newBackupFqn, key));
 107    }
 108  6 assertNull("Should be null", caches[2].get(newBackupFqn, key));
 109    }
 110   
 111  3 public void testDataReplicationSuppression() throws Exception
 112    {
 113  3 caches = createCaches(3, false, false, optimisticLocks);
 114   
 115  3 Fqn fqn = Fqn.fromString("/test");
 116  3 Fqn backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())
 117    + "/test");
 118  3 caches[0].put(fqn, key, value);
 119   
 120  3 dumpCacheContents(caches);
 121   
 122  3 assertEquals("value", caches[0].get(fqn, key));
 123  3 assertNull(caches[0].get(backupFqn, key));
 124  3 assertEquals("value", caches[1].get(backupFqn, key));
 125  3 assertNull(caches[1].get(fqn, key));
 126  3 assertNull(caches[2].get(fqn, key));
 127  3 assertNull(caches[2].get(backupFqn, key));
 128   
 129  3 assertNoLocks(caches);
 130   
 131  3 backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[1].getLocalAddress())
 132    + "/test");
 133   
 134  3 System.out.println("*** Calling get() on cache[1] with force option");
 135   
 136  3 caches[1].getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
 137  3 assertEquals("value", caches[1].get(fqn, key));
 138   
 139  3 dumpCacheContents(caches);
 140   
 141  3 assertNull(caches[1].get(backupFqn, key));
 142  3 assertEquals("value", caches[2].get(backupFqn, key));
 143  3 assertNull(caches[2].get(fqn, key));
 144  3 assertNull(caches[0].get(fqn, key));
 145  3 assertNull(caches[0].get(backupFqn, key));
 146    }
 147   
 148  3 public void testSubtreeRetrieval() throws Exception
 149    {
 150  3 caches = createCaches(3, false, true, optimisticLocks);
 151   
 152  3 Fqn fqn = Fqn.fromString("/test");
 153  3 Fqn fqn2 = Fqn.fromString("/test/subtree");
 154   
 155  3 Fqn backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
 156  3 Fqn backupFqn2 = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test/subtree");
 157   
 158   
 159  3 caches[0].put(fqn, key, value);
 160  3 caches[0].put(fqn2, key, value);
 161   
 162    // test backup replication to buddy
 163  3 assertEquals(value, caches[0].get(fqn, key));
 164  3 assertEquals(value, caches[0].get(fqn2, key));
 165  3 assertEquals(value, caches[1].get(backupFqn, key));
 166  3 assertEquals(value, caches[1].get(backupFqn2, key));
 167   
 168  3 assertTrue(!caches[0].exists(backupFqn));
 169  3 assertTrue(!caches[0].exists(backupFqn2));
 170  3 assertTrue(!caches[1].exists(fqn));
 171  3 assertTrue(!caches[1].exists(fqn2));
 172  3 assertTrue(!caches[2].exists(fqn));
 173  3 assertTrue(!caches[2].exists(fqn2));
 174  3 assertTrue(!caches[2].exists(backupFqn));
 175  3 assertTrue(!caches[2].exists(backupFqn2));
 176   
 177  3 assertNoLocks(caches);
 178   
 179    // gravitate to 2:
 180  3 caches[2].get(fqn); // expect entire subtree to gravitate.
 181   
 182  3 Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");
 183  3 Fqn newBackupFqn2 = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test/subtree");
 184   
 185  3 assertEquals(value, caches[2].get(fqn, key));
 186  3 assertTrue(caches[2].exists(fqn2));
 187  3 assertEquals(value, caches[0].get(newBackupFqn, key));
 188  3 assertTrue(caches[0].exists(newBackupFqn2));
 189   
 190  3 assertTrue(!caches[2].exists(newBackupFqn));
 191  3 assertTrue(!caches[2].exists(newBackupFqn2));
 192  3 assertTrue(!caches[0].exists(fqn));
 193  3 assertTrue(!caches[0].exists(fqn2));
 194  3 assertTrue(!caches[1].exists(fqn));
 195  3 assertTrue(!caches[1].exists(fqn2));
 196  3 assertTrue(!caches[1].exists(newBackupFqn));
 197  3 assertTrue(!caches[1].exists(newBackupFqn2));
 198   
 199  3 for (int i = 0; i < caches.length; i++)
 200    {
 201  9 assertTrue(!caches[i].exists(backupFqn));
 202  9 assertTrue(!caches[i].exists(backupFqn2));
 203    }
 204   
 205  3 assertNoLocks(caches);
 206    }
 207    }