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.metrics;
20  
21  import org.apache.hadoop.hbase.testclassification.SmallTests;
22  import org.apache.hadoop.hbase.testclassification.MetricsTests;
23  
24  import org.apache.hadoop.metrics2.lib.MetricMutableCounterLong;
25  import org.apache.hadoop.metrics2.lib.MetricMutableGaugeLong;
26  import org.junit.BeforeClass;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNull;
32  import static org.junit.Assert.assertSame;
33  
34  /**
35   * Test of the default BaseSource implementation for hadoop 1
36   */
37  @Category({MetricsTests.class, SmallTests.class})
38  public class TestBaseSourceImpl {
39  
40    private static BaseSourceImpl bmsi;
41  
42    @BeforeClass
43    public static void setUp() throws Exception {
44      bmsi = new BaseSourceImpl("TestName", "test description", "testcontext", "TestContext");
45    }
46  
47    @Test
48    public void testSetGauge() throws Exception {
49      String key = "testset";
50      bmsi.setGauge(key, 100);
51      MetricMutableGaugeLong g = (MetricMutableGaugeLong) bmsi.metricsRegistry.get(key);
52      assertEquals(key, g.name);
53      bmsi.setGauge(key, 110);
54      assertSame(g, bmsi.metricsRegistry.get(key));
55  
56    }
57  
58    @Test
59    public void testIncGauge() throws Exception {
60      String key = "testincgauge";
61      bmsi.incGauge(key, 100);
62      MetricMutableGaugeLong g = (MetricMutableGaugeLong) bmsi.metricsRegistry.get(key);
63      assertEquals(key, g.name);
64      bmsi.incGauge(key, 10);
65      assertSame(g, bmsi.metricsRegistry.get(key));
66    }
67  
68    @Test
69    public void testDecGauge() throws Exception {
70      String key = "testdec";
71      bmsi.decGauge(key, 100);
72      MetricMutableGaugeLong g = (MetricMutableGaugeLong) bmsi.metricsRegistry.get(key);
73      assertEquals(key, g.name);
74      bmsi.decGauge(key, 100);
75      assertSame(g, bmsi.metricsRegistry.get(key));
76    }
77  
78    @Test
79    public void testIncCounters() throws Exception {
80      String key = "testinccounter";
81      bmsi.incCounters(key, 100);
82      MetricMutableCounterLong c = (MetricMutableCounterLong) bmsi.metricsRegistry.get(key);
83      assertEquals(key, c.name);
84      bmsi.incCounters(key, 100);
85      assertSame(c, bmsi.metricsRegistry.get(key));
86    }
87  
88    @Test
89    public void testRemoveMetric() throws Exception {
90      bmsi.setGauge("testrm", 100);
91      bmsi.removeMetric("testrm");
92      assertNull(bmsi.metricsRegistry.get("testrm"));
93  
94    }
95  
96  }