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 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      HRegionLocation regionLocation = Mockito.mock(HRegionLocation.class);
57      ServerName serverName = Mockito.mock(ServerName.class);
58      HRegionInfo regionInfo = Mockito.mock(HRegionInfo.class);
59  
60      Mockito.when(connection.getConfiguration()).thenReturn(new Configuration());
61      Mockito.when(regionLocations.size()).thenReturn(1);
62      Mockito.when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation);
63      Mockito.when(regionLocation.getHostname()).thenReturn("localhost");
64      Mockito.when(regionLocation.getRegionInfo()).thenReturn(regionInfo);
65      Mockito.when(regionLocation.getServerName()).thenReturn(serverName);
66      Mockito.when(regionInfo.containsRow(ROW_BEFORE)).thenReturn(true);
67      Mockito.when(scan.includeStartRow()).thenReturn(true);
68      Mockito.when(scan.getStartRow()).thenReturn(ROW);
69    }
70  
71    @Test
72    public void testPrepareDoesNotUseCache() throws Exception {
73      TableName tableName = TableName.valueOf("MyTable");
74      Mockito.when(connection.relocateRegion(tableName, ROW, 0)).thenReturn(regionLocations);
75  
76      ReversedScannerCallable callable =
77          new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
78      callable.prepare(true);
79  
80      Mockito.verify(connection).relocateRegion(tableName, ROW, 0);
81    }
82  
83    @Test
84    public void testPrepareUsesCache() throws Exception {
85      TableName tableName = TableName.valueOf("MyTable");
86      Mockito.when(connection.locateRegion(tableName, ROW, true, true, 0))
87          .thenReturn(regionLocations);
88  
89      ReversedScannerCallable callable =
90          new ReversedScannerCallable(connection, tableName, scan, null, rpcFactory);
91      callable.prepare(false);
92  
93      Mockito.verify(connection).locateRegion(tableName, ROW, true, true, 0);
94    }
95  }