1 package org.jboss.cache.api; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheSPI; 5 import org.jboss.cache.config.Configuration; 6 import org.jboss.cache.factories.DefaultCacheFactory; 7 import org.jboss.cache.factories.XmlConfigurationParser; 8 9 import java.util.List ; 10 11 public class CacheSPITest extends TestCase 12 { 13 private CacheSPI cache1; 14 private CacheSPI cache2; 15 16 protected boolean optimistic = false; 17 18 protected void setUp() throws Exception 19 { 20 super.setUp(); 21 22 XmlConfigurationParser parser = new XmlConfigurationParser(); 23 Configuration conf1 = parser.parseFile("META-INF/replSync-service.xml"); 24 25 Configuration conf2 = parser.parseFile("META-INF/replSync-service.xml"); 26 27 conf1.setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 28 conf2.setNodeLockingScheme(optimistic ? Configuration.NodeLockingScheme.OPTIMISTIC : Configuration.NodeLockingScheme.PESSIMISTIC); 29 30 cache1 = (CacheSPI) DefaultCacheFactory.getInstance().createCache(conf1, false); 31 cache2 = (CacheSPI) DefaultCacheFactory.getInstance().createCache(conf2, false); 32 } 33 34 protected void tearDown() throws Exception 35 { 36 super.tearDown(); 37 38 if (cache1 != null) 39 { 40 try 41 { 42 cache1.stop(); 43 } 44 catch (Exception e) {} 45 } 46 47 if (cache2 != null) 48 { 49 try 50 { 51 cache2.stop(); 52 } 53 catch (Exception e) {} 54 } 55 } 56 57 public void testGetMembers() throws Exception 58 { 59 60 cache1.start(); 61 List memb1 = cache1.getMembers(); 62 assertEquals("View has one member", 1, memb1.size()); 63 64 Object coord = memb1.get(0); 65 66 cache2.start(); 67 memb1 = cache1.getMembers(); 68 List memb2 = cache2.getMembers(); 69 assertEquals("View has two members", 2, memb1.size()); 70 assertEquals("Both caches have same view", memb1, memb2); 71 72 cache1.stop(); 73 memb2 = cache2.getMembers(); 74 assertEquals("View has one member", 1, memb2.size()); 75 assertFalse("Coordinator changed", coord.equals(memb2.get(0))); 76 } 77 78 public void testIsCoordinator() throws Exception 79 { 80 XmlConfigurationParser parser = new XmlConfigurationParser(); 81 Configuration conf1 = parser.parseFile("META-INF/replSync-service.xml"); 82 83 Configuration conf2 = parser.parseFile("META-INF/replSync-service.xml"); 84 85 cache1 = (CacheSPI) DefaultCacheFactory.getInstance().createCache(conf1, false); 86 cache2 = (CacheSPI) DefaultCacheFactory.getInstance().createCache(conf2, false); 87 88 cache1.start(); 89 assertTrue("Cache1 is coordinator", cache1.getRPCManager().isCoordinator()); 90 91 cache2.start(); 92 assertTrue("Cache1 is still coordinator", cache1.getRPCManager().isCoordinator()); 93 assertFalse("Cache2 is not coordinator", cache2.getRPCManager().isCoordinator()); 94 95 cache1.stop(); 96 assertTrue("Cache2 is coordinator", cache2.getRPCManager().isCoordinator()); 97 98 } 99 } 100 | Popular Tags |