1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
60 HTableInterface table = pool.getTable(tableName);
61 Assert.assertNotNull(table);
62
63
64 pool.putTable(table);
65
66
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
77 HTableInterface table = pool.getTable(tableName);
78 Assert.assertNotNull(table);
79
80
81 pool.putTable(table);
82
83
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
94 HTableInterface table1 = pool.getTable(tableName);
95 HTableInterface table2 = pool.getTable(tableName);
96 HTableInterface table3 = pool.getTable(tableName);
97
98
99 pool.putTable(table1);
100 pool.putTable(table2);
101
102 pool.putTable(table3);
103
104
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
120 HTableInterface table1 = pool.getTable(tableName1);
121 HTableInterface table2 = pool.getTable(tableName2);
122 Assert.assertNotNull(table2);
123
124
125 pool.putTable(table1);
126 pool.putTable(table2);
127
128
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
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 }