View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.concurrent.atomic.AtomicLong;
24  
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestDelayingRunner {
35  
36    private static final TableName DUMMY_TABLE =
37        TableName.valueOf("DUMMY_TABLE");
38    private static final byte[] DUMMY_BYTES_1 = Bytes.toBytes("DUMMY_BYTES_1");
39    private static final byte[] DUMMY_BYTES_2 = Bytes.toBytes("DUMMY_BYTES_2");
40    private static HRegionInfo hri1 =
41        new HRegionInfo(DUMMY_TABLE, DUMMY_BYTES_1, DUMMY_BYTES_2, false, 1);
42  
43    @SuppressWarnings({ "rawtypes", "unchecked" })
44    @Test
45    public void testDelayingRunner() throws Exception{
46      MultiAction<Row> ma = new MultiAction<Row>();
47      ma.add(hri1.getRegionName(), new Action<Row>(new Put(DUMMY_BYTES_1), 0));
48      final AtomicLong endTime = new AtomicLong();
49      final long sleepTime = 1000;
50      DelayingRunner runner = new DelayingRunner(sleepTime, ma.actions.entrySet().iterator().next());
51      runner.setRunner(new Runnable() {
52        @Override
53        public void run() {
54          endTime.set(EnvironmentEdgeManager.currentTime());
55        }      
56      });
57      long startTime = EnvironmentEdgeManager.currentTime();
58      runner.run();
59      long delay = endTime.get() - startTime;
60      assertTrue("DelayingRunner did not delay long enough", delay >= sleepTime);
61      assertFalse("DelayingRunner delayed too long", delay > sleepTime + sleepTime*0.2);
62    }
63  
64  }