View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.hadoop.hbase.quotas;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.when;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.client.Connection;
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  
35  import com.google.common.collect.Iterables;
36  
37  /**
38   * Non-HBase cluster unit tests for {@link QuotaObserverChore}.
39   */
40  @Category(SmallTests.class)
41  public class TestQuotaObserverChore {
42    private Connection conn;
43    private QuotaObserverChore chore;
44  
45    @Before
46    public void setup() throws Exception {
47      conn = mock(Connection.class);
48      chore = mock(QuotaObserverChore.class);
49    }
50  
51    @Test
52    public void testNumRegionsForTable() {
53      TableName tn1 = TableName.valueOf("t1");
54      TableName tn2 = TableName.valueOf("t2");
55      TableName tn3 = TableName.valueOf("t3");
56  
57      final int numTable1Regions = 10;
58      final int numTable2Regions = 15;
59      final int numTable3Regions = 8;
60      Map<HRegionInfo,Long> regionReports = new HashMap<>();
61      for (int i = 0; i < numTable1Regions; i++) {
62        regionReports.put(new HRegionInfo(tn1, Bytes.toBytes(i), Bytes.toBytes(i + 1)), 0L);
63      }
64  
65      for (int i = 0; i < numTable2Regions; i++) {
66        regionReports.put(new HRegionInfo(tn2, Bytes.toBytes(i), Bytes.toBytes(i + 1)), 0L);
67      }
68  
69      for (int i = 0; i < numTable3Regions; i++) {
70        regionReports.put(new HRegionInfo(tn3, Bytes.toBytes(i), Bytes.toBytes(i + 1)), 0L);
71      }
72  
73      TableQuotaSnapshotStore store = new TableQuotaSnapshotStore(conn, chore, regionReports);
74      when(chore.getTableSnapshotStore()).thenReturn(store);
75  
76      assertEquals(numTable1Regions, Iterables.size(store.filterBySubject(tn1)));
77      assertEquals(numTable2Regions, Iterables.size(store.filterBySubject(tn2)));
78      assertEquals(numTable3Regions, Iterables.size(store.filterBySubject(tn3)));
79    }
80  }