View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import org.apache.hadoop.hbase.testclassification.SmallTests;
22  import org.junit.Test;
23  import org.junit.experimental.categories.Category;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import static junit.framework.Assert.assertEquals;
29  
30  @Category(SmallTests.class)
31  public class TestHDFSBlocksDistribution {
32    @Test
33    public void testAddHostsAndBlockWeight() throws Exception {
34      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
35      distribution.addHostsAndBlockWeight(null, 100);
36      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
37      distribution.addHostsAndBlockWeight(new String[0], 100);
38      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
39      distribution.addHostsAndBlockWeight(new String[] {"test"}, 101);
40      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
41      distribution.addHostsAndBlockWeight(new String[] {"test"}, 202);
42      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
43      assertEquals("test host should have weight 303", 303,
44          distribution.getHostAndWeights().get("test").getWeight());
45      distribution.addHostsAndBlockWeight(new String[] {"testTwo"}, 222);
46      assertEquals("Should be two hosts", 2, distribution.getHostAndWeights().size());
47      assertEquals("Total weight should be 525", 525, distribution.getUniqueBlocksTotalWeight());
48    }
49  
50    public class MockHDFSBlocksDistribution extends HDFSBlocksDistribution {
51      public Map<String,HostAndWeight> getHostAndWeights() {
52        HashMap<String, HostAndWeight> map = new HashMap<String, HostAndWeight>();
53        map.put("test", new HostAndWeight(null, 100));
54        return map;
55      }
56  
57    }
58  
59    @Test
60    public void testAdd() throws Exception {
61      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
62      distribution.add(new MockHDFSBlocksDistribution());
63      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
64      distribution.addHostsAndBlockWeight(new String[]{"test"}, 10);
65      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
66      distribution.add(new MockHDFSBlocksDistribution());
67      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
68      assertEquals("Total weight should be 10", 10, distribution.getUniqueBlocksTotalWeight());
69    }
70  }