1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }