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.client;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.concurrent.atomic.AtomicLong;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HRegionInfo;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category(SmallTests.class)
035public class TestDelayingRunner {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestDelayingRunner.class);
040
041  private static final TableName DUMMY_TABLE =
042      TableName.valueOf("DUMMY_TABLE");
043  private static final byte[] DUMMY_BYTES_1 = Bytes.toBytes("DUMMY_BYTES_1");
044  private static final byte[] DUMMY_BYTES_2 = Bytes.toBytes("DUMMY_BYTES_2");
045  private static HRegionInfo hri1 =
046      new HRegionInfo(DUMMY_TABLE, DUMMY_BYTES_1, DUMMY_BYTES_2, false, 1);
047
048  @SuppressWarnings({ "rawtypes", "unchecked" })
049  @Test
050  public void testDelayingRunner() throws Exception{
051    MultiAction ma = new MultiAction();
052    ma.add(hri1.getRegionName(), new Action(new Put(DUMMY_BYTES_1), 0));
053    final AtomicLong endTime = new AtomicLong();
054    final long sleepTime = 1000;
055    DelayingRunner runner = new DelayingRunner(sleepTime, ma.actions.entrySet().iterator().next());
056    runner.setRunner(new Runnable() {
057      @Override
058      public void run() {
059        endTime.set(EnvironmentEdgeManager.currentTime());
060      }
061    });
062    long startTime = EnvironmentEdgeManager.currentTime();
063    runner.run();
064    long delay = endTime.get() - startTime;
065    assertTrue("DelayingRunner did not delay long enough", delay >= sleepTime);
066    assertFalse("DelayingRunner delayed too long", delay > sleepTime + sleepTime*0.2);
067  }
068
069}