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