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.*;
21  
22  import java.util.concurrent.atomic.AtomicLong;
23  
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.TableName;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestDelayingRunner {
33  
34    private static final TableName DUMMY_TABLE =
35        TableName.valueOf("DUMMY_TABLE");
36    private static final byte[] DUMMY_BYTES_1 = "DUMMY_BYTES_1".getBytes();
37    private static final byte[] DUMMY_BYTES_2 = "DUMMY_BYTES_2".getBytes();
38    private static HRegionInfo hri1 =
39        new HRegionInfo(DUMMY_TABLE, DUMMY_BYTES_1, DUMMY_BYTES_2, false, 1);
40  
41    @SuppressWarnings({ "rawtypes", "unchecked" })
42    @Test
43    public void testDelayingRunner() throws Exception{
44      MultiAction<Row> ma = new MultiAction<Row>();
45      ma.add(hri1.getRegionName(), new Action<Row>(new Put(DUMMY_BYTES_1), 0));
46      final AtomicLong endTime = new AtomicLong();
47      final long sleepTime = 1000;
48      DelayingRunner runner = new DelayingRunner(sleepTime, ma.actions.entrySet().iterator().next());
49      runner.setRunner(new Runnable() {
50        @Override
51        public void run() {
52          endTime.set(EnvironmentEdgeManager.currentTime());
53        }      
54      });
55      long startTime = EnvironmentEdgeManager.currentTime();
56      runner.run();
57      long delay = endTime.get() - startTime;
58      assertTrue("DelayingRunner did not delay long enough", delay >= sleepTime);
59      assertFalse("DelayingRunner delayed too long", delay > sleepTime + sleepTime*0.2);
60    }
61  
62  }