1   package jgroup.relacs.daemon;
2   
3   import java.net.InetAddress;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import jgroup.core.EndPoint;
8   import jgroup.relacs.config.TransportConfig;
9   import jgroup.relacs.mss.MssHost;
10  import jgroup.relacs.types.EndPointImpl;
11  import jgroup.util.Util;
12  import junit.framework.TestCase;
13  
14  public class EstimateTest extends TestCase
15  {
16  
17    private SendBuffer sbuffer;
18    private EndPoint[] endpoints;
19    private Map<EndPoint, HostData> thosts;
20    private EndPoint[] rset;
21  
22    protected void setUp() throws Exception
23    {
24      super.setUp();
25      sbuffer = new SendBuffer();
26  
27      thosts = new HashMap<EndPoint, HostData>();
28      endpoints = new EndPointImpl[6];
29      endpoints[0] = new EndPointImpl(InetAddress.getByName("ba3.ux.uis.no"), 2121);
30      endpoints[1] = new EndPointImpl(InetAddress.getByName("ba4.ux.uis.no"), 2121);
31      endpoints[2] = new EndPointImpl(InetAddress.getByName("jo10.ux.uis.no"), 2121);
32      endpoints[3] = new EndPointImpl(InetAddress.getByName("wolfpack01.item.ntnu.no"), 2121);
33      endpoints[4] = new EndPointImpl(InetAddress.getByName("wolfpack02.item.ntnu.no"), 2121);
34      endpoints[5] = new EndPointImpl(InetAddress.getByName("wolfpack03.item.ntnu.no"), 2121);
35  
36      TransportConfig config = new TransportConfig();
37      for (EndPoint endpoint : endpoints) {
38        MssHost mssHost = new MssHost(config, endpoint, null, null, null);
39        HostData host = new HostData(mssHost);
40        thosts.put(endpoint, host);
41      }
42  
43      rset = new EndPointImpl[3];
44      rset[0] = endpoints[0];
45      rset[1] = endpoints[1];
46      rset[2] = endpoints[2];
47    }
48  
49    /*
50     * Test method for 'jgroup.relacs.daemon.Estimate.reset(Map, EndPoint[])'
51     */
52    public void testReset()
53    {
54  
55    }
56  
57    /*
58     * Test method for 'jgroup.relacs.daemon.Estimate.intersect(EndPoint[], int[])'
59     */
60    public void testIntersect()
61    {
62      // Check identity
63      EndPoint[] P = new EndPointImpl[3];
64      P[0] = endpoints[0];
65      P[1] = endpoints[1];
66      P[2] = endpoints[2];
67      Estimate estP = new Estimate(sbuffer, thosts, P);
68      Estimate estimate = new Estimate(sbuffer, thosts, rset);
69      estimate.intersect(P);
70      assertTrue(estP.equals(estimate));
71  
72      // One host less
73      P = new EndPointImpl[2];
74      P[0] = endpoints[0];
75      P[1] = endpoints[1];
76      estP = new Estimate(sbuffer, thosts, P);
77      estimate = new Estimate(sbuffer, thosts, rset);
78      estimate.intersect(P);
79      assertTrue(estP.equals(estimate));
80  
81      // Two hosts less
82      P = new EndPointImpl[1];
83      P[0] = endpoints[0];
84      estP = new Estimate(sbuffer, thosts, P);
85      estimate = new Estimate(sbuffer, thosts, rset);
86      estimate.intersect(P);
87      assertTrue(estP.equals(estimate));
88  
89      // No hosts; gives empty estimate after intersection
90      P = new EndPointImpl[0];
91      estP = new Estimate(sbuffer, thosts, P);
92      estimate = new Estimate(sbuffer, thosts, rset);
93      estimate.intersect(P);
94      assertTrue(estP.equals(estimate));
95  
96      P = new EndPointImpl[2];
97      P[0] = endpoints[0];
98      P[1] = endpoints[3];  // not in the original estimate
99      estP = new Estimate(sbuffer, thosts, new EndPoint[] { endpoints[0] });
100     estimate = new Estimate(sbuffer, thosts, rset);
101     estimate.intersect(P);
102     System.out.println(estP);
103     System.out.println(estimate);
104     assertTrue(estP.equals(estimate));
105   }
106 
107   /*
108    * Test method for 'jgroup.relacs.daemon.Estimate.updateAgreed(EndPoint[], int[])'
109    */
110   public void testUpdateAgreed()
111   {
112     Estimate estimate = new Estimate(sbuffer, thosts, rset);
113     for (HostData host : estimate.getHosts()) {
114       assertFalse(host.getAgreed() == 1);
115     }
116 
117     // Check identity
118     EndPoint[] P = new EndPointImpl[3];
119     P[0] = endpoints[0];
120     P[1] = endpoints[1];
121     P[2] = endpoints[2];
122     int[] agreed = new int[] { 1, 1, 1 };
123     estimate.updateAgreed(P, agreed);
124     assertTrue(estimate.checkAgreed(P, agreed));
125     for (HostData host : estimate.getHosts()) {
126       assertTrue(host.getAgreed() == 1);
127     }
128 
129     // One host less
130     P = new EndPointImpl[2];
131     P[0] = endpoints[0];
132     P[1] = endpoints[1];
133     agreed = new int[] { 2, 2 };
134     estimate.updateAgreed(P, agreed);
135     assertTrue(estimate.checkAgreed(P, agreed));
136     for (HostData host : estimate.getHosts()) {
137       if (Util.in(P, host.getEndPoint())) {
138         assertTrue(host.getAgreed() == 2);
139       } else {
140         assertTrue(host.getAgreed() == 1);
141       }
142     }
143 
144     // Two hosts less
145     P = new EndPointImpl[1];
146     P[0] = endpoints[0];
147     agreed = new int[] { 3 };
148     estimate.updateAgreed(P, agreed);
149     assertTrue(estimate.checkAgreed(P, agreed));
150     for (HostData host : estimate.getHosts()) {
151       if (Util.in(P, host.getEndPoint())) {
152         assertTrue(host.getAgreed() == 3);
153       } else {
154         assertTrue(host.getAgreed() == 1 || host.getAgreed() == 2);
155       }
156     }
157 
158     P = new EndPointImpl[2];
159     P[0] = endpoints[0];
160     P[1] = endpoints[3];  // not in the original estimate
161     agreed = new int[] { 99, 99 };
162     estimate.updateAgreed(P, agreed);
163     assertTrue(estimate.checkAgreed(P, agreed));
164     for (HostData host : estimate.getHosts()) {
165       if (Util.in(P, host.getEndPoint())) {
166         assertTrue(host.getAgreed() == 99);
167       } else {
168         assertTrue(host.getAgreed() <= 3);
169       }
170     }
171   }
172 
173   /*
174    * Test method for 'jgroup.relacs.daemon.Estimate.remove(EndPoint)'
175    */
176   public void testRemoveEndPoint()
177   {
178 
179   }
180 
181   /*
182    * Test method for 'jgroup.relacs.daemon.Estimate.remove(EndPoint[])'
183    */
184   public void testRemoveEndPointArray()
185   {
186 
187   }
188 
189   /*
190    * Test method for 'jgroup.relacs.daemon.Estimate.isSynchronized()'
191    */
192   public void testIsSynchronized()
193   {
194 
195   }
196 
197   /*
198    * Test method for 'jgroup.relacs.daemon.Estimate.hasChanged()'
199    */
200   public void testHasChanged()
201   {
202 
203   }
204 
205   /*
206    * Test method for 'jgroup.relacs.daemon.Estimate.getEndPoints()'
207    */
208   public void testGetEndPoints()
209   {
210     EndPoint[] P = new EndPointImpl[3];
211     P[0] = endpoints[0];
212     P[1] = endpoints[1];
213     P[2] = endpoints[2];
214     Estimate estimate = new Estimate(sbuffer, thosts, rset);
215     EndPoint[] x = estimate.getEndPoints();
216     for (int i = 0; i < x.length; i++) {
217       System.out.println(x[i]);
218       assertTrue(Util.in(rset, x[i]));
219     }
220     // One host less
221     P = new EndPointImpl[2];
222     P[0] = endpoints[0];
223     P[1] = endpoints[1];
224     estimate = new Estimate(sbuffer, thosts, rset);
225     estimate.intersect(P);
226     x = estimate.getEndPoints();
227     System.out.println("---");
228     for (int i = 0; i < x.length; i++) {
229       System.out.println(x[i]);
230       assertTrue(Util.in(P, x[i]));
231     }
232   }
233 
234 }