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