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.jupiter.api.Assertions.assertFalse; 023import static org.junit.jupiter.api.Assertions.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.regionserver.Store; 029import org.apache.hadoop.hbase.testclassification.SmallTests; 030import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 031import org.junit.jupiter.api.Tag; 032import org.junit.jupiter.api.Test; 033 034@Tag(SmallTests.TAG) 035public class TestCloseChecker { 036 037 @Test 038 public void testIsClosed() { 039 Store enableWrite = mock(Store.class); 040 when(enableWrite.areWritesEnabled()).thenReturn(true); 041 042 Store disableWrite = mock(Store.class); 043 when(disableWrite.areWritesEnabled()).thenReturn(false); 044 045 Configuration conf = new Configuration(); 046 047 long currentTime = EnvironmentEdgeManager.currentTime(); 048 049 conf.setInt(SIZE_LIMIT_KEY, 10); 050 conf.setLong(TIME_LIMIT_KEY, 10); 051 052 CloseChecker closeChecker = new CloseChecker(conf, currentTime); 053 assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime)); 054 assertFalse(closeChecker.isSizeLimit(enableWrite, 10L)); 055 056 closeChecker = new CloseChecker(conf, currentTime); 057 assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + 11)); 058 assertFalse(closeChecker.isSizeLimit(enableWrite, 11L)); 059 060 closeChecker = new CloseChecker(conf, currentTime); 061 assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 11)); 062 assertTrue(closeChecker.isSizeLimit(disableWrite, 11L)); 063 064 for (int i = 0; i < 10; i++) { 065 int plusTime = 5 * i; 066 assertFalse(closeChecker.isTimeLimit(enableWrite, currentTime + plusTime)); 067 assertFalse(closeChecker.isSizeLimit(enableWrite, 5L)); 068 } 069 070 closeChecker = new CloseChecker(conf, currentTime); 071 assertFalse(closeChecker.isTimeLimit(disableWrite, currentTime + 6)); 072 assertFalse(closeChecker.isSizeLimit(disableWrite, 6)); 073 assertTrue(closeChecker.isTimeLimit(disableWrite, currentTime + 12)); 074 assertTrue(closeChecker.isSizeLimit(disableWrite, 6)); 075 } 076}