BuddyAssignmentStateTransferTest.java :  » JBoss » JBossCache » org » jboss » cache » buddyreplication » Java Open Source

Java Open Source » JBoss » JBossCache 
JBossCache » org » jboss » cache » buddyreplication » BuddyAssignmentStateTransferTest.java
/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.cache.buddyreplication;

import org.jboss.cache.CacheSPI;
import org.jboss.cache.Fqn;
import org.jboss.cache.misc.TestingUtil;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;

import java.util.ArrayList;

/**
 * Tests how groups are formed and disbanded
 *
 * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 */
@Test(groups = {"functional", "jgroups"})
public class BuddyAssignmentStateTransferTest extends BuddyReplicationTestsBase
{

   protected int timeout = 10000; // !!!

   protected int getSleepTimeout()
   {
      return timeout;
   }

   public void testNonRegionBasedStateTransfer() throws Exception
   {
      caches = new ArrayList<CacheSPI<Object, Object>>();
      caches.add(createCache(1, "TEST", false, true));

      Fqn<String> main = Fqn.fromString("/a/b/c");
      caches.get(0).put(main, "name", "Joe");

      caches.add(createCache(1, "TEST", false, true));

      TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]), VIEW_BLOCK_TIMEOUT);
      TestingUtil.sleepThread(getSleepTimeout());

      Fqn<String> test = new Fqn<String>(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
            BuddyManager.getGroupNameFromAddress(caches.get(0).getLocalAddress()));
      test = new Fqn<String>(test, main);

      assertEquals("State transferred", "Joe", caches.get(1).get(test, "name"));

      caches.add(createCache(1, "TEST", false, true));

      TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]), VIEW_BLOCK_TIMEOUT);
      TestingUtil.sleepThread(getSleepTimeout());

      assertNull("State not transferred", caches.get(2).get(test, "name"));

      // Make 2 the buddy of 0
      caches.get(1).stop();
      caches.set(1, null);

      TestingUtil.sleepThread(getSleepTimeout());

      assertEquals("State transferred", "Joe", caches.get(2).get(test, "name"));
   }

   public void testRegionBasedStateTransfer() throws Exception
   {
      caches = new ArrayList<CacheSPI<Object, Object>>();

      caches.add(createCache(1, "TEST", false, false));
      caches.add(createCache(1, "TEST", false, false));
      caches.add(createCache(1, "TEST", false, false));
      // JBCACHE-1234 -- add a 4th cache so when we kill caches[1]
      // caches[0] is not the backup node for caches[2] (although
      // caches[2] *is* the backup node for caches[0]
      caches.add(createCache(1, "TEST", false, false));
      caches.get(0).getConfiguration().setInactiveOnStartup(true);
      caches.get(1).getConfiguration().setInactiveOnStartup(true);
      caches.get(2).getConfiguration().setInactiveOnStartup(true);
      caches.get(3).getConfiguration().setInactiveOnStartup(true);
      caches.get(0).getConfiguration().setUseRegionBasedMarshalling(true);
      caches.get(1).getConfiguration().setUseRegionBasedMarshalling(true);
      caches.get(2).getConfiguration().setUseRegionBasedMarshalling(true);
      caches.get(3).getConfiguration().setUseRegionBasedMarshalling(true);
      caches.get(0).start();
      caches.get(1).start();
      caches.get(2).start();
      caches.get(3).start();

      TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]), VIEW_BLOCK_TIMEOUT);
      TestingUtil.sleepThread(getSleepTimeout());

      Fqn fqnA = Fqn.fromString("/a");
      Fqn fqnD = Fqn.fromString("/d");

      // FIXME We have to use a hack to get JBC to recognize that our
      // regions are for marshalling
      ClassLoader cl = Fqn.class.getClassLoader();
      caches.get(0).getRegion(fqnA, true).registerContextClassLoader(cl);
      caches.get(1).getRegion(fqnA, true).registerContextClassLoader(cl);
      caches.get(2).getRegion(fqnA, true).registerContextClassLoader(cl);
      caches.get(3).getRegion(fqnA, true).registerContextClassLoader(cl);
      caches.get(0).getRegion(fqnD, true).registerContextClassLoader(cl);
      caches.get(1).getRegion(fqnD, true).registerContextClassLoader(cl);
      caches.get(2).getRegion(fqnD, true).registerContextClassLoader(cl);
      caches.get(3).getRegion(fqnD, true).registerContextClassLoader(cl);

      caches.get(0).getRegion(fqnA, true).activate();
      caches.get(1).getRegion(fqnA, true).activate();
      caches.get(2).getRegion(fqnA, true).activate();
      caches.get(3).getRegion(fqnA, true).activate();

      caches.get(0).getRegion(fqnD, true).activate();
      caches.get(1).getRegion(fqnD, true).activate();

      Fqn mainA = Fqn.fromString("/a/b/c");
      caches.get(0).put(mainA, "name", "Joe");

      Fqn mainD = Fqn.fromString("/d/e/f");
      caches.get(0).put(mainD, "name", "Joe");

      Fqn group = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN,
            BuddyManager.getGroupNameFromAddress(caches.get(0).getLocalAddress()));
      Fqn testA = new Fqn(group, mainA);
      assertEquals("/a replicated", "Joe", caches.get(1).get(testA, "name"));
      assertNull("No backup of /a", caches.get(2).get(testA, "name"));

      Fqn testD = new Fqn(group, mainD);
      assertEquals("/d replicated", "Joe", caches.get(1).get(testD, "name"));
      assertNull("No backup of /d", caches.get(2).get(testD, "name"));

      // Make 2 the buddy of 0 -- this should cause a push from 0 to 2
      caches.get(1).stop();

      TestingUtil.sleepThread(getSleepTimeout());

      assertEquals("/a state transferred", "Joe", caches.get(2).get(testA, "name"));
      assertNull("/d state not transferred", caches.get(2).get(testD, "name"));

      // JBCACHE-1234 -- Activate region on 2 and 3.  This should cause
      // a pull from 0 by and from 2 by 3.
      caches.get(2).getRegion(fqnD, true).activate();
      caches.get(3).getRegion(fqnD, true).activate();
      assertEquals("/d transferred to cache 2", "Joe", caches.get(2).get(testD, "name"));
      assertNull("/d state not transferred to cache 3", caches.get(3).get(testD, "name"));
   }

   public void testPersistentStateTransfer() throws Exception
   {
      caches = new ArrayList<CacheSPI<Object, Object>>();

      caches.add(createCacheWithCacheLoader(false, false, false, true, false));
      caches.get(0).getConfiguration().setFetchInMemoryState(false);

      caches.get(0).start();

      Fqn<String> main = Fqn.fromString("/a/b/c");
      caches.get(0).put(main, "name", "Joe");

      caches.add(createCacheWithCacheLoader(false, false, false, true, false));
      caches.get(1).getConfiguration().setFetchInMemoryState(false);

      caches.get(1).start();

      TestingUtil.blockUntilViewsReceived(caches.toArray(new CacheSPI[0]), VIEW_BLOCK_TIMEOUT);
      TestingUtil.sleepThread(getSleepTimeout());

      Fqn test = BuddyManager.getBackupFqn(caches.get(0).getLocalAddress(), main);

      assertFalse("/a/b/c shld not be bin memory", caches.get(1).exists(test));
      assertNotNull("/a/b/c shld be in CL", caches.get(1).getCacheLoaderManager().getCacheLoader().get(test));
      assertEquals("/a/b/c shld in cache loader", "Joe", caches.get(1).get(test, "name"));
   }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.