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.client;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNull;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.client.backoff.ServerStatistics;
026import org.apache.hadoop.hbase.testclassification.ClientTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ClientTests.class, SmallTests.class})
033public class TestResultStatsUtil {
034  @ClassRule
035  public static final HBaseClassTestRule CLASS_RULE =
036          HBaseClassTestRule.forClass(TestResultStatsUtil.class);
037
038  private static final RegionLoadStats regionLoadStats = new RegionLoadStats(100,
039          10,90);
040  private static final byte[] regionName = {80};
041  private static final ServerName server = ServerName.parseServerName("3.1.yg.n,50,1");
042
043  @Test
044  public void testUpdateStats() {
045    // Create a tracker
046    ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
047
048    // Pass in the tracker for update
049    ResultStatsUtil.updateStats(serverStatisticTracker, server, regionName, regionLoadStats);
050
051    // Check that the tracker was updated as expected
052    ServerStatistics stats = serverStatisticTracker.getStats(server);
053
054    assertEquals(regionLoadStats.memstoreLoad, stats.getStatsForRegion(regionName)
055            .getMemStoreLoadPercent());
056    assertEquals(regionLoadStats.compactionPressure, stats.getStatsForRegion(regionName)
057            .getCompactionPressure());
058    assertEquals(regionLoadStats.heapOccupancy, stats.getStatsForRegion(regionName)
059            .getHeapOccupancyPercent());
060  }
061
062  @Test
063  public void testUpdateStatsRegionNameNull() {
064    ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
065
066    ResultStatsUtil.updateStats(serverStatisticTracker, server, null, regionLoadStats);
067
068    ServerStatistics stats = serverStatisticTracker.getStats(server);
069    assertNull(stats);
070  }
071
072  @Test
073  public void testUpdateStatsStatsNull() {
074    ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
075
076    ResultStatsUtil.updateStats(serverStatisticTracker, server, regionName, null);
077
078    ServerStatistics stats = serverStatisticTracker.getStats(server);
079    assertNull(stats);
080  }
081
082  @Test
083  public void testUpdateStatsTrackerNull() {
084    ServerStatisticTracker serverStatisticTracker = new ServerStatisticTracker();
085
086    ResultStatsUtil.updateStats(null, server, regionName, regionLoadStats);
087
088    ServerStatistics stats = serverStatisticTracker.getStats(server);
089    assertNull(stats);
090  }
091}