use of org.apache.phoenix.hbase.index.table.HTableFactory in project phoenix by apache.
the class TestCachingHTableFactory method testCacheCorrectlyExpiresTable.
@Test
public void testCacheCorrectlyExpiresTable() throws Exception {
// setup the mocks for the tables we will request
HTableFactory delegate = Mockito.mock(HTableFactory.class);
RegionCoprocessorEnvironment e = Mockito.mock(RegionCoprocessorEnvironment.class);
Configuration conf = new Configuration();
Mockito.when(e.getConfiguration()).thenReturn(conf);
Mockito.when(e.getSharedData()).thenReturn(new ConcurrentHashMap<String, Object>());
ImmutableBytesPtr t1 = new ImmutableBytesPtr(Bytes.toBytes("t1"));
ImmutableBytesPtr t2 = new ImmutableBytesPtr(Bytes.toBytes("t2"));
ImmutableBytesPtr t3 = new ImmutableBytesPtr(Bytes.toBytes("t3"));
HTableInterface table1 = Mockito.mock(HTableInterface.class);
HTableInterface table2 = Mockito.mock(HTableInterface.class);
HTableInterface table3 = Mockito.mock(HTableInterface.class);
// setup our factory with a cache size of 2
CachingHTableFactory factory = new CachingHTableFactory(delegate, 2, e);
Mockito.when(delegate.getTable(t1, factory.getPool())).thenReturn(table1);
Mockito.when(delegate.getTable(t2, factory.getPool())).thenReturn(table2);
Mockito.when(delegate.getTable(t3, factory.getPool())).thenReturn(table3);
HTableInterface ft1 = factory.getTable(t1);
HTableInterface ft2 = factory.getTable(t2);
ft1.close();
HTableInterface ft3 = factory.getTable(t3);
// get the same table a second time, after it has gone out of cache
factory.getTable(t1);
Mockito.verify(delegate, Mockito.times(2)).getTable(t1, factory.getPool());
Mockito.verify(delegate, Mockito.times(1)).getTable(t2, factory.getPool());
Mockito.verify(delegate, Mockito.times(1)).getTable(t3, factory.getPool());
Mockito.verify(table1).close();
}
Aggregations