View Javadoc

1   /**
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
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
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.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.Map;
26  
27  import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
28  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
29  import org.apache.hadoop.hbase.testclassification.SmallTests;
30  import org.apache.hadoop.hbase.testclassification.MetricsTests;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category({MetricsTests.class, SmallTests.class})
35  public class TestMetricsRegionSourceImpl {
36  
37    @SuppressWarnings("SelfComparison")
38    @Test
39    public void testCompareToHashCodeEquals() throws Exception {
40      MetricsRegionServerSourceFactory fact = CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class);
41  
42      MetricsRegionSource one = fact.createRegion(new RegionWrapperStub("TEST"));
43      MetricsRegionSource oneClone = fact.createRegion(new RegionWrapperStub("TEST"));
44      MetricsRegionSource two = fact.createRegion(new RegionWrapperStub("TWO"));
45  
46      assertEquals(0, one.compareTo(oneClone));
47      assertEquals(one.hashCode(), oneClone.hashCode());
48      assertNotEquals(one, two);
49  
50      assertTrue( one.compareTo(two) != 0);
51      assertTrue( two.compareTo(one) != 0);
52      assertTrue( two.compareTo(one) != one.compareTo(two));
53      assertTrue( two.compareTo(two) == 0);
54    }
55  
56  
57    @Test(expected = RuntimeException.class)
58    public void testNoGetRegionServerMetricsSourceImpl() throws Exception {
59      // This should throw an exception because MetricsRegionSourceImpl should only
60      // be created by a factory.
61      CompatibilitySingletonFactory.getInstance(MetricsRegionSource.class);
62    }
63  
64    static class RegionWrapperStub implements MetricsRegionWrapper {
65  
66      private String regionName;
67  
68      public RegionWrapperStub(String regionName) {
69        this.regionName = regionName;
70      }
71  
72      @Override
73      public String getTableName() {
74        return null;
75      }
76  
77      @Override
78      public String getNamespace() {
79        return null;
80      }
81  
82      @Override
83      public String getRegionName() {
84        return this.regionName;
85      }
86  
87      @Override
88      public long getNumStores() {
89        return 0;
90      }
91  
92      @Override
93      public long getNumStoreFiles() {
94        return 0;
95      }
96  
97      @Override
98      public long getMemstoreSize() {
99        return 0;
100     }
101 
102     @Override
103     public long getStoreFileSize() {
104       return 0;
105     }
106 
107     @Override
108     public long getReadRequestCount() {
109       return 0;
110     }
111 
112     @Override
113     public long getWriteRequestCount() {
114       return 0;
115     }
116 
117     @Override
118     public long getNumFilesCompacted() {
119       return 0;
120     }
121 
122     @Override
123     public long getNumBytesCompacted() {
124       return 0;
125     }
126 
127     @Override
128     public long getNumCompactionsCompleted() {
129       return 0;
130     }
131 
132     @Override
133     public int getRegionHashCode() {
134       return regionName.hashCode();
135     }
136 
137     /**
138      * Always return 0 for testing
139      */
140     @Override
141     public int getReplicaId() {
142       return 0;
143     }
144   }
145 }