1 8 package org.jboss.cache.replicated; 9 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.CacheImpl; 16 import org.jboss.cache.config.Configuration; 17 import org.jboss.cache.lock.IsolationLevel; 18 import org.jboss.cache.transaction.DummyTransactionManager; 19 20 import javax.naming.Context ; 21 import javax.transaction.NotSupportedException ; 22 import javax.transaction.RollbackException ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.Transaction ; 25 import java.io.NotSerializableException ; 26 import java.io.Serializable ; 27 28 34 public class ReplicationExceptionTest extends TestCase 35 { 36 CacheImpl cache1, cache2; 37 Configuration.CacheMode caching_mode = Configuration.CacheMode.REPL_SYNC; 38 final String group_name = "TreeCacheTestGroup"; 39 String props = 40 "UDP(ip_mcast=true;ip_ttl=64;loopback=false;mcast_addr=228.1.2.3;" + 41 "mcast_port=45566;mcast_recv_buf_size=80000;mcast_send_buf_size=150000;" + 42 "ucast_recv_buf_size=80000;ucast_send_buf_size=150000):" + 43 "PING(down_thread=true;num_initial_members=2;timeout=500;up_thread=true):" + 44 "MERGE2(max_interval=20000;min_interval=10000):" + 45 "FD(down_thread=true;shun=true;up_thread=true):" + 46 "VERIFY_SUSPECT(down_thread=true;timeout=1500;up_thread=true):" + 47 "pbcast.NAKACK(down_thread=true;gc_lag=50;retransmit_timeout=600,1200,2400,4800;" + 48 "up_thread=true):" + 49 "pbcast.STABLE(desired_avg_gossip=20000;down_thread=true;up_thread=true):" + 50 "UNICAST(down_thread=true;min_threshold=10;timeout=600,1200,2400;window_size=100):" + 51 "FRAG(down_thread=true;frag_size=8192;up_thread=true):" + 52 "pbcast.GMS(join_retry_timeout=2000;join_timeout=5000;print_local_addr=true;shun=true):" + 53 "pbcast.STATE_TRANSFER(down_thread=true;up_thread=true)"; 54 55 final static Log log_ = LogFactory.getLog(ReplicationExceptionTest.class); 56 String old_factory = null; 57 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 58 DummyTransactionManager tx_mgr; 59 Throwable t1_ex, t2_ex, ex = null; 60 61 public ReplicationExceptionTest(String name) 62 { 63 super(name); 64 } 65 66 public void setUp() throws Exception 67 { 68 super.setUp(); 69 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 70 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 71 tx_mgr = DummyTransactionManager.getInstance(); 72 t1_ex = t2_ex = ex = null; 73 } 74 75 public void tearDown() throws Exception 76 { 77 super.tearDown(); 78 DummyTransactionManager.destroy(); 79 destroyCaches(); 80 if (old_factory != null) 81 { 82 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 83 old_factory = null; 84 } 85 } 86 87 Transaction beginTransaction() throws SystemException , NotSupportedException 88 { 89 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 90 mgr.begin(); 91 return mgr.getTransaction(); 92 } 93 94 void initCaches(Configuration.CacheMode caching_mode) throws Exception 95 { 96 this.caching_mode = caching_mode; 97 cache1 = new CacheImpl(); 98 cache2 = new CacheImpl(); 99 cache1.getConfiguration().setCacheMode(caching_mode); 100 cache2.getConfiguration().setCacheMode(caching_mode); 101 cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 102 cache2.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE); 103 104 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 105 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 106 110 cache1.getConfiguration().setLockAcquisitionTimeout(5000); 111 cache2.getConfiguration().setLockAcquisitionTimeout(5000); 112 cache1.start(); 113 cache2.start(); 114 } 115 116 void destroyCaches() throws Exception 117 { 118 if (cache1 != null) 119 cache1.stop(); 120 if (cache2 != null) 121 cache2.stop(); 122 cache1 = null; 123 cache2 = null; 124 } 125 126 public void testNonSerializableRepl() throws Exception 127 { 128 try 129 { 130 initCaches(Configuration.CacheMode.REPL_SYNC); 131 132 cache1.put("/a/b/c", "test", new ContainerData()); 133 134 assertNotNull("NonSerializableData should not be null on cache2", cache2.get("/a/b/c", "test")); 136 } 137 catch (RuntimeException runtime) 138 { 139 Throwable t = runtime.getCause(); 140 if (t instanceof NotSerializableException ) 141 { 142 System.out.println("received NotSerializableException - as expected"); 143 } 144 else 145 { 146 fail("should have received NotSerializableException, but received " + t.getClass()); 147 } 148 } 149 catch (Exception exc) 150 { 151 fail("failure - we should not get here: " + exc); 152 } 153 } 154 155 public void testNonSerizlableReplWithTx() throws Exception 156 { 157 Transaction tx; 158 159 try 160 { 161 initCaches(Configuration.CacheMode.REPL_SYNC); 162 163 tx = beginTransaction(); 164 cache1.put("/a/b/c", "test", new ContainerData()); 165 tx.commit(); 166 167 assertNotNull("NonSerializableData should not be null on cache2", cache2.get("/a/b/c", "test")); 169 } 170 catch (RollbackException rollback) 171 { 172 System.out.println("received RollbackException - as expected"); 173 } 174 catch (Exception e) 175 { 176 fail(e.toString()); 178 } 179 } 180 181 public static Test suite() throws Exception 182 { 183 return new TestSuite(ReplicationExceptionTest.class); 184 } 185 186 187 static class NonSerializabeData 188 { 189 int i; 190 } 191 192 static class ContainerData implements Serializable 193 { 194 int i; 195 NonSerializabeData non_serializable_data; 196 private static final long serialVersionUID = -8322197791060897247L; 197 198 public ContainerData() 199 { 200 i = 99; 201 non_serializable_data = new NonSerializabeData(); 202 } 203 } 204 } 205 | Popular Tags |