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.junit.Test;
22  import org.junit.experimental.categories.Category;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import static junit.framework.Assert.assertEquals;
28  
29  @Category(SmallTests.class)
30  public class TestHDFSBlocksDistribution {
31    @Test
32    public void testAddHostsAndBlockWeight() throws Exception {
33      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
34      distribution.addHostsAndBlockWeight(null, 100);
35      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
36      distribution.addHostsAndBlockWeight(new String[0], 100);
37      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
38      distribution.addHostsAndBlockWeight(new String[] {"test"}, 101);
39      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
40      distribution.addHostsAndBlockWeight(new String[] {"test"}, 202);
41      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
42      assertEquals("test host should have weight 303", 303,
43          distribution.getHostAndWeights().get("test").getWeight());
44      distribution.addHostsAndBlockWeight(new String[] {"testTwo"}, 222);
45      assertEquals("Should be two hosts", 2, distribution.getHostAndWeights().size());
46      assertEquals("Total weight should be 525", 525, distribution.getUniqueBlocksTotalWeight());
47    }
48  
49    public class MockHDFSBlocksDistribution extends HDFSBlocksDistribution {
50      public Map<String,HostAndWeight> getHostAndWeights() {
51        HashMap<String, HostAndWeight> map = new HashMap<String, HostAndWeight>();
52        map.put("test", new HostAndWeight(null, 100));
53        return map;
54      }
55  
56    }
57  
58    @Test
59    public void testAdd() throws Exception {
60      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
61      distribution.add(new MockHDFSBlocksDistribution());
62      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
63      distribution.addHostsAndBlockWeight(new String[]{"test"}, 10);
64      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
65      distribution.add(new MockHDFSBlocksDistribution());
66      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
67      assertEquals("Total weight should be 10", 10, distribution.getUniqueBlocksTotalWeight());
68    }
69  }