Skip to content

Commit

Permalink
add UT for MetaLocationCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Apache9 committed Jul 20, 2020
1 parent 488a9b7 commit 9e3f6b8
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class MetaLocationCache implements Stoppable {
@VisibleForTesting
static final int DEFAULT_SYNC_INTERVAL_SECONDS = 1;

private static final String FETCH_TIMEOUT_MS =
@VisibleForTesting
static final String FETCH_TIMEOUT_MS =
"hbase.master.meta-location-cache.fetch-timeout-ms";

// default timeout 1 second
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.master;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.ChoreService;
import org.apache.hadoop.hbase.HBaseClassTestRule;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.AsyncClusterConnection;
import org.apache.hadoop.hbase.client.RegionInfoBuilder;
import org.apache.hadoop.hbase.testclassification.MasterTests;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.util.FutureUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category({ MasterTests.class, MediumTests.class })
public class TestMetaLocationCache {

@ClassRule
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestMetaFixerNoCluster.class);

private static Configuration CONF = HBaseConfiguration.create();

private static ChoreService CHORE_SERVICE;

private static int FETCH_TIMEOUT_MS = 100;

private MasterServices master;

private MetaLocationCache cache;

@BeforeClass
public static void setUpBeforeClass() {
CONF.setInt(MetaLocationCache.SYNC_INTERVAL_SECONDS, 1);
CONF.setInt(MetaLocationCache.FETCH_TIMEOUT_MS, FETCH_TIMEOUT_MS);
CHORE_SERVICE = new ChoreService("TestMetaLocationCache");
}

@AfterClass
public static void tearDownAfterClass() {
CHORE_SERVICE.shutdown();
}

@Before
public void setUp() {
master = mock(MasterServices.class);
when(master.getConfiguration()).thenReturn(CONF);
when(master.getChoreService()).thenReturn(CHORE_SERVICE);
cache = new MetaLocationCache(master);
}

@After
public void tearDown() {
if (cache != null) {
cache.stop("test end");
}
}

@Test
public void testError() throws InterruptedException {
AsyncClusterConnection conn = mock(AsyncClusterConnection.class);
when(conn.getAllMetaRegionLocations(anyInt()))
.thenReturn(FutureUtils.failedFuture(new RuntimeException("inject error")));
when(master.getAsyncClusterConnection()).thenReturn(conn);
Thread.sleep(2000);
assertTrue(cache.getAllMetaRegionLocations(true).isEmpty());

HRegionLocation loc =
new HRegionLocation(RegionInfoBuilder.newBuilder(TableName.META_TABLE_NAME).build(),
ServerName.valueOf("localhost", 12345, System.currentTimeMillis()));
when(conn.getAllMetaRegionLocations(anyInt()))
.thenReturn(CompletableFuture.completedFuture(Arrays.asList(loc)));
Thread.sleep(2000);
List<HRegionLocation> list = cache.getAllMetaRegionLocations(false);
assertEquals(1, list.size());
assertEquals(loc, list.get(0));
}
}

0 comments on commit 9e3f6b8

Please sign in to comment.