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.regionserver.compactions;
019
020import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.SIZE_LIMIT_KEY;
021import static org.apache.hadoop.hbase.regionserver.compactions.CloseChecker.TIME_LIMIT_KEY;
022import static org.junit.Assert.assertFalse;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.when;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.regionserver.Store;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category(SmallTests.class)
037public class TestCloseChecker {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestCloseChecker.class);
042
043  @Test
044  public void testIsClosed() {
045    Store enableWrite = mock(Store.class);
046    when(enableWrite.areWritesEnabled()).thenReturn(true);
047
048    Store disableWrite = mock(Store.class);
049    when(disableWrite.areWritesEnabled()).thenReturn(false);
050
051    Configuration conf = new Configuration();
052
053    long currentTime = EnvironmentEdgeManager.currentTime();
054
055    conf.setInt(SIZE_LIMIT_KEY, 10);
056    conf.setLong(TIME_LIMIT_KEY, 10);
057
058    CloseChecker closeChecker = new CloseChecker(conf, currentTime);
059    assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime));
060    assertFalse(closeChecker.isSizeLimit(enableWrite, 10L));
061
062    closeChecker = new CloseChecker(conf, currentTime);
063    assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + 11));
064    assertFalse(closeChecker.isSizeLimit(enableWrite, 11L));
065
066    closeChecker = new CloseChecker(conf, currentTime);
067    assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 11));
068    assertTrue(closeChecker.isSizeLimit(disableWrite, 11L));
069
070    for (int i = 0; i < 10; i++) {
071      int plusTime = 5 * i;
072      assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + plusTime));
073      assertFalse(closeChecker.isSizeLimit(enableWrite, 5L));
074    }
075
076    closeChecker = new CloseChecker(conf, currentTime);
077    assertFalse(closeChecker.isTimeLimit(disableWrite, currentTime + 6));
078    assertFalse(closeChecker.isSizeLimit(disableWrite, 6));
079    assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 12));
080    assertTrue(closeChecker.isSizeLimit(disableWrite, 6));
081  }
082}