KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > 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.tests.replicated;
10
11
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15 import org.jboss.cache.PropertyConfigurator;
16 import org.jboss.cache.TreeCache;
17
18 /**
19  * Unit test for replicated async TreeCache. Use locking and multiple threads to test
20  * concurrent access to the tree.
21  *
22  * @version $Revision: 1.1.1.1 $
23  */

24 public class AsyncReplTest extends TestCase {
25    TreeCache cache1, cache2;
26    String JavaDoc props=null;
27
28    public AsyncReplTest(String JavaDoc name) {
29       super(name);
30    }
31
32    public void setUp() throws Exception JavaDoc {
33       super.setUp();
34
35       log("creating cache1");
36       cache1=createCache("CacheGroup");
37
38       log("creating cache2");
39       cache2=createCache("CacheGroup");
40    }
41
42    private TreeCache createCache(String JavaDoc name) throws Exception JavaDoc {
43       TreeCache tree=new TreeCache();
44       PropertyConfigurator config=new PropertyConfigurator();
45       config.configure(tree, "META-INF/replAsync-service.xml"); // read in generic replAsync xml
46
tree.setClusterName(name);
47       tree.createService();
48       tree.startService();
49       return tree;
50    }
51
52    public void tearDown() throws Exception JavaDoc {
53       super.tearDown();
54       if(cache1 != null) {
55          log("stopping cache1");
56          cache1.stopService();
57       }
58
59       if(cache2 != null) {
60          log("stopping cache2");
61          cache2.stopService();
62       }
63    }
64
65
66    public void testPutShouldNotReplicateToDifferentCluster() {
67       TreeCache cache3=null;
68       try {
69          cache3=createCache("DifferentGroup");
70          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
71          // because we use async repl, modfication may not yet have been propagated to cache2, so
72
// we have to wait a little
73
sleep(2000);
74          assertNull("Should not have replicated", cache3.get("/a/b/c", "age"));
75       }
76       catch(Exception JavaDoc e) {
77          fail(e.toString());
78       }
79       finally {
80          if(cache3 != null)
81             cache3.stopService();
82       }
83    }
84
85    public void testStateTransfer() {
86       TreeCache cache4=null;
87       try {
88          cache1.put("a/b/c", "age", new Integer JavaDoc(38));
89          cache4=createCache("CacheGroup");
90          assertEquals(3, cache4.getMembers().size()); // cache1, cache2 and cache4
91
assertEquals("\"age\" should be 38", new Integer JavaDoc(38), cache4.get("/a/b/c", "age"));
92       }
93       catch(Exception JavaDoc e) {
94          fail(e.toString());
95       }
96       finally {
97          if(cache4 != null) {
98             System.out.println("cache4's view: " + cache4.getMembers());
99             cache4.stopService();
100          }
101       }
102    }
103
104
105    public void testAsyncReplDelay() {
106       Integer JavaDoc age;
107
108       try {
109          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
110
111          // value on cache2 may be 38 or not yet replicated
112
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
113          log("attr \"age\" of \"/a/b/c\" on cache2=" + age);
114          assertTrue("should be either null or 38", age == null || age.intValue() == 38);
115       }
116       catch(Exception JavaDoc e) {
117          fail(e.toString());
118       }
119    }
120
121    public void testSyncRepl() throws Exception JavaDoc {
122       Integer JavaDoc age;
123       cache1.setCacheMode(TreeCache.REPL_SYNC);
124       cache1.setSyncCommitPhase(true);
125       cache2.setCacheMode(TreeCache.REPL_SYNC);
126       cache2.setSyncCommitPhase(true);
127       
128
129       try {
130          cache1.put("/a/b/c", "age", new Integer JavaDoc(38));
131
132          // value on cache2 must be 38
133
age=(Integer JavaDoc)cache2.get("/a/b/c", "age");
134          log("attr \"age\" of \"/a/b/c\" on cache2=" + age);
135          assertNotNull("\"age\" obtained from cache2 is null ", age);
136          assertTrue("\"age\" must be 38", age.intValue() == 38);
137       }
138       catch(Exception JavaDoc e) {
139          fail(e.toString());
140       }
141    }
142
143
144    private void sleep(long timeout) {
145       try {
146          Thread.sleep(timeout);
147       }
148       catch(Throwable JavaDoc t) {
149          ;
150       }
151    }
152
153    void log(String JavaDoc msg) {
154       System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
155    }
156
157
158    public static Test suite() {
159       return new TestSuite(AsyncReplTest.class);
160    }
161
162    public static void main(String JavaDoc[] args) {
163       junit.textui.TestRunner.run(suite());
164    }
165 }
166
Popular Tags