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