1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.api; |
9 |
| |
10 |
| import junit.framework.TestCase; |
11 |
| import org.jboss.cache.Cache; |
12 |
| import org.jboss.cache.CacheSPI; |
13 |
| import org.jboss.cache.DefaultCacheFactory; |
14 |
| import org.jboss.cache.Fqn; |
15 |
| import org.jboss.cache.InvocationContext; |
16 |
| import org.jboss.cache.Node; |
17 |
| import org.jboss.cache.config.Configuration.CacheMode; |
18 |
| import org.jboss.cache.config.Option; |
19 |
| import org.jboss.cache.factories.UnitTestCacheFactory; |
20 |
| import org.jboss.cache.misc.TestingUtil; |
21 |
| |
22 |
| import java.util.HashMap; |
23 |
| import java.util.Map; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class SyncReplTest extends TestCase |
29 |
| { |
30 |
| private CacheSPI[] caches; |
31 |
| |
32 |
3
| protected void setUp()
|
33 |
| { |
34 |
3
| System.out.println("*** In setUp()");
|
35 |
3
| caches = new CacheSPI[2];
|
36 |
3
| caches[0] = (CacheSPI) DefaultCacheFactory.getInstance().createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
|
37 |
3
| caches[1] = (CacheSPI) DefaultCacheFactory.getInstance().createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
|
38 |
| |
39 |
3
| TestingUtil.blockUntilViewsReceived(caches, 5000);
|
40 |
3
| System.out.println("*** Finished setUp()");
|
41 |
| } |
42 |
| |
43 |
3
| protected void tearDown()
|
44 |
| { |
45 |
3
| System.out.println("*** In tearDown()");
|
46 |
3
| if (caches != null)
|
47 |
| { |
48 |
3
| for (Cache c : caches)
|
49 |
| { |
50 |
6
| c.stop();
|
51 |
6
| c = null;
|
52 |
| } |
53 |
3
| caches = null;
|
54 |
| } |
55 |
3
| System.out.println("*** Finished tearDown()");
|
56 |
| } |
57 |
| |
58 |
1
| public void testBasicOperation()
|
59 |
| { |
60 |
1
| assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
|
61 |
1
| assertInvocationContextInitState();
|
62 |
| |
63 |
1
| Fqn f = Fqn.fromString("/test/data");
|
64 |
1
| String k = "key", v = "value";
|
65 |
| |
66 |
1
| assertNull("Should be null", caches[0].getRoot().getChild(f));
|
67 |
1
| assertNull("Should be null", caches[1].getRoot().getChild(f));
|
68 |
| |
69 |
1
| Node node = caches[0].getRoot().addChild(f);
|
70 |
| |
71 |
1
| assertNotNull("Should not be null", node);
|
72 |
| |
73 |
1
| node.put(k, v);
|
74 |
| |
75 |
1
| assertEquals(v, node.get(k));
|
76 |
1
| assertEquals(v, caches[0].get(f, k));
|
77 |
1
| assertEquals("Should have replicated", v, caches[1].get(f, k));
|
78 |
| } |
79 |
| |
80 |
1
| public void testSyncRepl()
|
81 |
| { |
82 |
1
| assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
|
83 |
1
| assertInvocationContextInitState();
|
84 |
| |
85 |
1
| Fqn fqn = Fqn.fromString("/JSESSIONID/1010.10.5:3000/1234567890/1");
|
86 |
1
| caches[0].getConfiguration().setSyncCommitPhase(true);
|
87 |
1
| caches[1].getConfiguration().setSyncCommitPhase(true);
|
88 |
| |
89 |
| |
90 |
1
| caches[0].put(fqn, "age", 38);
|
91 |
1
| assertEquals("Value should be set", 38, caches[0].get(fqn, "age"));
|
92 |
1
| assertEquals("Value should have replicated", 38, caches[1].get(fqn, "age"));
|
93 |
| } |
94 |
| |
95 |
1
| public void testPutMap()
|
96 |
| { |
97 |
1
| assertClusterSize("Should only be 2 caches in the cluster!!!", 2);
|
98 |
1
| assertInvocationContextInitState();
|
99 |
| |
100 |
1
| Fqn fqn = Fqn.fromString("/JSESSIONID/10.10.10.5:3000/1234567890/1");
|
101 |
1
| Fqn fqn1 = Fqn.fromString("/JSESSIONID/10.10.10.5:3000/1234567890/2");
|
102 |
| |
103 |
1
| Map map = new HashMap();
|
104 |
1
| map.put("1", "1");
|
105 |
1
| map.put("2", "2");
|
106 |
1
| caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
107 |
1
| caches[0].getRoot().addChild(fqn).putAll(map);
|
108 |
1
| caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
109 |
1
| assertEquals("Value should be set", "1", caches[0].get(fqn, "1"));
|
110 |
| |
111 |
1
| map = new HashMap();
|
112 |
1
| map.put("3", "3");
|
113 |
1
| map.put("4", "4");
|
114 |
1
| caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
115 |
1
| caches[0].getRoot().addChild(fqn1).putAll(map);
|
116 |
| |
117 |
1
| caches[0].getInvocationContext().getOptionOverrides().setSuppressLocking(true);
|
118 |
1
| assertEquals("Value should be set", "2", caches[0].get(fqn, "2"));
|
119 |
| } |
120 |
| |
121 |
| |
122 |
3
| private void assertClusterSize(String message, int size)
|
123 |
| { |
124 |
3
| for (Cache c : caches)
|
125 |
| { |
126 |
6
| assertClusterSize(message, size, c);
|
127 |
| } |
128 |
| } |
129 |
| |
130 |
6
| private void assertClusterSize(String message, int size, Cache c)
|
131 |
| { |
132 |
6
| assertEquals(message, size, c.getMembers().size());
|
133 |
| } |
134 |
| |
135 |
3
| private void assertInvocationContextInitState()
|
136 |
| { |
137 |
3
| for (Cache c : caches)
|
138 |
| { |
139 |
6
| assertInvocationContextInitState(c);
|
140 |
| } |
141 |
| } |
142 |
| |
143 |
6
| private void assertInvocationContextInitState(Cache c)
|
144 |
| { |
145 |
6
| InvocationContext ctx = c.getInvocationContext();
|
146 |
6
| InvocationContext control = null;
|
147 |
6
| try
|
148 |
| { |
149 |
6
| control = ctx.clone();
|
150 |
| } |
151 |
| catch (CloneNotSupportedException e) |
152 |
| { |
153 |
0
| fail("Unable to clone InvocationContext");
|
154 |
| } |
155 |
| |
156 |
6
| control.reset();
|
157 |
6
| control.setOptionOverrides(new Option());
|
158 |
| |
159 |
6
| assertEquals("Should be equal", control, ctx);
|
160 |
| } |
161 |
| |
162 |
| |
163 |
| } |