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 = TableName.valueOf("DUMMY_TABLE");
042  private static final byte[] DUMMY_BYTES_1 = Bytes.toBytes("DUMMY_BYTES_1");
043  private static final byte[] DUMMY_BYTES_2 = Bytes.toBytes("DUMMY_BYTES_2");
044  private static HRegionInfo hri1 =
045    new HRegionInfo(DUMMY_TABLE, DUMMY_BYTES_1, DUMMY_BYTES_2, false, 1);
046
047  @SuppressWarnings({ "rawtypes", "unchecked" })
048  @Test
049  public void testDelayingRunner() throws Exception {
050    MultiAction ma = new MultiAction();
051    ma.add(hri1.getRegionName(), new Action(new Put(DUMMY_BYTES_1), 0));
052    final AtomicLong endTime = new AtomicLong();
053    final long sleepTime = 1000;
054    DelayingRunner runner = new DelayingRunner(sleepTime, ma.actions.entrySet().iterator().next());
055    runner.setRunner(new Runnable() {
056      @Override
057      public void run() {
058        endTime.set(EnvironmentEdgeManager.currentTime());
059      }
060    });
061    long startTime = EnvironmentEdgeManager.currentTime();
062    runner.run();
063    long delay = endTime.get() - startTime;
064    assertTrue("DelayingRunner did not delay long enough", delay >= sleepTime);
065    assertFalse("DelayingRunner delayed too long", delay > sleepTime + sleepTime * 0.2);
066  }
067
068}