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