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.regionreplication;
019
020import static org.mockito.Mockito.mock;
021import static org.mockito.Mockito.times;
022import static org.mockito.Mockito.verify;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseConfiguration;
026import org.apache.hadoop.hbase.testclassification.MediumTests;
027import org.apache.hadoop.hbase.testclassification.RegionServerTests;
028import org.junit.jupiter.api.BeforeEach;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(RegionServerTests.TAG)
033@Tag(MediumTests.TAG)
034public class TestRegionReplicationFlushRequester {
035
036  private Configuration conf;
037
038  private Runnable requester;
039
040  private RegionReplicationFlushRequester flushRequester;
041
042  @BeforeEach
043  public void setUp() {
044    conf = HBaseConfiguration.create();
045    conf.setInt(RegionReplicationFlushRequester.MIN_INTERVAL_SECS, 1);
046    requester = mock(Runnable.class);
047    flushRequester = new RegionReplicationFlushRequester(conf, requester);
048  }
049
050  @Test
051  public void testRequest() throws InterruptedException {
052    // should call request directly
053    flushRequester.requestFlush(100L);
054    verify(requester, times(1)).run();
055
056    // should not call request directly, since the min interval is 1 second
057    flushRequester.requestFlush(200L);
058    verify(requester, times(1)).run();
059    Thread.sleep(2000);
060    verify(requester, times(2)).run();
061
062    // should call request directly because we have already elapsed more than 1 second
063    Thread.sleep(2000);
064    flushRequester.requestFlush(300L);
065    verify(requester, times(3)).run();
066  }
067
068  @Test
069  public void testCancelFlushRequest() throws InterruptedException {
070    flushRequester.requestFlush(100L);
071    flushRequester.requestFlush(200L);
072    verify(requester, times(1)).run();
073
074    // the pending flush request should be canceled
075    flushRequester.recordFlush(300L);
076    Thread.sleep(2000);
077    verify(requester, times(1)).run();
078  }
079}