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 org.apache.hadoop.conf.Configuration;
21 import org.apache.hadoop.hbase.HRegionInfo;
22 import org.apache.hadoop.hbase.HRegionLocation;
23 import org.apache.hadoop.hbase.RegionLocations;
24 import org.apache.hadoop.hbase.ServerName;
25 import org.apache.hadoop.hbase.TableName;
26 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
27 import org.apache.hadoop.hbase.testclassification.ClientTests;
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.runners.MockitoJUnitRunner;
37
38 @RunWith(MockitoJUnitRunner.class)
39 @Category({ ClientTests.class, SmallTests.class })
40 public class TestReversedScannerCallable {
41
42 @Mock
43 private ClusterConnection connection;
44 @Mock
45 private Scan scan;
46 @Mock
47 private RpcControllerFactory rpcFactory;
48 @Mock
49 private RegionLocations regionLocations;
50
51 private final byte[] ROW = Bytes.toBytes("row1");
52
53 @Before
54 public void setUp() throws Exception {
55 byte[] ROW_BEFORE = ConnectionUtils.createCloseRowBefore(ROW);
56
57 Configuration conf = Mockito.mock(Configuration.class);
58 HRegionLocation regionLocation = Mockito.mock(HRegionLocation.class);
59 ServerName serverName = Mockito.mock(ServerName.class);
60 HRegionInfo regionInfo = Mockito.mock(HRegionInfo.class);
61
62 Mockito.when(connection.getConfiguration()).thenReturn(conf);
63 Mockito.when(regionLocations.size()).thenReturn(1);
64 Mockito.when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation);
65 Mockito.when(regionLocation.getHostname()).thenReturn("localhost");
66 Mockito.when(regionLocation.getRegionInfo()).thenReturn(regionInfo);
67 Mockito.when(regionLocation.getServerName()).thenReturn(serverName);
68 Mockito.when(regionInfo.containsRow(ROW_BEFORE)).thenReturn(true);
69 Mockito.when(scan.includeStartRow()).thenReturn(true);
70 Mockito.when(scan.getStartRow()).thenReturn(ROW);
71 }
72
73 @Test
74 public void testPrepareDoesNotUseCache() throws Exception {
75 TableName tableName = TableName.valueOf("MyTable");
76 Mockito.when(connection.relocateRegion(tableName, ROW, 0)).thenReturn(regionLocations);
77
78 ReversedScannerCallable callable =
79 new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
80 callable.prepare(true);
81
82 Mockito.verify(connection).relocateRegion(tableName, ROW, 0);
83 }
84
85 @Test
86 public void testPrepareUsesCache() throws Exception {
87 TableName tableName = TableName.valueOf("MyTable");
88 Mockito.when(connection.locateRegion(tableName, ROW, true, true, 0))
89 .thenReturn(regionLocations);
90
91 ReversedScannerCallable callable =
92 new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
93 callable.prepare(false);
94
95 Mockito.verify(connection).locateRegion(tableName, ROW, true, true, 0);
96 }
97 }