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