KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > replicated > AsyncReplTest


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
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
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 JavaDoc;
24
25 /**
26  * Unit test for replicated async CacheImpl. Use locking and multiple threads to test
27  * concurrent access to the tree.
28  *
29  * @version $Revision: 1.14 $
30  */

31 public class AsyncReplTest extends TestCase
32 {
33    CacheImpl cache1, cache2;
34    String JavaDoc props = null;
35
36    public AsyncReplTest(String JavaDoc name)
37    {
38       super(name);
39    }
40
41    protected void setUp() throws Exception JavaDoc
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 JavaDoc name) throws Exception JavaDoc
53    {
54       CacheImpl cache = new CacheImpl();
55       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replAsync-service.xml"));
56       cache.getConfiguration().setClusterName(name);
57       
58       // Call the hook that allows mux integration
59
configureMultiplexer(cache);
60       
61       cache.create();
62       cache.start();
63       
64       validateMultiplexer(cache);
65       
66       return cache;
67    }
68    
69    /**
70     * Provides a hook for multiplexer integration. This default implementation
71     * is a no-op; subclasses that test mux integration would override
72     * to integrate the given cache with a multiplexer.
73     *
74     * param cache a cache that has been configured but not yet created.
75     */

76    protected void configureMultiplexer(Cache cache) throws Exception JavaDoc
77    {
78       // default does nothing
79
}
80    
81    /**
82     * Provides a hook to check that the cache's channel came from the
83     * multiplexer, or not, as expected. This default impl asserts that
84     * the channel did not come from the multiplexer.
85     *
86     * @param cache a cache that has already been started
87     */

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 JavaDoc
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 JavaDoc
113    {
114       // test a very simple replication.
115
Fqn fqn = Fqn.fromString("/a");
116       String JavaDoc key = "key";
117
118       cache1.put(fqn, key, "value1");
119       // allow for replication
120
TestingUtil.sleepThread((long) 500);
121       Assert.assertEquals("value1", cache1.get(fqn, key));
122       Assert.assertEquals("value1", cache2.get(fqn, key));
123
124       TransactionManager JavaDoc 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          // because we use async repl, modfication may not yet have been propagated to cache2, so
172
// we have to wait a little
173
TestingUtil.sleepThread((long) 1000);
174          assertNull("Should not have replicated", cache3.get("/a/b/c", "age"));
175       }
176       catch (Exception JavaDoc 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()); // cache1, cache2 and cache4
198
assertEquals("\"age\" should be 38", 38, cache4.get("/a/b/c", "age"));
199       }
200       catch (Exception JavaDoc 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 JavaDoc age;
218
219       try
220       {
221          cache1.put("/a/b/c", "age", 38);
222
223          // value on cache2 may be 38 or not yet replicated
224
age = (Integer JavaDoc) 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 JavaDoc e)
229       {
230          fail(e.toString());
231       }
232    }
233
234    public void testAsyncReplTxDelay()
235    {
236       Integer JavaDoc age;
237
238       try
239       {
240          TransactionManager JavaDoc tm = cache1.getTransactionManager();
241          tm.begin();
242          cache1.put("/a/b/c", "age", 38);
243          tm.commit();
244
245          // value on cache2 may be 38 or not yet replicated
246
age = (Integer JavaDoc) 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 JavaDoc e)
251       {
252          fail(e.toString());
253       }
254    }
255
256    void log(String JavaDoc 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 JavaDoc[] args)
268    {
269       junit.textui.TestRunner.run(suite());
270    }
271 }
272
Popular Tags