Skip to content

Commit

Permalink
Increase code reusability in UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhaoBQ committed Apr 30, 2021
1 parent 361ad39 commit 0c52346
Showing 1 changed file with 16 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
Expand Down Expand Up @@ -371,36 +372,20 @@ public void testHonorsMergeEnabledInTD() {

@Test
public void testHonorsMinimumRegionCount() {
conf.setInt(MERGE_MIN_REGION_COUNT_KEY, 1);
final TableName tableName = name.getTableName();
final List<RegionInfo> regionInfos = createRegionInfos(tableName, 3);
// create a table topology that results in both a merge plan and a split plan. Assert that the
// merge is only created when the when the number of table regions is above the region count
// threshold, and that the split plan is create in both cases.
final Map<byte[], Integer> regionSizes = createRegionSizesMap(regionInfos, 1, 1, 10);
setupMocksForNormalizer(regionSizes, regionInfos);

List<NormalizationPlan> plans = normalizer.computePlansForTable(tableDescriptor);
assertThat(plans, contains(
new SplitNormalizationPlan(regionInfos.get(2), 10),
new MergeNormalizationPlan.Builder()
.addTarget(regionInfos.get(0), 1)
.addTarget(regionInfos.get(1), 1)
.build()));

// have to call setupMocks again because we don't have dynamic config update on normalizer.
conf.setInt(MERGE_MIN_REGION_COUNT_KEY, 4);
setupMocksForNormalizer(regionSizes, regionInfos);
assertThat(normalizer.computePlansForTable(tableDescriptor), contains(
new SplitNormalizationPlan(regionInfos.get(2), 10)));
honorsMinimumRegionCount(MERGE_MIN_REGION_COUNT_KEY);
}

/**
* Test the backward compatibility of the deprecated MIN_REGION_COUNT_KEY configuration.
*/
@Test
public void testHonorsOldMinimumRegionCount() {
conf.setInt(MIN_REGION_COUNT_KEY, 1);
honorsMinimumRegionCount(MIN_REGION_COUNT_KEY);
assertNull(conf.get(MERGE_MIN_REGION_COUNT_KEY));
}

private void honorsMinimumRegionCount(String confKey) {
conf.setInt(confKey, 1);
final TableName tableName = name.getTableName();
final List<RegionInfo> regionInfos = createRegionInfos(tableName, 3);
// create a table topology that results in both a merge plan and a split plan. Assert that the
Expand All @@ -418,34 +403,15 @@ public void testHonorsOldMinimumRegionCount() {
.build()));

// have to call setupMocks again because we don't have dynamic config update on normalizer.
conf.setInt(MIN_REGION_COUNT_KEY, 4);
conf.setInt(confKey, 4);
setupMocksForNormalizer(regionSizes, regionInfos);
assertThat(normalizer.computePlansForTable(tableDescriptor), contains(
new SplitNormalizationPlan(regionInfos.get(2), 10)));
}

@Test
public void testHonorsMinimumRegionCountInTD() {
conf.setInt(MERGE_MIN_REGION_COUNT_KEY, 1);
final TableName tableName = name.getTableName();
final List<RegionInfo> regionInfos = createRegionInfos(tableName, 3);
// create a table topology that results in both a merge plan and a split plan. Assert that the
// merge is only created when the when the number of table regions is above the region count
// threshold, and that the split plan is create in both cases.
final Map<byte[], Integer> regionSizes = createRegionSizesMap(regionInfos, 1, 1, 10);
setupMocksForNormalizer(regionSizes, regionInfos);

List<NormalizationPlan> plans = normalizer.computePlansForTable(tableDescriptor);
assertThat(plans, contains(
new SplitNormalizationPlan(regionInfos.get(2), 10),
new MergeNormalizationPlan.Builder()
.addTarget(regionInfos.get(0), 1)
.addTarget(regionInfos.get(1), 1)
.build()));

when(tableDescriptor.getValue(MIN_REGION_COUNT_KEY)).thenReturn("4");
assertThat(normalizer.computePlansForTable(tableDescriptor), contains(
new SplitNormalizationPlan(regionInfos.get(2), 10)));
honorsOldMinimumRegionCountInTD(MERGE_MIN_REGION_COUNT_KEY);
}

/**
Expand All @@ -454,7 +420,11 @@ public void testHonorsMinimumRegionCountInTD() {
*/
@Test
public void testHonorsOldMinimumRegionCountInTD() {
conf.setInt(MIN_REGION_COUNT_KEY, 1);
honorsOldMinimumRegionCountInTD(MIN_REGION_COUNT_KEY);
}

private void honorsOldMinimumRegionCountInTD(String confKey) {
conf.setInt(confKey, 1);
final TableName tableName = name.getTableName();
final List<RegionInfo> regionInfos = createRegionInfos(tableName, 3);
// create a table topology that results in both a merge plan and a split plan. Assert that the
Expand All @@ -471,7 +441,7 @@ public void testHonorsOldMinimumRegionCountInTD() {
.addTarget(regionInfos.get(1), 1)
.build()));

when(tableDescriptor.getValue(MIN_REGION_COUNT_KEY)).thenReturn("4");
when(tableDescriptor.getValue(confKey)).thenReturn("4");
assertThat(normalizer.computePlansForTable(tableDescriptor), contains(
new SplitNormalizationPlan(regionInfos.get(2), 10)));
}
Expand Down

0 comments on commit 0c52346

Please sign in to comment.