1 8 9 package org.jboss.cache.replicated; 10 11 12 import junit.framework.Assert; 13 import junit.framework.Test; 14 import junit.framework.TestCase; 15 import junit.framework.TestSuite; 16 17 import org.jboss.cache.Cache; 18 import org.jboss.cache.CacheImpl; 19 import org.jboss.cache.Fqn; 20 import org.jboss.cache.factories.XmlConfigurationParser; 21 import org.jboss.cache.misc.TestingUtil; 22 23 import javax.transaction.TransactionManager ; 24 25 31 public class AsyncReplTest extends TestCase 32 { 33 CacheImpl cache1, cache2; 34 String props = null; 35 36 public AsyncReplTest(String name) 37 { 38 super(name); 39 } 40 41 protected void setUp() throws Exception 42 { 43 super.setUp(); 44 45 log("creating cache1"); 46 cache1 = createCache("CacheGroup"); 47 48 log("creating cache2"); 49 cache2 = createCache("CacheGroup"); 50 } 51 52 private CacheImpl createCache(String name) throws Exception 53 { 54 CacheImpl cache = new CacheImpl(); 55 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replAsync-service.xml")); 56 cache.getConfiguration().setClusterName(name); 57 58 configureMultiplexer(cache); 60 61 cache.create(); 62 cache.start(); 63 64 validateMultiplexer(cache); 65 66 return cache; 67 } 68 69 76 protected void configureMultiplexer(Cache cache) throws Exception 77 { 78 } 80 81 88 protected void validateMultiplexer(Cache cache) 89 { 90 assertFalse("Cache is not using multiplexer", cache.getConfiguration().isUsingMultiplexer()); 91 } 92 93 protected void tearDown() throws Exception 94 { 95 super.tearDown(); 96 if (cache1 != null) 97 { 98 log("stopping cache1"); 99 cache1.stop(); 100 cache1 = null; 101 } 102 103 if (cache2 != null) 104 { 105 log("stopping cache2"); 106 cache2.stop(); 107 cache2 = null; 108 } 109 } 110 111 112 public void testTxCompletion() throws Exception 113 { 114 Fqn fqn = Fqn.fromString("/a"); 116 String key = "key"; 117 118 cache1.put(fqn, key, "value1"); 119 TestingUtil.sleepThread((long) 500); 121 Assert.assertEquals("value1", cache1.get(fqn, key)); 122 Assert.assertEquals("value1", cache2.get(fqn, key)); 123 124 TransactionManager mgr = cache1.getTransactionManager(); 125 mgr.begin(); 126 127 cache1.put(fqn, key, "value2"); 128 Assert.assertEquals("value2", cache1.get(fqn, key)); 129 Assert.assertEquals("value1", cache2.get(fqn, key)); 130 131 mgr.commit(); 132 133 TestingUtil.sleepThread((long) 500); 134 135 Assert.assertEquals("value2", cache1.get(fqn, key)); 136 Assert.assertEquals("value2", cache2.get(fqn, key)); 137 138 mgr.begin(); 139 cache1.put(fqn, key, "value3"); 140 Assert.assertEquals("value3", cache1.get(fqn, key)); 141 Assert.assertEquals("value2", cache2.get(fqn, key)); 142 143 mgr.rollback(); 144 145 TestingUtil.sleepThread((long) 500); 146 147 Assert.assertEquals("value2", cache1.get(fqn, key)); 148 Assert.assertEquals("value2", cache2.get(fqn, key)); 149 150 if (cache1 != null) 151 { 152 cache1.stop(); 153 cache1 = null; 154 } 155 156 if (cache2 != null) 157 { 158 cache2.stop(); 159 cache2 = null; 160 } 161 162 } 163 164 public void testPutShouldNotReplicateToDifferentCluster() 165 { 166 CacheImpl cache3 = null; 167 try 168 { 169 cache3 = createCache("DifferentGroup"); 170 cache1.put("/a/b/c", "age", 38); 171 TestingUtil.sleepThread((long) 1000); 174 assertNull("Should not have replicated", cache3.get("/a/b/c", "age")); 175 } 176 catch (Exception e) 177 { 178 fail(e.toString()); 179 } 180 finally 181 { 182 if (cache3 != null) 183 { 184 cache3.stop(); 185 } 186 } 187 } 188 189 public void testStateTransfer() 190 { 191 CacheImpl cache4 = null; 192 try 193 { 194 cache1.put("a/b/c", "age", 38); 195 cache4 = createCache("CacheGroup"); 196 System.out.println("" + cache4.getMembers()); 197 assertEquals(3, cache4.getMembers().size()); assertEquals("\"age\" should be 38", 38, cache4.get("/a/b/c", "age")); 199 } 200 catch (Exception e) 201 { 202 fail(e.toString()); 203 } 204 finally 205 { 206 if (cache4 != null) 207 { 208 System.out.println("cache4's view: " + cache4.getMembers()); 209 cache4.stop(); 210 } 211 } 212 } 213 214 215 public void testAsyncReplDelay() 216 { 217 Integer age; 218 219 try 220 { 221 cache1.put("/a/b/c", "age", 38); 222 223 age = (Integer ) cache2.get("/a/b/c", "age"); 225 log("attr \"age\" of \"/a/b/c\" on cache2=" + age); 226 assertTrue("should be either null or 38", age == null || age == 38); 227 } 228 catch (Exception e) 229 { 230 fail(e.toString()); 231 } 232 } 233 234 public void testAsyncReplTxDelay() 235 { 236 Integer age; 237 238 try 239 { 240 TransactionManager tm = cache1.getTransactionManager(); 241 tm.begin(); 242 cache1.put("/a/b/c", "age", 38); 243 tm.commit(); 244 245 age = (Integer ) cache2.get("/a/b/c", "age"); 247 log("attr \"age\" of \"/a/b/c\" on cache2=" + age); 248 assertTrue("should be either null or 38", age == null || age == 38); 249 } 250 catch (Exception e) 251 { 252 fail(e.toString()); 253 } 254 } 255 256 void log(String msg) 257 { 258 System.out.println("-- [" + Thread.currentThread() + "]: " + msg); 259 } 260 261 262 public static Test suite() 263 { 264 return new TestSuite(AsyncReplTest.class); 265 } 266 267 public static void main(String [] args) 268 { 269 junit.textui.TestRunner.run(suite()); 270 } 271 } 272 | Popular Tags |