1 8 9 package org.jboss.test.cache.test.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 AsyncUnitTestCase extends TestCase { 25 TreeCache cache1, cache2; 26 String props=null; 27 28 public AsyncUnitTestCase(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 cache2.setCacheMode(TreeCache.REPL_SYNC); 125 126 try { 127 cache1.put("/a/b/c", "age", new Integer (38)); 128 129 age=(Integer )cache2.get("/a/b/c", "age"); 131 log("attr \"age\" of \"/a/b/c\" on cache2=" + age); 132 assertNotNull("\"age\" obtained from cache2 is null ", age); 133 assertTrue("\"age\" must be 38", age.intValue() == 38); 134 } 135 catch(Exception e) { 136 fail(e.toString()); 137 } 138 } 139 140 141 private void sleep(long timeout) { 142 try { 143 Thread.sleep(timeout); 144 } 145 catch(Throwable t) { 146 ; 147 } 148 } 149 150 void log(String msg) { 151 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 152 } 153 154 155 public static Test suite() { 156 return new TestSuite(AsyncUnitTestCase.class); 157 } 158 159 public static void main(String [] args) { 160 junit.textui.TestRunner.run(suite()); 161 } 162 } 163 | Popular Tags |