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