KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > NextMemberBuddyLocatorTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.buddyreplication;
8
9 import junit.framework.TestCase;
10 import org.jgroups.stack.IpAddress;
11
12 import java.net.InetAddress JavaDoc;
13 import java.net.NetworkInterface JavaDoc;
14 import java.net.Inet6Address JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.HashMap JavaDoc;
20
21 /**
22  * Tests the NextMemberBuddyLocator
23  *
24  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
25  */

26 public class NextMemberBuddyLocatorTest extends TestCase
27 {
28     private IpAddress dataOwner;
29     private List JavaDoc buddies_localhost = new ArrayList JavaDoc();
30     private List JavaDoc buddies_same_host_different_nic = new ArrayList JavaDoc();
31     private List JavaDoc buddies_different_hosts = new ArrayList JavaDoc();
32
33     {
34         try
35         {
36             dataOwner = new IpAddress(InetAddress.getByName("localhost"), 1000);
37             buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 2000));
38             buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 3000));
39             buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 4000));
40             buddies_localhost.add(new IpAddress(InetAddress.getByName("localhost"), 5000));
41
42             // lets get a few more interfaces from the current host
43
Enumeration JavaDoc en= NetworkInterface.getNetworkInterfaces();
44             while(en.hasMoreElements())
45             {
46                 NetworkInterface JavaDoc i=(NetworkInterface JavaDoc)en.nextElement();
47                 for(Enumeration JavaDoc en2=i.getInetAddresses(); en2.hasMoreElements();)
48                 {
49                     InetAddress JavaDoc addr=(InetAddress JavaDoc)en2.nextElement();
50                     if(addr.isLoopbackAddress() || addr instanceof Inet6Address JavaDoc) continue;
51                     buddies_same_host_different_nic.add(new IpAddress(addr, 1000));
52                 }
53             }
54
55             // now lets get some which are definitely on different hosts
56
buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.amazon.com"), 1000));
57             buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.cnn.com"), 1000));
58             buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.google.com"), 1000));
59             buddies_different_hosts.add(new IpAddress(InetAddress.getByName("www.microsoft.com"), 1000));
60         }
61         catch (Exception JavaDoc e)
62         {
63             e.printStackTrace();
64         }
65     }
66
67
68     private List JavaDoc getBuddies(int numBuddies, boolean ignoreColoc, List JavaDoc candidates)
69     {
70         return getBuddies(numBuddies, ignoreColoc, candidates, null);
71     }
72
73     private List JavaDoc getBuddies(int numBuddies, boolean ignoreColoc, List JavaDoc candidates, Map JavaDoc buddyPool)
74     {
75         NextMemberBuddyLocatorConfig cfg = new NextMemberBuddyLocatorConfig();
76         cfg.setIgnoreColocatedBuddies(ignoreColoc);
77         cfg.setNumBuddies(numBuddies);
78         NextMemberBuddyLocator nmbl = new NextMemberBuddyLocator();
79         nmbl.init(cfg);
80         return nmbl.locateBuddies(buddyPool, candidates, dataOwner);
81     }
82
83
84     // without colocation
85

