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.Assert.assertNotNull;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.TableNameTestRule;
027import org.apache.hadoop.hbase.client.Get;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.regionserver.DisabledRegionSplitPolicy;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.testclassification.RPCTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.util.LoadTestKVGenerator;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Rule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045@Category({ RPCTests.class, MediumTests.class })
046public class TestSimpleRpcServer {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestSimpleRpcServer.class);
051
052  private static final byte[] FAMILY = Bytes.toBytes("f");
053  private static final byte[] QUALIFIER = Bytes.toBytes("q");
054  private static final int NUM_ROWS = 100;
055  private static final int MIN_LEN = 1000;
056  private static final int MAX_LEN = 1000000;
057  protected static final LoadTestKVGenerator GENERATOR = new LoadTestKVGenerator(MIN_LEN, MAX_LEN);
058  protected static HBaseTestingUtil TEST_UTIL;
059
060  @Rule
061  public TableNameTestRule name = new TableNameTestRule();
062
063  @SuppressWarnings("deprecation")
064  @BeforeClass
065  public static void setupClass() throws Exception {
066    // A subclass may have already created TEST_UTIL and is now upcalling to us
067    if (TEST_UTIL == null) {
068      TEST_UTIL = new HBaseTestingUtil();
069    }
070    // Set RPC server impl to SimpleRpcServer
071    TEST_UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
072      SimpleRpcServer.class.getName());
073    TEST_UTIL.startMiniCluster();
074  }
075
076  @AfterClass
077  public static void tearDownClass() throws Exception {
078    TEST_UTIL.shutdownMiniCluster();
079  }
080
081  @Test
082  public void testSimpleRpcServer() throws Exception {
083    doTest(name.getTableName());
084  }
085
086  protected void doTest(TableName tableName) throws Exception {
087    // Splitting just complicates the test scenario, disable it
088    final TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
089      .setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName()).build();
090    try (Table table =
091      TEST_UTIL.createTable(desc, new byte[][] { FAMILY }, TEST_UTIL.getConfiguration())) {
092      // put some test data
093      for (int i = 0; i < NUM_ROWS; i++) {
094        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
095        final byte[] v = GENERATOR.generateRandomSizeValue(rowKey, QUALIFIER);
096        table.put(new Put(rowKey).addColumn(FAMILY, QUALIFIER, v));
097      }
098      // read to verify it.
099      for (int i = 0; i < NUM_ROWS; i++) {
100        final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i));
101        final Result r = table.get(new Get(rowKey).addColumn(FAMILY, QUALIFIER));
102        assertNotNull("Result was empty", r);
103        final byte[] v = r.getValue(FAMILY, QUALIFIER);
104        assertNotNull("Result did not contain expected value", v);
105        assertTrue("Value was not verified", LoadTestKVGenerator.verify(v, rowKey, QUALIFIER));
106      }
107    }
108  }
109
110}