1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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   * This class is a JUnit-test for ReplicaCount.
40   * 
41   * @author Henning Hommeland
42   */
43  public class TestReplicaCount extends TestCase
44  {
45  
46    /* Fields */
47    private AppConfig[] apps;
48  
49    /* Test-methods */
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      /* Make sure that the ReplicaCount object was created */
58      assertTrue(!rc.equals(null));
59  
60      /* Assign replicas */
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      /* Remove replicas */
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      /* Check that the set of hosts is the same */
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      /* Make sure that the ReplicaCount object was created */
88      assertTrue(!rc.equals(null));
89  
90      /* Assign replicas */
91      HostSet hs1 = rc.assignReplicas(apps[1]);
92  
93      /* Test using removeReplicas() and reassignReplicas() */
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     /* HostSets should now be equal */;
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     /* Make sure that the ReplicaCount object was created */
113     assertTrue(!rc.equals(null));
114 
115     /* Assign and collocate apps */
116     HostSet hs1 = rc.assignReplicas(apps[1]);
117     HostSet hs2 = rc.collocateReplicas(apps[2], apps[1]);
118 
119     /* HostSets should be equal */
120     assertTrue(hs1.equals(hs2));
121     HostSet hs3 = rc.removeReplicas(apps[1]);
122     assertTrue(hs1.equals(hs3));
123 
124     /* Also - removeReplicas() should remove apps[2] as well */
125     // GroupTable groupTable = (GroupTable) rc.getState();
126     // assertTrue(!groupTable.contains(apps[2]));
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     /* Make sure that the ReplicaCount object was created */
137     assertTrue(!rc.equals(null));
138 
139     /* State before setting new view */
140     // System.out.println("Getting state before running new view:");
141     // System.out.println("***************************************");
142     // System.out.println(rc.getState());
143     // System.out.println("***************************************");
144     /* Generate a new view */
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     /* Bootstrapping no longer supported!? */
151     // GroupTable groupTable = (GroupTable) rc.getState();
152     // groupTable.add(apps[1], hosts);
153     /* Ugly hack!!!! */
154     // ReplicaManagerImpl rm = new ReplicaManagerImpl(true);
155     /* Send out new view */
156     rc.viewChangeEvent(newView);
157     // System.out.println("Getting state after running new view:");
158     // System.out.println("**************************************");
159     // System.out.println(rc.getState());
160     // System.out.println("**************************************");
161 
162     /*
163      * Make sure that ReplicaCount is in correct state. (In lack of equals()
164      * implementation in GroupTable).
165      */
166     // groupTable = (GroupTable) rc.getState();
167     // assertTrue(groupTable.contains(apps[1]));
168     // assertTrue(groupTable.size() == 1);
169     // MemberId[] memTab2 = new MemberId[2];
170     // memTab2 = groupTable.getMembers(apps[1].getGroupId());
171     // assertTrue(memTab[0].equals(memTab2[1]));
172     // assertTrue(memTab[1].equals(memTab2[0]));
173   }
174 
175   /**
176    * Generate a MemberId from parameter host. FIXME: Will generate equal
177    * MemberId for same host.
178    * 
179    * @param host
180    *          Hostname for which to generate MemberId.
181    * @return MembedId for the given host.
182    * @throws Exception
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     // suite.addTest(new TestReplicaCount("testReassignReplica"));
198     // suite.addTest(new TestReplicaCount("testGetPutState"));
199     // suite.addTest(new TestReplicaCount("testCollocateReplicas"));
200     // suite.addTest(new TestReplicaCount("testViewChange"));
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    * @see TestCase#setUp()
211    */
212   protected void setUp() throws Exception
213   {
214     super.setUp();
215     // Enable JUnit tests for Jgroup
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    * @see TestCase#tearDown()
233    */
234   protected void tearDown() throws Exception
235   {
236     super.tearDown();
237   }
238 
239   /**
240    * Constructor for TestReplicaCount.
241    * 
242    * @param name
243    */
244   public TestReplicaCount(String name) throws Exception
245   {
246     super(name);
247   }
248 }