1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.loader; |
8 |
| |
9 |
| import junit.framework.Assert; |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.cache.CacheImpl; |
13 |
| import org.jboss.cache.DefaultCacheFactory; |
14 |
| import org.jboss.cache.Fqn; |
15 |
| import org.jboss.cache.config.Configuration; |
16 |
| |
17 |
| import java.util.Map; |
18 |
| import java.util.Set; |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| public class ClusteredCacheLoaderTest extends AbstractCacheLoaderTestBase |
26 |
| { |
27 |
| private static Log log = LogFactory.getLog(ClusteredCacheLoaderTest.class); |
28 |
| private CacheImpl cache1, cache2; |
29 |
| private CacheLoader loader1, loader2; |
30 |
| private Fqn fqn = Fqn.fromString("/a"); |
31 |
| private String key = "key"; |
32 |
| |
33 |
4
| protected void setUp() throws Exception
|
34 |
| { |
35 |
0
| if (cache1 != null || cache2 != null) tearDown();
|
36 |
4
| cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
37 |
4
| cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
38 |
| |
39 |
4
| cache1.getConfiguration().setClusterName("CCL-Test");
|
40 |
4
| cache1.getConfiguration().setInitialStateRetrievalTimeout(2000);
|
41 |
4
| cache2.getConfiguration().setClusterName("CCL-Test");
|
42 |
4
| cache2.getConfiguration().setInitialStateRetrievalTimeout(2000);
|
43 |
4
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
44 |
4
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
45 |
| |
46 |
4
| cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false));
|
47 |
4
| cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.ClusteredCacheLoader", "timeout=500", false, false, false));
|
48 |
| |
49 |
4
| cache1.start();
|
50 |
4
| cache2.start();
|
51 |
| |
52 |
4
| loader1 = cache1.getCacheLoaderManager().getCacheLoader();
|
53 |
4
| loader2 = cache2.getCacheLoaderManager().getCacheLoader();
|
54 |
| } |
55 |
| |
56 |
4
| protected void tearDown()
|
57 |
| { |
58 |
4
| if (cache1 != null)
|
59 |
| { |
60 |
4
| cache1.stop();
|
61 |
4
| cache1 = null;
|
62 |
4
| loader1 = null;
|
63 |
| } |
64 |
| |
65 |
4
| if (cache2 != null)
|
66 |
| { |
67 |
4
| cache2.stop();
|
68 |
4
| cache2 = null;
|
69 |
4
| loader2 = null;
|
70 |
| } |
71 |
| } |
72 |
| |
73 |
1
| public void testGetKeyValue() throws Exception
|
74 |
| { |
75 |
1
| cache1.put(fqn, key, "value");
|
76 |
| |
77 |
1
| log.info("Finished put");
|
78 |
| |
79 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
80 |
1
| Assert.assertEquals("value", loader2.get(fqn).get(key));
|
81 |
| |
82 |
1
| cache1.evict(fqn);
|
83 |
| |
84 |
| |
85 |
| |
86 |
1
| Assert.assertEquals("value", loader1.get(fqn).get(key));
|
87 |
1
| Assert.assertNull("Expecting null", loader2.get(fqn));
|
88 |
| |
89 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
90 |
| |
91 |
| |
92 |
| } |
93 |
| |
94 |
1
| public void testGet() throws Exception
|
95 |
| { |
96 |
1
| cache1.put(fqn, key, "value");
|
97 |
| |
98 |
| |
99 |
1
| Map map = loader1.get(fqn);
|
100 |
1
| Assert.assertTrue("Should contain key", map.containsKey(key));
|
101 |
1
| Assert.assertEquals("value", map.get(key));
|
102 |
1
| Assert.assertEquals(1, map.size());
|
103 |
| |
104 |
1
| map = loader2.get(fqn);
|
105 |
1
| Assert.assertTrue("Should contain key", map.containsKey(key));
|
106 |
1
| Assert.assertEquals("value", map.get(key));
|
107 |
1
| Assert.assertEquals(1, map.size());
|
108 |
| |
109 |
1
| cache1.evict(fqn);
|
110 |
| |
111 |
| |
112 |
| |
113 |
1
| map = loader1.get(fqn);
|
114 |
1
| Assert.assertTrue(map.containsKey(key));
|
115 |
1
| Assert.assertEquals("value", map.get(key));
|
116 |
1
| Assert.assertEquals(1, map.size());
|
117 |
| |
118 |
1
| Assert.assertNull("Expecting null", loader2.get(fqn));
|
119 |
1
| map = loader2.get(fqn);
|
120 |
1
| Assert.assertNull("Should be null", map);
|
121 |
| |
122 |
| |
123 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
124 |
| |
125 |
1
| map = loader2.get(fqn);
|
126 |
1
| Assert.assertTrue(map.containsKey(key));
|
127 |
1
| Assert.assertEquals("value", map.get(key));
|
128 |
1
| Assert.assertEquals(1, map.size());
|
129 |
| } |
130 |
| |
131 |
1
| public void testGetChildrenNames() throws Exception
|
132 |
| { |
133 |
1
| cache1.put(fqn, key, "value");
|
134 |
1
| Fqn child1 = new Fqn(fqn, "child1");
|
135 |
1
| Fqn child2 = new Fqn(fqn, "child2");
|
136 |
1
| Fqn child3 = new Fqn(fqn, "child3");
|
137 |
1
| cache1.put(child1, key, "value");
|
138 |
1
| cache1.put(child2, key, "value");
|
139 |
1
| cache1.put(child3, key, "value");
|
140 |
| |
141 |
| |
142 |
1
| Set childNames = loader1.getChildrenNames(fqn);
|
143 |
1
| Assert.assertEquals(3, childNames.size());
|
144 |
1
| childNames = loader2.getChildrenNames(fqn);
|
145 |
1
| Assert.assertEquals(3, childNames.size());
|
146 |
| |
147 |
1
| cache1.evict(child1);
|
148 |
1
| cache1.evict(child2);
|
149 |
1
| cache1.evict(child3);
|
150 |
1
| cache1.evict(fqn);
|
151 |
| |
152 |
| |
153 |
| |
154 |
1
| childNames = loader1.getChildrenNames(fqn);
|
155 |
1
| Assert.assertEquals(3, childNames.size());
|
156 |
| |
157 |
1
| childNames = loader2.getChildrenNames(fqn);
|
158 |
1
| Assert.assertNull("should be null", childNames);
|
159 |
| |
160 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
161 |
| |
162 |
1
| Assert.assertEquals("value", cache1.get(child1, key));
|
163 |
1
| Assert.assertEquals("value", cache1.get(child2, key));
|
164 |
1
| Assert.assertEquals("value", cache1.get(child3, key));
|
165 |
| |
166 |
1
| childNames = loader2.getChildrenNames(fqn);
|
167 |
1
| Assert.assertEquals(3, childNames.size());
|
168 |
| } |
169 |
| |
170 |
1
| public void testExists() throws Exception
|
171 |
| { |
172 |
1
| cache1.put(fqn, key, "value");
|
173 |
| |
174 |
| |
175 |
1
| Assert.assertTrue("should exist", loader1.exists(fqn));
|
176 |
1
| Assert.assertTrue("should exist", loader2.exists(fqn));
|
177 |
| |
178 |
1
| cache1.evict(fqn);
|
179 |
| |
180 |
| |
181 |
| |
182 |
1
| Assert.assertTrue("should exist", loader1.exists(fqn));
|
183 |
1
| Assert.assertTrue("should not exist", !loader2.exists(fqn));
|
184 |
| |
185 |
1
| Assert.assertEquals("value", cache1.get(fqn, key));
|
186 |
| |
187 |
1
| Assert.assertTrue("should exist", loader2.exists(fqn));
|
188 |
| } |
189 |
| } |