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.*;
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 }