1   /**
2    * Copyright 2009 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  
24  import junit.framework.Assert;
25  
26  import org.apache.hadoop.hbase.HBaseConfiguration;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.MasterNotRunningException;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  
36  /**
37   * Tests HTablePool.
38   */
39  public class TestHTablePool  {
40  
41    private static HBaseTestingUtility TEST_UTIL   =  new HBaseTestingUtility();
42  
43    @BeforeClass
44    public static void beforeClass() throws Exception {
45      TEST_UTIL.startMiniCluster(1);
46  
47    }
48  
49    @AfterClass
50    public static void afterClass() throws IOException {
51      TEST_UTIL.shutdownMiniCluster();
52    }
53  
54    @Test
55    public void testTableWithStringName() {
56      HTablePool pool = new HTablePool((HBaseConfiguration)null, Integer.MAX_VALUE);
57      String tableName = "testTable";
58  
59      // Request a table from an empty pool
60      HTableInterface table = pool.getTable(tableName);
61      Assert.assertNotNull(table);
62  
63      // Return the table to the pool
64      pool.putTable(table);
65  
66      // Request a table of the same name
67      HTableInterface sameTable = pool.getTable(tableName);
68      Assert.assertSame(table, sameTable);
69    }
70  
71    @Test
72    public void testTableWithByteArrayName() {
73      HTablePool pool = new HTablePool((HBaseConfiguration)null, Integer.MAX_VALUE);
74      byte[] tableName = Bytes.toBytes("testTable");
75  
76      // Request a table from an empty pool
77      HTableInterface table = pool.getTable(tableName);
78      Assert.assertNotNull(table);
79  
80      // Return the table to the pool
81      pool.putTable(table);
82  
83      // Request a table of the same name
84      HTableInterface sameTable = pool.getTable(tableName);
85      Assert.assertSame(table, sameTable);
86    }
87  
88    @Test
89    public void testTableWithMaxSize() {
90      HTablePool pool = new HTablePool((HBaseConfiguration)null, 2);
91      String tableName = "testTable";
92  
93      // Request tables from an empty pool
94      HTableInterface table1 = pool.getTable(tableName);
95      HTableInterface table2 = pool.getTable(tableName);
96      HTableInterface table3 = pool.getTable(tableName);
97  
98      // Return the tables to the pool
99      pool.putTable(table1);
100     pool.putTable(table2);
101     // The pool should reject this one since it is already full
102     pool.putTable(table3);
103 
104     // Request tables of the same name
105     HTableInterface sameTable1 = pool.getTable(tableName);
106     HTableInterface sameTable2 = pool.getTable(tableName);
107     HTableInterface sameTable3 = pool.getTable(tableName);
108     Assert.assertSame(table1, sameTable1);
109     Assert.assertSame(table2, sameTable2);
110     Assert.assertNotSame(table3, sameTable3);
111   }
112 
113   @Test
114   public void testTablesWithDifferentNames() {
115     HTablePool pool = new HTablePool((HBaseConfiguration)null, Integer.MAX_VALUE);
116     String tableName1 = "testTable1";
117     String tableName2 = "testTable2";
118 
119     // Request a table from an empty pool
120     HTableInterface table1 = pool.getTable(tableName1);
121     HTableInterface table2 = pool.getTable(tableName2);
122     Assert.assertNotNull(table2);
123 
124     // Return the tables to the pool
125     pool.putTable(table1);
126     pool.putTable(table2);
127 
128     // Request tables of the same names
129     HTableInterface sameTable1 = pool.getTable(tableName1);
130     HTableInterface sameTable2 = pool.getTable(tableName2);
131     Assert.assertSame(table1, sameTable1);
132     Assert.assertSame(table2, sameTable2);
133   }
134 
135 
136   @Test
137   public void testCloseTablePool() throws IOException {
138 
139     HTablePool pool = new HTablePool(TEST_UTIL.getConfiguration(), 4);
140     String tableName = "testTable";
141     HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
142 
143     if (admin.tableExists(tableName)) {
144       admin.deleteTable(tableName);
145     }
146 
147     HTableDescriptor tableDescriptor = new HTableDescriptor(Bytes
148         .toBytes(tableName));
149     tableDescriptor.addFamily(new HColumnDescriptor("randomFamily"));
150     admin.createTable(tableDescriptor);
151 
152 
153     // Request tables from an empty pool
154     HTableInterface[] tables = new HTableInterface[4];
155     for (int i = 0; i < 4; ++i ) {
156       tables[i] = pool.getTable(tableName);
157     }
158 
159     pool.closeTablePool(tableName);
160 
161     for (int i = 0; i < 4; ++i ) {
162       pool.putTable(tables[i]);
163     }
164 
165     Assert.assertEquals(4, pool.getCurrentPoolSize(tableName));
166 
167     pool.closeTablePool(tableName);
168 
169     Assert.assertEquals(0, pool.getCurrentPoolSize(tableName));
170 
171   }
172 }