1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.arm;
20
21 import java.net.InetAddress;
22
23 import jgroup.core.ConfigManager;
24 import jgroup.core.MemberId;
25 import jgroup.relacs.config.AppConfig;
26 import jgroup.relacs.config.DistributedSystemConfig;
27 import jgroup.relacs.config.Host;
28 import jgroup.relacs.config.HostSet;
29 import jgroup.relacs.types.EndPointImpl;
30 import jgroup.relacs.types.LocalId;
31 import jgroup.relacs.types.MemberIdImpl;
32 import jgroup.relacs.types.ViewImpl;
33 import jgroup.util.JUnit;
34 import junit.framework.Test;
35 import junit.framework.TestCase;
36 import junit.framework.TestSuite;
37
38
39
40
41
42
43 public class TestReplicaCount extends TestCase
44 {
45
46
47 private AppConfig[] apps;
48
49
50 public void testAssignRemoveReplicas() throws Exception
51 {
52 System.out.println("FIRST TEST: testAssignRemoveReplicas()");
53 GroupTable gt = new GroupTable(5000, 2);
54 DistributedSystemConfig dsc = ConfigManager.getDistributedSystem();
55 ReplicaCount rc = new ReplicaCount(gt, dsc.getDomainSet());
56
57
58 assertTrue(!rc.equals(null));
59
60
61 HostSet hs1 = rc.assignReplicas(apps[1]);
62 HostSet hs2 = rc.assignReplicas(apps[2]);
63 System.out.println("hs1: " + hs1);
64 System.out.println("app1: " + apps[1]);
65 System.out.println("hs2: " + hs2);
66 System.out.println("app2: " + apps[2]);
67
68
69 HostSet hs3 = rc.removeReplicas(apps[1]);
70 assertTrue(hs1.equals(hs3));
71 HostSet hs4 = rc.removeReplicas(apps[2]);
72 assertTrue(hs2.equals(hs4));
73
74
75 hs1.addHosts(hs2);
76 hs3.addHosts(hs4);
77 assertTrue(hs1.equals(hs3));
78 }
79
80 public void testReassignReplica() throws Exception
81 {
82 System.out.println("SECOND TEST: testReassignReplica():");
83 GroupTable gt = new GroupTable(5000, 2);
84 DistributedSystemConfig dsc = ConfigManager.getDistributedSystem();
85 ReplicaCount rc = new ReplicaCount(gt, dsc.getDomainSet());
86
87
88 assertTrue(!rc.equals(null));
89
90
91 HostSet hs1 = rc.assignReplicas(apps[1]);
92
93
94 Host[] hostArray = hs1.toArray();
95 Host host1 = rc.reassignReplica(apps[1], hostArray[0]);
96 Host host2 = rc.reassignReplica(apps[1], hostArray[1]);
97 HostSet hs2 = rc.removeReplicas(apps[1]);
98 hs1.addHost(host1);
99 hs1.addHost(host2);
100
101
102 assertTrue(hs2.equals(hs1));
103 }
104
105 public void testCollocateReplicas() throws Exception
106 {
107 System.out.println("FOURTH TEST: testCollocateReplicas()");
108 GroupTable gt = new GroupTable(5000, 2);
109 DistributedSystemConfig dsc = ConfigManager.getDistributedSystem();
110 ReplicaCount rc = new ReplicaCount(gt, dsc.getDomainSet());
111
112
113 assertTrue(!rc.equals(null));
114
115
116 HostSet hs1 = rc.assignReplicas(apps[1]);
117 HostSet hs2 = rc.collocateReplicas(apps[2], apps[1]);
118
119
120 assertTrue(hs1.equals(hs2));
121 HostSet hs3 = rc.removeReplicas(apps[1]);
122 assertTrue(hs1.equals(hs3));
123
124
125
126
127 }
128
129 public void testViewChange() throws Exception
130 {
131 System.out.println("FIFTH TEST: testViewChange()");
132 GroupTable gt = new GroupTable(5000, 2);
133 DistributedSystemConfig dsc = ConfigManager.getDistributedSystem();
134 ReplicaCount rc = new ReplicaCount(gt, dsc.getDomainSet());
135
136
137 assertTrue(!rc.equals(null));
138
139
140
141
142
143
144
145 MemberId[] memTab = new MemberId[2];
146 memTab[0] = generateMemberId("lx0070.ux.his.no");
147 memTab[1] = generateMemberId("lx0071.ux.his.no");
148 ViewImpl newView = new ViewImpl(apps[1].getGroupId(), 1, 0, memTab);
149
150
151
152
153
154
155
156 rc.viewChangeEvent(newView);
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 }
174
175
176
177
178
179
180
181
182
183
184 private MemberId generateMemberId(String host) throws Exception
185 {
186 InetAddress ia = InetAddress.getByName(host);
187 EndPointImpl endPoint = new EndPointImpl(ia, 54300);
188 LocalId localId = new LocalId(1, endPoint);
189 MemberIdImpl memberId = new MemberIdImpl(endPoint, 1, localId);
190 return memberId;
191 }
192
193 public static Test suite() throws Exception
194 {
195 TestSuite suite = new TestSuite();
196 suite.addTest(new TestReplicaCount("testAssignRemoveReplicas"));
197
198
199
200
201 return suite;
202 }
203
204 public static void main(String[] args) throws Exception
205 {
206 junit.textui.TestRunner.run(suite());
207 }
208
209
210
211
212 protected void setUp() throws Exception
213 {
214 super.setUp();
215
216 JUnit.JUNIT_TEST = true;
217
218 String siteconfig = System.getProperty("jgroup.system.config",
219 "file:target/classes/siteconfig/partition-config.xml");
220 ConfigManager.init(siteconfig);
221 apps = AppConfig.getApplications();
222 for (int i = 0; i < apps.length; i++) {
223 try {
224 apps[i].getInitialRedundancy();
225 } catch (IllegalStateException e) {
226 continue;
227 }
228 }
229 }
230
231
232
233
234 protected void tearDown() throws Exception
235 {
236 super.tearDown();
237 }
238
239
240
241
242
243
244 public TestReplicaCount(String name) throws Exception
245 {
246 super(name);
247 }
248 }