1 8 package org.jboss.cache.tests.replicated; 9 10 import EDU.oswego.cs.dl.util.concurrent.FIFOSemaphore; 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.jboss.cache.CacheException; 17 import org.jboss.cache.Fqn; 18 import org.jboss.cache.TreeCache; 19 import org.jboss.cache.TreeCacheListener; 20 import org.jboss.cache.lock.IsolationLevel; 21 import org.jboss.cache.transaction.DummyTransactionManager; 22 import org.jgroups.View; 23 24 import javax.naming.Context ; 25 import javax.transaction.NotSupportedException ; 26 import javax.transaction.SystemException ; 27 import javax.transaction.Transaction ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 37 public class SyncCacheListenerTest extends TestCase { 38 TreeCache cache1, cache2; 39 int caching_mode=TreeCache.REPL_SYNC; 40 final String group_name="TreeCacheTestGroup"; 41 String props= 42 "UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;" + 43 "mcast_port=45566;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;" + 44 "ucast_recv_buf_size=80000;ucast_send_buf_size=150000):" + 45 "PING(down_thread=true;num_initial_members=2;timeout=500;up_thread=true):" + 46 "MERGE2(max_interval=20000;min_interval=10000):" + 47 "FD(down_thread=true;shun=true;up_thread=true):" + 48 "VERIFY_SUSPECT(down_thread=true;timeout=1500;up_thread=true):" + 49 "pbcast.NAKACK(down_thread=true;gc_lag=50;retransmit_timeout=600,1200,2400,4800;" + 50 "up_thread=true):" + 51 "pbcast.STABLE(desired_avg_gossip=20000;down_thread=true;up_thread=true):" + 52 "UNICAST(down_thread=true;min_threshold=10;timeout=600,1200,2400;window_size=100):" + 53 "FRAG(down_thread=true;frag_size=8192;up_thread=true):" + 54 "pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):" + 55 "pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)"; 56 57 final static Log log_=LogFactory.getLog(SyncCacheListenerTest.class); 58 String old_factory=null; 59 final String FACTORY="org.jboss.cache.transaction.DummyContextFactory"; 60 FIFOSemaphore lock=new FIFOSemaphore(1); 61 DummyTransactionManager tx_mgr; 62 Throwable t1_ex, t2_ex, ex=null; 63 64 65 66 public SyncCacheListenerTest(String name) { 67 super(name); 68 } 69 70 public void setUp() throws Exception { 71 super.setUp(); 72 old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 73 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 74 tx_mgr=DummyTransactionManager.getInstance(); 75 t1_ex=t2_ex=ex=null; 76 } 77 78 public void tearDown() throws Exception { 79 super.tearDown(); 80 DummyTransactionManager.destroy(); 81 destroyCaches(); 82 if(old_factory != null) { 83 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 84 old_factory=null; 85 } 86 } 87 88 Transaction beginTransaction() throws SystemException , NotSupportedException { 89 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 90 mgr.begin(); 91 Transaction tx=mgr.getTransaction(); 92 return tx; 93 } 94 95 void initCaches(int caching_mode) throws Exception { 96 this.caching_mode=caching_mode; 97 cache1=new TreeCache(); 98 cache2=new TreeCache(); 99 cache1.setCacheMode(caching_mode); 100 cache2.setCacheMode(caching_mode); 101 cache1.setIsolationLevel(IsolationLevel.SERIALIZABLE); 102 cache2.setIsolationLevel(IsolationLevel.SERIALIZABLE); 103 104 cache1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 105 cache2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 106 110 cache1.setLockAcquisitionTimeout(5000); 111 cache2.setLockAcquisitionTimeout(5000); 112 cache1.start(); 113 cache2.start(); 114 } 115 116 void destroyCaches() throws Exception { 117 if(cache1 != null) 118 cache1.stop(); 119 if(cache2 != null) 120 cache2.stop(); 121 cache1=null; 122 cache2=null; 123 } 124 125 public void testSyncTxRepl() throws Exception { 126 Integer age; 127 Transaction tx; 128 129 try { 130 initCaches(TreeCache.REPL_SYNC); 131 132 tx=beginTransaction(); 133 Listener lis = new Listener(); 134 cache1.addTreeCacheListener(lis); 135 lis.put("/a/b/c", "age", new Integer (38)); 136 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age")); 137 tx.commit(); 138 139 age=(Integer )cache2.get("/a/b/c", "age"); 141 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 142 assertTrue("\"age\" must be 38", age.intValue() == 38); 143 } 144 catch(Exception e) { 145 fail(e.toString()); 146 } 147 } 148 149 150 public void testSyncRepl() throws Exception { 151 Integer age; 152 try { 153 initCaches(TreeCache.REPL_SYNC); 154 155 Listener lis = new Listener(); 156 cache1.addTreeCacheListener(lis); 157 lis.put("/a/b/c", "age", new Integer (38)); 158 159 age=(Integer )cache2.get("/a/b/c", "age"); 161 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 162 assertTrue("\"age\" must be 38", age.intValue() == 38); 163 } 164 catch(Exception e) { 165 fail(e.toString()); 166 } 167 } 168 169 public void testSyncTxReplMap() throws Exception { 170 Integer age; 171 Transaction tx; 172 173 try { 174 initCaches(TreeCache.REPL_SYNC); 175 176 tx=beginTransaction(); 177 Listener lis = new Listener(); 178 cache1.addTreeCacheListener(lis); 179 Map map = new HashMap (); 180 map.put("age", new Integer (38)); 181 map.put("name", "Ben"); 182 lis.put("/a/b/c", map); 183 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age")); 184 tx.commit(); 185 186 age=(Integer )cache2.get("/a/b/c", "age"); 188 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 189 assertTrue("\"age\" must be 38", age.intValue() == 38); 190 } 191 catch(Exception e) { 192 fail(e.toString()); 193 } 194 } 195 196 197 public void testSyncReplMap() throws Exception { 198 Integer age; 199 try { 200 initCaches(TreeCache.REPL_SYNC); 201 202 Listener lis = new Listener(); 203 cache1.addTreeCacheListener(lis); 204 Map map = new HashMap (); 205 map.put("age", new Integer (38)); 206 map.put("name", "Ben"); 207 lis.put("/a/b/c", map); 208 209 age=(Integer )cache2.get("/a/b/c", "age"); 211 assertNotNull("\"age\" obtained from cache2 must be non-null ", age); 212 assertTrue("\"age\" must be 38", age.intValue() == 38); 213 } 214 catch(Exception e) { 215 fail(e.toString()); 216 } 217 } 218 219 220 class Listener implements TreeCacheListener 221 { 222 Object key_ = null; 223 public void put(String fqn, Object key, Object val) throws Exception 224 { 225 key_ = key; 226 cache1.put(fqn, key, val); 227 } 228 229 public void put(String fqn, Map map) throws Exception 230 { 231 if(map.size() == 0) fail("put(): map size can't be 0"); 232 Set set = map.keySet(); 233 key_ = set.iterator().next(); cache1.put(fqn, map); 235 } 236 237 238 public void nodeCreated(Fqn fqn) { 239 } 241 242 public void nodeRemoved(Fqn fqn) { 243 } 245 246 public void nodeLoaded(Fqn fqn) { 247 } 249 250 public void nodeEvicted(Fqn fqn) { 251 } 253 254 public void nodeModified(Fqn fqn) 255 { 256 log_.debug("nodeModified visited with fqn: " +fqn); 257 try { 258 cache1.get(fqn, key_); 260 } catch (CacheException e) { 261 e.printStackTrace(); fail("nodeModified: test failed with exception: " +e); 263 } 264 } 265 266 public void nodeVisited(Fqn fqn) { 267 } 269 270 public void cacheStarted(TreeCache cache) { 271 } 273 274 public void cacheStopped(TreeCache cache) { 275 } 277 278 public void viewChange(View new_view) { 280 } 282 } 283 284 static void _pause(long millis) { 285 try { 286 Thread.sleep(millis); 287 } 288 catch(Exception t) { 289 } 290 } 291 292 public static Test suite() throws Exception { 293 return new TestSuite(SyncCacheListenerTest.class); 295 } 296 297 298 } 299 | Popular Tags |