001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.jupiter.api.Assertions.assertNotNull;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.Get;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.Result;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031import org.apache.hadoop.hbase.regionserver.DisabledRegionSplitPolicy;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
034import org.junit.jupiter.api.BeforeEach;
035import org.junit.jupiter.api.TestInfo;
036
037public abstract class AbstractTestRpcServer {
038
039  private static final byte[] FAMILY = Bytes.toBytes("f");
040  private static final byte[] QUALIFIER = Bytes.toBytes("q");
041  private static final int NUM_ROWS = 100;
042  private static final int MIN_LEN = 1000;
043  private static final int MAX_LEN = 1000000;
044  protected static final LoadTestKVGenerator GENERATOR = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
045  protected static HBaseTestingUtil TEST_UTIL;
046  protected TableName tableName;
047
048  @BeforeEach
049  public void setUpTest(TestInfo testInfo) {
050    tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
051  }
052
053  protected void doTest(TableName tableName) throws Exception {
054    // Splitting just complicates the test scenario, disable it
055    final TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
056      .setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName()).build();
057    try (Table table =
058      TEST_UTIL.createTable(desc, new byte[][] { FAMILY }, TEST_UTIL.getConfiguration())) {
059      // put some test data
060      for (int i = 0; i < NUM_ROWS; i++) {
061        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
062        final byte[] v = GENERATOR.generateRandomSizeValue(rowKey, QUALIFIER);
063        table.put(new Put(rowKey).addColumn(FAMILY, QUALIFIER, v));
064      }
065      // read to verify it.
066      for (int i = 0; i < NUM_ROWS; i++) {
067        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
068        final Result r = table.get(new Get(rowKey).addColumn(FAMILY, QUALIFIER));
069        assertNotNull(r, "Result was empty");
070        final byte[] v = r.getValue(FAMILY, QUALIFIER);
071        assertNotNull(v, "Result did not contain expected value");
072        assertTrue(LoadTestKVGenerator.verify(v, rowKey, QUALIFIER), "Value was not verified");
073      }
074    }
075  }
076}