1 8 9 package org.jboss.cache.tests.replicated; 10 11 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 import org.jboss.cache.PropertyConfigurator; 16 import org.jboss.cache.TreeCache; 17 18 24 public class AsyncReplTest extends TestCase { 25 TreeCache cache1, cache2; 26 String props=null; 27 28 public AsyncReplTest(String name) { 29 super(name); 30 } 31 32 public void setUp() throws Exception { 33 super.setUp(); 34 35 log("creating cache1"); 36 cache1=createCache("CacheGroup"); 37 38 log("creating cache2"); 39 cache2=createCache("CacheGroup"); 40 } 41 42 private TreeCache createCache(String name) throws Exception { 43 TreeCache tree=new TreeCache(); 44 PropertyConfigurator config=new PropertyConfigurator(); 45 config.configure(tree, "META-INF/replAsync-service.xml"); tree.setClusterName(name); 47 tree.createService(); 48 tree.startService(); 49 return tree; 50 } 51 52 public void tearDown() throws Exception { 53 super.tearDown(); 54 if(cache1 != null) { 55 log("stopping cache1"); 56 cache1.stopService(); 57 } 58 59 if(cache2 != null) { 60 log("stopping cache2"); 61 cache2.stopService(); 62 } 63 } 64 65 66 public void testPutShouldNotReplicateToDifferentCluster() { 67 TreeCache cache3=null; 68 try { 69 cache3=createCache("DifferentGroup"); 70 cache1.put("/a/b/c", "age", new Integer (38)); 71 sleep(2000); 74 assertNull("Should not have replicated", cache3.get("/a/b/c", "age")); 75 } 76 catch(Exception e) { 77 fail(e.toString()); 78 } 79 finally { 80 if(cache3 != null) 81 cache3.stopService(); 82 } 83 } 84 85 public void testStateTransfer() { 86 TreeCache cache4=null; 87 try { 88 cache1.put("a/b/c", "age", new Integer (38)); 89 cache4=createCache("CacheGroup"); 90 assertEquals(3, cache4.getMembers().size()); assertEquals("\"age\" should be 38", new Integer (38), cache4.get("/a/b/c", "age")); 92 } 93 catch(Exception e) { 94 fail(e.toString()); 95 } 96 finally { 97 if(cache4 != null) { 98 System.out.println("cache4's view: " + cache4.getMembers()); 99 cache4.stopService(); 100 } 101 } 102 } 103 104 105 public void testAsyncReplDelay() { 106 Integer age; 107 108 try { 109 cache1.put("/a/b/c", "age", new Integer (38)); 110 111 age=(Integer )cache2.get("/a/b/c", "age"); 113 log("attr \"age\" of \"/a/b/c\" on cache2=" + age); 114 assertTrue("should be either null or 38", age == null || age.intValue() == 38); 115 } 116 catch(Exception e) { 117 fail(e.toString()); 118 } 119 } 120 121 public void testSyncRepl() throws Exception { 122 Integer age; 123 cache1.setCacheMode(TreeCache.REPL_SYNC); 124 cache1.setSyncCommitPhase(true); 125 cache2.setCacheMode(TreeCache.REPL_SYNC); 126 cache2.setSyncCommitPhase(true); 127 128 129 try { 130 cache1.put("/a/b/c", "age", new Integer (38)); 131 132 age=(Integer )cache2.get("/a/b/c", "age"); 134 log("attr \"age\" of \"/a/b/c\" on cache2=" + age); 135 assertNotNull("\"age\" obtained from cache2 is null ", age); 136 assertTrue("\"age\" must be 38", age.intValue() == 38); 137 } 138 catch(Exception e) { 139 fail(e.toString()); 140 } 141 } 142 143 144 private void sleep(long timeout) { 145 try { 146 Thread.sleep(timeout); 147 } 148 catch(Throwable t) { 149 ; 150 } 151 } 152 153 void log(String msg) { 154 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 155 } 156 157 158 public static Test suite() { 159 return new TestSuite(AsyncReplTest.class); 160 } 161 162 public static void main(String [] args) { 163 junit.textui.TestRunner.run(suite()); 164 } 165 } 166 | Popular Tags |