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.assertTrue;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotEquals;
24  
25  import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
26  import org.apache.hadoop.hbase.testclassification.MetricsTests;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   *  Test for MetricsTableSourceImpl
33   */
34  @Category({MetricsTests.class, SmallTests.class})
35  public class TestMetricsTableSourceImpl {
36  
37    @Test
38    public void testCompareToHashCode() throws Exception {
39      MetricsRegionServerSourceFactory metricsFact =
40          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class);
41  
42      MetricsTableSource one = metricsFact.createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
43      MetricsTableSource oneClone = metricsFact.createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
44      MetricsTableSource two = metricsFact.createTable("TWOTABLE", new TableWrapperStub("TWOTABLE"));
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    @Test(expected = RuntimeException.class)
57    public void testNoGetTableMetricsSourceImpl() throws Exception {
58      // This should throw an exception because MetricsTableSourceImpl should only
59      // be created by a factory.
60      CompatibilitySingletonFactory.getInstance(MetricsTableSourceImpl.class);
61    }
62  
63    @Test
64    public void testGetTableMetrics() throws Exception{
65      MetricsTableSource oneTbl =
66          CompatibilitySingletonFactory.getInstance(MetricsRegionServerSourceFactory.class)
67          .createTable("ONETABLE", new TableWrapperStub("ONETABLE"));
68      assertEquals("ONETABLE", oneTbl.getTableName());
69    }
70  
71    static class TableWrapperStub implements MetricsTableWrapperAggregate {
72  
73      private String tableName;
74  
75      public TableWrapperStub(String tableName) {
76        this.tableName = tableName;
77      }
78  
79      @Override
80      public long getReadRequestsCount(String table) {
81        return 10;
82      }
83  
84      @Override
85      public long getWriteRequestsCount(String table) {
86        return 20;
87      }
88  
89      @Override
90      public long getTotalRequestsCount(String table) {
91        return 30;
92      }
93  
94      @Override
95      public long getMemstoresSize(String table) {
96        return 1000;
97      }
98  
99      @Override
100     public long getStoreFilesSize(String table) {
101       return 2000;
102     }
103 
104     @Override
105     public long getTableSize(String table) {
106       return 3000;
107     }
108 
109     public String getTableName() {
110       return tableName;
111     }
112   }
113 }