1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.buddyreplication; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jgroups.Address; |
11 |
| import org.jgroups.stack.IpAddress; |
12 |
| |
13 |
| import java.net.Inet6Address; |
14 |
| import java.net.InetAddress; |
15 |
| import java.net.NetworkInterface; |
16 |
| import java.util.Enumeration; |
17 |
| import java.util.HashMap; |
18 |
| import java.util.LinkedList; |
19 |
| import java.util.List; |
20 |
| import java.util.Map; |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class NextMemberBuddyLocatorTest extends TestCase |
28 |
| { |
29 |
| private IpAddress dataOwner; |
30 |
| private List<Address> buddies_localhost, buddies_same_host_different_nic, buddies_different_hosts; |
31 |
| |
32 |
16
| protected void setUp()
|
33 |
| { |
34 |
16
| buddies_localhost = new LinkedList<Address>();
|
35 |
16
| buddies_same_host_different_nic = new LinkedList<Address>();
|
36 |
16
| buddies_different_hosts = new LinkedList<Address>();
|
37 |
| |
38 |
16
| try
|
39 |
| { |
40 |
16
| dataOwner = new IpAddress(InetAddress.getByName("localhost"), 1000);
|
41 |
16
| buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 2000));
|
42 |
16
| buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 3000));
|
43 |
16
| buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 4000));
|
44 |
16
| buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 5000));
|
45 |
| |
46 |
| |
47 |
16
| Enumeration en = NetworkInterface.getNetworkInterfaces();
|
48 |
16
| while (en.hasMoreElements())
|
49 |
| { |
50 |
32
| NetworkInterface i = (NetworkInterface) en.nextElement();
|
51 |
32
| for (Enumeration en2 = i.getInetAddresses(); en2.hasMoreElements();)
|
52 |
| { |
53 |
48
| InetAddress addr = (InetAddress) en2.nextElement();
|
54 |
16
| if (addr.isLoopbackAddress() || addr instanceof Inet6Address) continue;
|
55 |
32
| buddies_same_host_different_nic.add(new IpAddress(addr, 1000));
|
56 |
| } |
57 |
| } |
58 |
| |
59 |
| |
60 |
| |
61 |
16
| buddies_different_hosts.add(new IpAddress(InetAddress.getByName("61.62.63.64"), 1000));
|
62 |
16
| buddies_different_hosts.add(new IpAddress(InetAddress.getByName("81.82.83.84"), 1000));
|
63 |
16
| buddies_different_hosts.add(new IpAddress(InetAddress.getByName("101.102.103.104"), 1000));
|
64 |
16
| buddies_different_hosts.add(new IpAddress(InetAddress.getByName("121.122.123.124"), 1000));
|
65 |
| } |
66 |
| catch (Exception e) |
67 |
| { |
68 |
0
| e.printStackTrace();
|
69 |
| } |
70 |
| } |
71 |
| |
72 |
16
| protected void tearDown()
|
73 |
| { |
74 |
16
| buddies_localhost = null;
|
75 |
16
| buddies_same_host_different_nic = null;
|
76 |
16
| buddies_different_hosts = null;
|
77 |
16
| dataOwner = null;
|
78 |
| } |
79 |
| |
80 |
| |
81 |
13
| private List getBuddies(int numBuddies, boolean ignoreColoc, List<Address> candidates)
|
82 |
| { |
83 |
13
| return getBuddies(numBuddies, ignoreColoc, candidates, null);
|
84 |
| } |
85 |
| |
86 |
16
| private List getBuddies(int numBuddies, boolean ignoreColoc, List<Address> candidates, Map<Address, String> buddyPool)
|
87 |
| { |
88 |
16
| NextMemberBuddyLocatorConfig cfg = new NextMemberBuddyLocatorConfig();
|
89 |
16
| cfg.setIgnoreColocatedBuddies(ignoreColoc);
|
90 |
16
| cfg.setNumBuddies(numBuddies);
|
91 |
16
| NextMemberBuddyLocator nmbl = new NextMemberBuddyLocator();
|
92 |
16
| nmbl.init(cfg);
|
93 |
16
| return nmbl.locateBuddies(buddyPool, candidates, dataOwner);
|
94 |
| } |
95 |
| |
96 |
| |
97 |
| |
98 |
1
| public void testSingleBuddyNoColoc()
|
99 |
| { |
100 |
1
| List<Address> list = new LinkedList<Address>();
|
101 |
1
| list.add(dataOwner);
|
102 |
1
| list.add(buddies_localhost.get(0));
|
103 |
1
| list.add(buddies_localhost.get(1));
|
104 |
1
| List results = getBuddies(1, false, list);
|
105 |
| |
106 |
1
| assertEquals(1, results.size());
|
107 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
108 |
| } |
109 |
| |
110 |
1
| public void testThreeBuddiesNoColoc()
|
111 |
| { |
112 |
1
| List<Address> list = new LinkedList<Address>();
|
113 |
1
| list.add(dataOwner);
|
114 |
1
| list.add(buddies_localhost.get(0));
|
115 |
1
| list.add(buddies_localhost.get(1));
|
116 |
1
| list.add(buddies_localhost.get(2));
|
117 |
1
| list.add(buddies_localhost.get(3));
|
118 |
| |
119 |
1
| List results = getBuddies(3, false, list);
|
120 |
| |
121 |
1
| assertEquals(3, results.size());
|
122 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
123 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
124 |
1
| assertEquals(buddies_localhost.get(2), results.get(2));
|
125 |
| } |
126 |
| |
127 |
1
| public void testMoreBuddiesThanAvblNoColoc()
|
128 |
| { |
129 |
1
| List<Address> list = new LinkedList<Address>();
|
130 |
1
| list.add(dataOwner);
|
131 |
1
| list.add(buddies_localhost.get(0));
|
132 |
1
| list.add(buddies_localhost.get(1));
|
133 |
| |
134 |
1
| List results = getBuddies(3, false, list);
|
135 |
| |
136 |
1
| assertEquals(2, results.size());
|
137 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
138 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
139 |
| } |
140 |
| |
141 |
| |
142 |
1
| public void testSingleBuddyWithColocAllCandidatesColoc()
|
143 |
| { |
144 |
1
| List<Address> list = new LinkedList<Address>();
|
145 |
1
| list.add(dataOwner);
|
146 |
1
| list.add(buddies_localhost.get(0));
|
147 |
1
| list.add(buddies_localhost.get(1));
|
148 |
1
| List results = getBuddies(1, true, list);
|
149 |
| |
150 |
1
| assertEquals(1, results.size());
|
151 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
152 |
| } |
153 |
| |
154 |
1
| public void testThreeBuddiesWithColocAllCandidatesColoc()
|
155 |
| { |
156 |
1
| List<Address> list = new LinkedList<Address>();
|
157 |
1
| list.add(dataOwner);
|
158 |
1
| list.add(buddies_localhost.get(0));
|
159 |
1
| list.add(buddies_localhost.get(1));
|
160 |
1
| list.add(buddies_localhost.get(2));
|
161 |
1
| list.add(buddies_localhost.get(3));
|
162 |
| |
163 |
1
| List results = getBuddies(3, true, list);
|
164 |
| |
165 |
1
| assertEquals(3, results.size());
|
166 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
167 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
168 |
1
| assertEquals(buddies_localhost.get(2), results.get(2));
|
169 |
| } |
170 |
| |
171 |
1
| public void testMoreBuddiesThanAvblWithColocAllCandidatesColoc()
|
172 |
| { |
173 |
1
| List<Address> list = new LinkedList<Address>();
|
174 |
1
| list.add(dataOwner);
|
175 |
1
| list.add(buddies_localhost.get(0));
|
176 |
1
| list.add(buddies_localhost.get(1));
|
177 |
| |
178 |
1
| List results = getBuddies(3, true, list);
|
179 |
| |
180 |
1
| assertEquals(2, results.size());
|
181 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
182 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
183 |
| } |
184 |
| |
185 |
| |
186 |
1
| public void testSingleBuddyWithColocAllCandidatesColocDiffNics()
|
187 |
| { |
188 |
1
| List<Address> list = new LinkedList<Address>();
|
189 |
1
| list.add(dataOwner);
|
190 |
1
| list.add(buddies_localhost.get(0));
|
191 |
1
| list.add(buddies_localhost.get(1));
|
192 |
1
| list.addAll(buddies_same_host_different_nic);
|
193 |
1
| List results = getBuddies(1, true, list);
|
194 |
| |
195 |
1
| assertEquals(1, results.size());
|
196 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
197 |
| } |
198 |
| |
199 |
1
| public void testThreeBuddiesWithColocAllCandidatesColocDiffNics()
|
200 |
| { |
201 |
1
| List<Address> list = new LinkedList<Address>();
|
202 |
1
| list.add(dataOwner);
|
203 |
1
| list.add(buddies_localhost.get(0));
|
204 |
1
| list.add(buddies_localhost.get(1));
|
205 |
1
| list.add(buddies_localhost.get(2));
|
206 |
1
| list.add(buddies_localhost.get(3));
|
207 |
1
| list.addAll(buddies_same_host_different_nic);
|
208 |
| |
209 |
1
| List results = getBuddies(3, true, list);
|
210 |
| |
211 |
1
| assertEquals(3, results.size());
|
212 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
213 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
214 |
1
| assertEquals(buddies_localhost.get(2), results.get(2));
|
215 |
| } |
216 |
| |
217 |
1
| public void testMoreBuddiesThanAvblWithColocAllCandidatesColocDiffNics()
|
218 |
| { |
219 |
1
| List<Address> list = new LinkedList<Address>();
|
220 |
1
| list.add(dataOwner);
|
221 |
1
| list.add(buddies_localhost.get(0));
|
222 |
1
| list.add(buddies_localhost.get(1));
|
223 |
1
| list.addAll(buddies_same_host_different_nic);
|
224 |
| |
225 |
1
| List results = getBuddies(3, true, list);
|
226 |
| |
227 |
1
| assertEquals(buddies_same_host_different_nic.isEmpty() ? 2 : 3, results.size());
|
228 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
229 |
1
| assertEquals(buddies_localhost.get(1), results.get(1));
|
230 |
1
| if (!buddies_same_host_different_nic.isEmpty())
|
231 |
1
| assertEquals(buddies_same_host_different_nic.get(0), results.get(2));
|
232 |
| } |
233 |
| |
234 |
| |
235 |
1
| public void testSingleBuddyWithColocDiffHosts()
|
236 |
| { |
237 |
1
| List<Address> list = new LinkedList<Address>();
|
238 |
1
| list.add(dataOwner);
|
239 |
1
| list.add(buddies_localhost.get(0));
|
240 |
1
| list.add(buddies_localhost.get(1));
|
241 |
1
| list.addAll(buddies_same_host_different_nic);
|
242 |
1
| list.addAll(buddies_different_hosts);
|
243 |
1
| List results = getBuddies(1, true, list);
|
244 |
| |
245 |
1
| assertEquals(1, results.size());
|
246 |
1
| assertEquals(buddies_different_hosts.get(0), results.get(0));
|
247 |
| } |
248 |
| |
249 |
1
| public void testThreeBuddiesWithColocDiffHosts()
|
250 |
| { |
251 |
1
| List<Address> list = new LinkedList<Address>();
|
252 |
1
| list.add(dataOwner);
|
253 |
1
| list.add(buddies_localhost.get(0));
|
254 |
1
| list.add(buddies_localhost.get(1));
|
255 |
1
| list.add(buddies_localhost.get(2));
|
256 |
1
| list.add(buddies_localhost.get(3));
|
257 |
1
| list.addAll(buddies_same_host_different_nic);
|
258 |
1
| list.addAll(buddies_different_hosts);
|
259 |
| |
260 |
1
| List results = getBuddies(3, true, list);
|
261 |
| |
262 |
1
| assertEquals(3, results.size());
|
263 |
1
| assertEquals(buddies_different_hosts.get(0), results.get(0));
|
264 |
1
| assertEquals(buddies_different_hosts.get(1), results.get(1));
|
265 |
1
| assertEquals(buddies_different_hosts.get(2), results.get(2));
|
266 |
| } |
267 |
| |
268 |
1
| public void testMoreBuddiesThanAvblWithColocDiffHosts()
|
269 |
| { |
270 |
1
| List<Address> list = new LinkedList<Address>();
|
271 |
1
| list.add(dataOwner);
|
272 |
1
| list.add(buddies_localhost.get(0));
|
273 |
1
| list.add(buddies_localhost.get(1));
|
274 |
1
| list.add(buddies_different_hosts.get(0));
|
275 |
1
| list.add(buddies_different_hosts.get(1));
|
276 |
| |
277 |
| |
278 |
1
| List results = getBuddies(3, true, list);
|
279 |
| |
280 |
1
| assertEquals(3, results.size());
|
281 |
1
| assertEquals(buddies_different_hosts.get(0), results.get(0));
|
282 |
1
| assertEquals(buddies_different_hosts.get(1), results.get(1));
|
283 |
1
| assertEquals(buddies_localhost.get(0), results.get(2));
|
284 |
| } |
285 |
| |
286 |
| |
287 |
1
| public void testSingleLocalBuddyWithPool()
|
288 |
| { |
289 |
1
| List<Address> list = new LinkedList<Address>();
|
290 |
1
| list.add(dataOwner);
|
291 |
1
| list.add(buddies_localhost.get(0));
|
292 |
1
| list.add(buddies_localhost.get(1));
|
293 |
1
| list.add(buddies_localhost.get(2));
|
294 |
| |
295 |
1
| Map<Address, String> pool = new HashMap<Address, String>();
|
296 |
1
| pool.put(dataOwner, "A");
|
297 |
1
| pool.put(buddies_localhost.get(2), "A");
|
298 |
1
| pool.put(buddies_localhost.get(0), "B");
|
299 |
1
| pool.put(buddies_localhost.get(1), "B");
|
300 |
| |
301 |
1
| List results = getBuddies(1, true, list, pool);
|
302 |
| |
303 |
1
| assertEquals(1, results.size());
|
304 |
1
| assertEquals(buddies_localhost.get(2), results.get(0));
|
305 |
| } |
306 |
| |
307 |
| |
308 |
| |
309 |
1
| public void testSingleLocalBuddyWithPoolMixed1()
|
310 |
| { |
311 |
1
| List<Address> list = new LinkedList<Address>();
|
312 |
1
| list.add(dataOwner);
|
313 |
1
| list.add(buddies_localhost.get(0));
|
314 |
1
| list.add(buddies_localhost.get(1));
|
315 |
1
| list.add(buddies_localhost.get(2));
|
316 |
1
| list.add(buddies_different_hosts.get(0));
|
317 |
1
| list.add(buddies_different_hosts.get(1));
|
318 |
| |
319 |
1
| Map<Address, String> pool = new HashMap<Address, String>();
|
320 |
1
| pool.put(dataOwner, "A");
|
321 |
1
| pool.put(buddies_localhost.get(2), "A");
|
322 |
1
| pool.put(buddies_localhost.get(0), "B");
|
323 |
1
| pool.put(buddies_localhost.get(1), "B");
|
324 |
1
| pool.put(buddies_different_hosts.get(0), "C");
|
325 |
1
| pool.put(buddies_different_hosts.get(1), "C");
|
326 |
| |
327 |
1
| List results = getBuddies(1, true, list, pool);
|
328 |
| |
329 |
1
| assertEquals(1, results.size());
|
330 |
1
| assertEquals(buddies_localhost.get(2), results.get(0));
|
331 |
| } |
332 |
| |
333 |
1
| public void testSingleLocalBuddyWithPoolMixed2()
|
334 |
| { |
335 |
1
| List<Address> list = new LinkedList<Address>();
|
336 |
1
| list.add(dataOwner);
|
337 |
1
| list.add(buddies_localhost.get(0));
|
338 |
1
| list.add(buddies_localhost.get(1));
|
339 |
1
| list.add(buddies_localhost.get(2));
|
340 |
1
| list.add(buddies_different_hosts.get(0));
|
341 |
1
| list.add(buddies_different_hosts.get(1));
|
342 |
| |
343 |
1
| Map<Address, String> pool = new HashMap<Address, String>();
|
344 |
1
| pool.put(dataOwner, "A");
|
345 |
1
| pool.put(buddies_localhost.get(2), "B");
|
346 |
1
| pool.put(buddies_localhost.get(0), "B");
|
347 |
1
| pool.put(buddies_localhost.get(1), "B");
|
348 |
1
| pool.put(buddies_different_hosts.get(0), "C");
|
349 |
1
| pool.put(buddies_different_hosts.get(1), "C");
|
350 |
| |
351 |
1
| List results = getBuddies(1, true, list, pool);
|
352 |
| |
353 |
1
| assertEquals(1, results.size());
|
354 |
| |
355 |
1
| assertEquals(buddies_different_hosts.get(0), results.get(0));
|
356 |
| } |
357 |
| |
358 |
1
| public void testWithDataOwnerAtEnd()
|
359 |
| { |
360 |
1
| List<Address> list = new LinkedList<Address>();
|
361 |
1
| list.addAll(buddies_localhost);
|
362 |
1
| list.add(dataOwner);
|
363 |
| |
364 |
1
| List results = getBuddies(1, true, list);
|
365 |
| |
366 |
1
| assertEquals(1, results.size());
|
367 |
1
| assertEquals(buddies_localhost.get(0), results.get(0));
|
368 |
| } |
369 |
| |
370 |
| } |