86     public void testSingleBuddyNoColoc()
87     {
88         List JavaDoc list = new ArrayList JavaDoc();
89         list.add(dataOwner);
90         list.add(buddies_localhost.get(0));
91         list.add(buddies_localhost.get(1));
92         List JavaDoc results = getBuddies(1, false, list);
93
94         assertEquals(1, results.size());
95         assertEquals(buddies_localhost.get(0), results.get(0));
96     }
97
98     public void testThreeBuddiesNoColoc()
99     {
100         List JavaDoc list = new ArrayList JavaDoc();
101         list.add(dataOwner);
102         list.add(buddies_localhost.get(0));
103         list.add(buddies_localhost.get(1));
104         list.add(buddies_localhost.get(2));
105         list.add(buddies_localhost.get(3));
106
107         List JavaDoc results = getBuddies(3, false, list);
108
109         assertEquals(3, results.size());
110         assertEquals(buddies_localhost.get(0), results.get(0));
111         assertEquals(buddies_localhost.get(1), results.get(1));
112         assertEquals(buddies_localhost.get(2), results.get(2));
113     }
114
115     public void testMoreBuddiesThanAvblNoColoc()
116     {
117         List JavaDoc list = new ArrayList JavaDoc();
118         list.add(dataOwner);
119         list.add(buddies_localhost.get(0));
120         list.add(buddies_localhost.get(1));
121
122         List JavaDoc results = getBuddies(3, false, list);
123
124         assertEquals(2, results.size());
125         assertEquals(buddies_localhost.get(0), results.get(0));
126         assertEquals(buddies_localhost.get(1), results.get(1));
127     }
128
129     // with colocation, but all candidates are on the same host
130
public void testSingleBuddyWithColocAllCandidatesColoc()
131     {
132         List JavaDoc list = new ArrayList JavaDoc();
133         list.add(dataOwner);
134         list.add(buddies_localhost.get(0));
135         list.add(buddies_localhost.get(1));
136         List JavaDoc results = getBuddies(1, true, list);
137
138         assertEquals(1, results.size());
139         assertEquals(buddies_localhost.get(0), results.get(0));
140     }
141
142     public void testThreeBuddiesWithColocAllCandidatesColoc()
143     {
144         List JavaDoc list = new ArrayList JavaDoc();
145         list.add(dataOwner);
146         list.add(buddies_localhost.get(0));
147         list.add(buddies_localhost.get(1));
148         list.add(buddies_localhost.get(2));
149         list.add(buddies_localhost.get(3));
150
151         List JavaDoc results = getBuddies(3, true, list);
152
153         assertEquals(3, results.size());
154         assertEquals(buddies_localhost.get(0), results.get(0));
155         assertEquals(buddies_localhost.get(1), results.get(1));
156         assertEquals(buddies_localhost.get(2), results.get(2));
157     }
158
159     public void testMoreBuddiesThanAvblWithColocAllCandidatesColoc()
160     {
161         List JavaDoc list = new ArrayList JavaDoc();
162         list.add(dataOwner);
163         list.add(buddies_localhost.get(0));
164         list.add(buddies_localhost.get(1));
165
166         List JavaDoc results = getBuddies(3, true, list);
167
168         assertEquals(2, results.size());
169         assertEquals(buddies_localhost.get(0), results.get(0));
170         assertEquals(buddies_localhost.get(1), results.get(1));
171     }
172
173     // with colocation, all candidates are on the same host but with different NICs
174
public void testSingleBuddyWithColocAllCandidatesColocDiffNics()
175     {
176         List JavaDoc list = new ArrayList JavaDoc();
177         list.add(dataOwner);
178         list.add(buddies_localhost.get(0));
179         list.add(buddies_localhost.get(1));
180         list.addAll(buddies_same_host_different_nic);
181         List JavaDoc results = getBuddies(1, true, list);
182
183         assertEquals(1, results.size());
184         assertEquals(buddies_localhost.get(0), results.get(0));
185     }
186
187     public void testThreeBuddiesWithColocAllCandidatesColocDiffNics()
188     {
189         List JavaDoc list = new ArrayList JavaDoc();
190         list.add(dataOwner);
191         list.add(buddies_localhost.get(0));
192         list.add(buddies_localhost.get(1));
193         list.add(buddies_localhost.get(2));
194         list.add(buddies_localhost.get(3));
195         list.addAll(buddies_same_host_different_nic);
196
197         List JavaDoc results = getBuddies(3, true, list);
198
199         assertEquals(3, results.size());
200         assertEquals(buddies_localhost.get(0), results.get(0));
201         assertEquals(buddies_localhost.get(1), results.get(1));
202         assertEquals(buddies_localhost.get(2), results.get(2));
203     }
204
205     public void testMoreBuddiesThanAvblWithColocAllCandidatesColocDiffNics()
206     {
207         List JavaDoc list = new ArrayList JavaDoc();
208         list.add(dataOwner);
209         list.add(buddies_localhost.get(0));
210         list.add(buddies_localhost.get(1));
211         list.addAll(buddies_same_host_different_nic);
212
213         List JavaDoc results = getBuddies(3, true, list);
214
215         assertEquals(buddies_same_host_different_nic.isEmpty() ? 2 : 3, results.size());
216         assertEquals(buddies_localhost.get(0), results.get(0));
217         assertEquals(buddies_localhost.get(1), results.get(1));
218         if (!buddies_same_host_different_nic.isEmpty()) assertEquals(buddies_same_host_different_nic.get(0), results.get(2));
219     }
220
221     // now for some non-colocated buddies to pick from
222
public void testSingleBuddyWithColocDiffHosts()
223     {
224         List JavaDoc list = new ArrayList JavaDoc();
225         list.add(dataOwner);
226         list.add(buddies_localhost.get(0));
227         list.add(buddies_localhost.get(1));
228         list.addAll(buddies_same_host_different_nic);
229         list.addAll(buddies_different_hosts);
230         List JavaDoc results = getBuddies(1, true, list);
231
232         assertEquals(1, results.size());
233         assertEquals(buddies_different_hosts.get(0), results.get(0));
234     }
235
236     public void testThreeBuddiesWithColocDiffHosts()
237     {
238         List JavaDoc list = new ArrayList JavaDoc();
239         list.add(dataOwner);
240         list.add(buddies_localhost.get(0));
241         list.add(buddies_localhost.get(1));
242         list.add(buddies_localhost.get(2));
243         list.add(buddies_localhost.get(3));
244         list.addAll(buddies_same_host_different_nic);
245         list.addAll(buddies_different_hosts);
246
247         List JavaDoc results = getBuddies(3, true, list);
248
249         assertEquals(3, results.size());
250         assertEquals(buddies_different_hosts.get(0), results.get(0));
251         assertEquals(buddies_different_hosts.get(1), results.get(1));
252         assertEquals(buddies_different_hosts.get(2), results.get(2));
253     }
254
255     public void testMoreBuddiesThanAvblWithColocDiffHosts()
256     {
257         List JavaDoc list = new ArrayList JavaDoc();
258         list.add(dataOwner);
259         list.add(buddies_localhost.get(0));
260         list.add(buddies_localhost.get(1));
261         list.add(buddies_different_hosts.get(0));
262         list.add(buddies_different_hosts.get(1));
263
264
265         List JavaDoc results = getBuddies(3, true, list);
266
267         assertEquals(3, results.size());
268         assertEquals(buddies_different_hosts.get(0), results.get(0));
269         assertEquals(buddies_different_hosts.get(1), results.get(1));
270         assertEquals(buddies_localhost.get(0), results.get(2));
271     }
272
273     // now lets try this with a buddy pool
274
public void testSingleLocalBuddyWithPool()
275     {
276         List JavaDoc list = new ArrayList JavaDoc();
277         list.add( dataOwner );
278         list.add( buddies_localhost.get(0) );
279         list.add( buddies_localhost.get(1) );
280         list.add( buddies_localhost.get(2) );
281
282         Map JavaDoc pool = new HashMap JavaDoc();
283         pool.put(dataOwner, "A");
284         pool.put(buddies_localhost.get(2), "A");
285         pool.put(buddies_localhost.get(0), "B");
286         pool.put(buddies_localhost.get(1), "B");
287
288         List JavaDoc results = getBuddies(1, true, list, pool);
289
290         assertEquals(1, results.size());
291         assertEquals(buddies_localhost.get(2), results.get(0));
292     }
293
294
295     // now lets try this with a buddy pool
296
public void testSingleLocalBuddyWithPoolMixed1()
297     {
298         List JavaDoc list = new ArrayList JavaDoc();
299         list.add( dataOwner );
300         list.add( buddies_localhost.get(0) );
301         list.add( buddies_localhost.get(1) );
302         list.add( buddies_localhost.get(2) );
303         list.add( buddies_different_hosts.get(0) );
304         list.add( buddies_different_hosts.get(1) );
305
306         Map JavaDoc pool = new HashMap JavaDoc();
307         pool.put(dataOwner, "A");
308         pool.put(buddies_localhost.get(2), "A");
309         pool.put(buddies_localhost.get(0), "B");
310         pool.put(buddies_localhost.get(1), "B");
311         pool.put(buddies_different_hosts.get(0), "C");
312         pool.put(buddies_different_hosts.get(1), "C");
313
314         List JavaDoc results = getBuddies(1, true, list, pool);
315
316         assertEquals(1, results.size());
317         assertEquals(buddies_localhost.get(2), results.get(0));
318     }
319
320     public void testSingleLocalBuddyWithPoolMixed2()
321     {
322         List JavaDoc list = new ArrayList JavaDoc();
323         list.add( dataOwner );
324         list.add( buddies_localhost.get(0) );
325         list.add( buddies_localhost.get(1) );
326         list.add( buddies_localhost.get(2) );
327         list.add( buddies_different_hosts.get(0) );
328         list.add( buddies_different_hosts.get(1) );
329
330         Map JavaDoc pool = new HashMap JavaDoc();
331         pool.put(dataOwner, "A");
332         pool.put(buddies_localhost.get(2), "B");
333         pool.put(buddies_localhost.get(0), "B");
334         pool.put(buddies_localhost.get(1), "B");
335         pool.put(buddies_different_hosts.get(0), "C");
336         pool.put(buddies_different_hosts.get(1), "C");
337
338         List JavaDoc results = getBuddies(1, true, list, pool);
339
340         assertEquals(1, results.size());
341         // preference should be for the non-coloc host
342
assertEquals(buddies_different_hosts.get(0), results.get(0));
343     }
344
345     public void testWithDataOwnerAtEnd()
346     {
347         List JavaDoc list = new ArrayList JavaDoc();
348         list.addAll(buddies_localhost);
349         list.add(dataOwner);
350
351         List JavaDoc results = getBuddies(1, true, list);
352
353         assertEquals(1, results.size());
354         assertEquals(buddies_localhost.get(0), results.get(0));
355     }
356
357 }
358
Popular Tags