/** * @param implementation An {@link HRegionInterface} instance; you'll likely want to pass a mocked * HRS; can be null. * @return Mock up a connection that returns a {@link org.apache.hadoop.conf.Configuration} when * {@link HConnection#getConfiguration()} is called, a 'location' when {@link * HConnection#getRegionLocation(byte[], byte[], boolean)} is called, and that returns the * passed {@link HRegionInterface} instance when {@link * HConnection#getHRegionConnection(String, int)} is called (Be sure call {@link * HConnectionManager#deleteConnection(org.apache.hadoop.conf.Configuration)} when done with * this mocked Connection. * @throws IOException */ private HConnection mockConnection(final HRegionInterface implementation) throws IOException { HConnection connection = HConnectionTestingUtility.getMockedConnection(UTIL.getConfiguration()); Mockito.doNothing().when(connection).close(); // Make it so we return any old location when asked. final HRegionLocation anyLocation = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, SN.getHostname(), SN.getPort()); Mockito.when( connection.getRegionLocation( (byte[]) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean())) .thenReturn(anyLocation); Mockito.when(connection.locateRegion((byte[]) Mockito.any(), (byte[]) Mockito.any())) .thenReturn(anyLocation); if (implementation != null) { // If a call to getHRegionConnection, return this implementation. Mockito.when(connection.getHRegionConnection(Mockito.anyString(), Mockito.anyInt())) .thenReturn(implementation); } return connection; }
/** * Calls {@link #getMockedConnection(Configuration)} and then mocks a few more of the popular * {@link HConnection} methods so they do 'normal' operation (see return doc below for list). Be * sure to shutdown the connection when done by calling {@link * HConnectionManager#deleteConnection(Configuration, boolean)} else it will stick around; this is * probably not what you want. * * @param implementation An {@link HRegionInterface} instance; you'll likely want to pass a mocked * HRS; can be null. * @param conf Configuration to use * @param implementation An HRegionInterface; can be null but is usually itself a mock. * @param sn ServerName to include in the region location returned by this <code>implementation * </code> * @param hri HRegionInfo to include in the location returned when getRegionLocation is called on * the mocked connection * @return Mock up a connection that returns a {@link Configuration} when {@link * HConnection#getConfiguration()} is called, a 'location' when {@link * HConnection#getRegionLocation(byte[], byte[], boolean)} is called, and that returns the * passed {@link HRegionInterface} instance when {@link * HConnection#getHRegionConnection(String, int)} is called (Be sure call {@link * HConnectionManager#deleteConnection(org.apache.hadoop.conf.Configuration, boolean)} when * done with this mocked Connection. * @throws IOException */ public static HConnection getMockedConnectionAndDecorate( final Configuration conf, final HRegionInterface implementation, final ServerName sn, final HRegionInfo hri) throws IOException { HConnection c = HConnectionTestingUtility.getMockedConnection(conf); Mockito.doNothing().when(c).close(); // Make it so we return a particular location when asked. final HRegionLocation loc = new HRegionLocation(hri, sn.getHostname(), sn.getPort()); Mockito.when( c.getRegionLocation( (byte[]) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean())) .thenReturn(loc); Mockito.when(c.locateRegion((byte[]) Mockito.any(), (byte[]) Mockito.any())).thenReturn(loc); if (implementation != null) { // If a call to getHRegionConnection, return this implementation. Mockito.when(c.getHRegionConnection(Mockito.anyString(), Mockito.anyInt())) .thenReturn(implementation); } return c; }
@Test(expected = RetriesExhaustedException.class) public void testTimeoutWaitForMeta() throws IOException, InterruptedException { HConnection connection = HConnectionTestingUtility.getMockedConnection(UTIL.getConfiguration()); final CatalogTracker ct = constructAndStartCatalogTracker(connection); ct.waitForMeta(100); }