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