001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.metrics; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNull; 022 023import org.apache.hadoop.hbase.testclassification.MetricsTests; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.apache.hadoop.metrics2.lib.MutableFastCounter; 026import org.apache.hadoop.metrics2.lib.MutableGaugeLong; 027import org.junit.jupiter.api.BeforeAll; 028import org.junit.jupiter.api.Tag; 029import org.junit.jupiter.api.Test; 030 031/** 032 * Test of default BaseSource for hadoop 2 033 */ 034@Tag(MetricsTests.TAG) 035@Tag(SmallTests.TAG) 036public class TestBaseSourceImpl { 037 038 private static BaseSourceImpl bmsi; 039 040 @BeforeAll 041 public static void setUp() throws Exception { 042 bmsi = new BaseSourceImpl("TestName", "test description", "testcontext", "TestContext"); 043 } 044 045 @Test 046 public void testSetGauge() throws Exception { 047 bmsi.setGauge("testset", 100); 048 assertEquals(100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testset")).value()); 049 bmsi.setGauge("testset", 300); 050 assertEquals(300, ((MutableGaugeLong) bmsi.metricsRegistry.get("testset")).value()); 051 052 } 053 054 @Test 055 public void testIncGauge() throws Exception { 056 bmsi.incGauge("testincgauge", 100); 057 assertEquals(100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testincgauge")).value()); 058 bmsi.incGauge("testincgauge", 100); 059 assertEquals(200, ((MutableGaugeLong) bmsi.metricsRegistry.get("testincgauge")).value()); 060 061 } 062 063 @Test 064 public void testDecGauge() throws Exception { 065 bmsi.decGauge("testdec", 100); 066 assertEquals(-100, ((MutableGaugeLong) bmsi.metricsRegistry.get("testdec")).value()); 067 bmsi.decGauge("testdec", 100); 068 assertEquals(-200, ((MutableGaugeLong) bmsi.metricsRegistry.get("testdec")).value()); 069 070 } 071 072 @Test 073 public void testIncCounters() throws Exception { 074 bmsi.incCounters("testinccounter", 100); 075 assertEquals(100, ((MutableFastCounter) bmsi.metricsRegistry.get("testinccounter")).value()); 076 bmsi.incCounters("testinccounter", 100); 077 assertEquals(200, ((MutableFastCounter) bmsi.metricsRegistry.get("testinccounter")).value()); 078 079 } 080 081 @Test 082 public void testRemoveMetric() throws Exception { 083 bmsi.setGauge("testrmgauge", 100); 084 bmsi.removeMetric("testrmgauge"); 085 assertNull(bmsi.metricsRegistry.get("testrmgauge")); 086 } 087}