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.MutableCounterLong;
22  import org.apache.hadoop.metrics2.lib.MutableGaugeLong;
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  
29  /**
30   *  Test of default BaseSource for hadoop 2
31   */
32  public class TestBaseSourceImpl {
33  
34    private static BaseSourceImpl bmsi;
35  
36    @BeforeClass
37    public static void setUp() throws Exception {
38      bmsi = new BaseSourceImpl("TestName", "test description", "testcontext", "TestContext");
39    }
40  
41    @Test
42    public void testSetGauge() throws Exception {
43      bmsi.setGauge("testset", 100);
44      assertEquals(100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testset")).value());
45      bmsi.setGauge("testset", 300);
46      assertEquals(300, ((MutableGaugeLong) bmsi.metricsRegistry.get("testset")).value());
47  
48    }
49  
50    @Test
51    public void testIncGauge() throws Exception {
52      bmsi.incGauge("testincgauge", 100);
53      assertEquals(100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testincgauge")).value());
54      bmsi.incGauge("testincgauge", 100);
55      assertEquals(200, ((MutableGaugeLong) bmsi.metricsRegistry.get("testincgauge")).value());
56  
57    }
58  
59    @Test
60    public void testDecGauge() throws Exception {
61      bmsi.decGauge("testdec", 100);
62      assertEquals(-100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testdec")).value());
63      bmsi.decGauge("testdec", 100);
64      assertEquals(-200, ((MutableGaugeLong) bmsi.metricsRegistry.get("testdec")).value());
65  
66    }
67  
68    @Test
69    public void testIncCounters() throws Exception {
70      bmsi.incCounters("testinccounter", 100);
71      assertEquals(100, ((MutableCounterLong) bmsi.metricsRegistry.get("testinccounter")).value());
72      bmsi.incCounters("testinccounter", 100);
73      assertEquals(200, ((MutableCounterLong) bmsi.metricsRegistry.get("testinccounter")).value());
74  
75    }
76  
77    @Test
78    public void testRemoveMetric() throws Exception {
79      bmsi.setGauge("testrmgauge", 100);
80      bmsi.removeMetric("testrmgauge");
81      assertNull(bmsi.metricsRegistry.get("testrmgauge"));
82    }
83  
84  }